Skip to content

Commit 244f137

Browse files
Apply suggestions from code review
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
1 parent cd15cd2 commit 244f137

1 file changed

Lines changed: 70 additions & 31 deletions

File tree

docs/source/migrating/apollo-client-4-migration.mdx

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ npm install @apollo/client graphql rxjs
5959

6060
## Codemod
6161

62-
To ease the migration process, we have created a codemod that will automatically update your codebase to use the new imports and APIs in Apollo Client 4.
62+
To ease the migration process, we have provided a codemod that automatically updates your codebase.
6363

6464
This codemod consists of the following steps:
6565

@@ -76,8 +76,7 @@ This codemod consists of the following steps:
7676
fn<ApolloLink.Result>();
7777
```
7878
- `links` step:
79-
- In Apollo Client 4, the links have been unified to all be classes. This step updates your code to use the new class-based links.
80-
For example, it will change:
79+
- Updates the usage of Apollo-provided links to their associated class implementation. For example:
8180
```ts
8281
import { createHttpLink } from "@apollo/client";
8382
const link = createHttpLink({ uri: "https://example.com/graphql" });
@@ -87,10 +86,34 @@ This codemod consists of the following steps:
8786
import { HttpLink } from "@apollo/client/link/http";
8887
const link = new HttpLink({ uri: "https://example.com/graphql" });
8988
```
90-
- The `from`, `split` and `concat` functions should now be used as methods on the `ApolloLink` class, so this step will also update your code to use these static methods.
89+
- Updates the usage of `from`, `split` and `concat` functions from `@apollo/client/link` to use the static methods on the `ApolloLink` class. For example:
90+
```ts
91+
import { from } from "@apollo/client";
92+
93+
const link = from([a, b, c]);
94+
```
95+
becomes
96+
```ts
97+
import { ApolloLink } from "@apollo/client";
98+
99+
const link = ApolloLink.from([a, b, c]);
100+
```
91101
- `removals`
92-
In Apollo Client 4, a number of exports have been removed for various reasons.
93-
This step will move all of those imports to point at `@apollo/client/v4-migration`, which is a special migration entry point. All imports from that entry point are type-only and have a DocBlock explaining why the specific export was removed. Many of those DocBlocks also contain migration instructions to help you move away from those removed exports.
102+
- Updates exports removed from Apollo Client to a special `@apollo/client/v4-migration` entrypoint. This is a type-only entrypoint that contains doc blocks with migration instructions for each removed item.
103+
```ts
104+
import { Concast } from "@apollo/client";
105+
```
106+
becomes
107+
```ts
108+
import { Concast } from "@apollo/client/v4-migration";
109+
```
110+
111+
<Note>
112+
113+
Any runtime values exported from `@apollo/client/v4-migration` will throw an error at runtime since their implementations do not exist.
114+
115+
</Note>
116+
```
94117
95118
### Running the codemod
96119
@@ -100,9 +123,17 @@ To run the codemod, use the following command:
100123
npx @apollo/client-codemod-migrate-3-to-4 src
101124
```
102125

103-
which will behave just like the command line tool `jscodeshift` with a preselected codemod.
126+
<Note>
127+
128+
This command behaves similarly to [`jscodeshift`](https://github.com/facebook/jscodeshift) with a preselected codemod.
129+
130+
</Note>
104131

105-
Run `npx @apollo/client-codemod-migrate-3-to-4 --help` to see all available options.
132+
For more details on the available options, run the command using the `--help` option.
133+
134+
```sh
135+
npx @apollo/client-codemod-migrate-3-to-4 --help
136+
```
106137

107138
### Using the Codemod with TypeScript
108139

@@ -113,29 +144,38 @@ npx @apollo/client-codemod-migrate-3-to-4 --parser ts --extensions ts src
113144
npx @apollo/client-codemod-migrate-3-to-4 --parser tsx --extensions tsx src
114145
```
115146

116-
### Running only specific steps
147+
<Note>
148+
149+
This example targets the `src` directory with the codemod. Replace `src` with the file pattern applicable to your file structure if it differs.
150+
151+
</Note>
152+
153+
### Running specific modifications
117154

118-
You can also run the codemod with only specific steps by using the `--codemod` option. For example, to run only the `imports` and `links` steps, you can use:
155+
If you prefer to migrate your application more selectively instead of all at once, you can specify specific modifications using the `--codemod` option. For example, to run only the `imports` and `links` modifications, run the following command:
119156

120157
```sh
121158
npx @apollo/client-codemod-migrate-3-to-4 --codemod imports --codemod links src
122159
```
123160

124161
## Updating imports
125162

126-
If you used the [Codemod](#running-the-codemod) to update your imports, you can skip this section.
127-
If you did not use the codemod, you will need to manually update your imports to point at the right files.
163+
<Note>
164+
165+
This section contains instructions for manually updating imports. If you used the [Codemod](#running-the-codemod) to update your imports, you can safely skip this section.
166+
167+
</Note>
128168

129169
### Move from manual CJS/ESM imports to `exports`
130170

131-
Apollo Client 4 now has an `exports` field in the `package.json`, which means that you previously used imports like `@apollo/client/react/index.js` or `@apollo/client/react/react.cjs`, you should now use the `@apollo/client/react` entry point.
132-
Your bundler will be aware of the different module formats provided and be able to resolve the right one for you, based on import conditions and the `exports` field of the `package.json`.
171+
Apollo Client 4 now includes an `exports` field in the package's `package.json` definition. Instead of importing `.js` or `.cjs` files directly (e.g. `@apollo/client/react/index.js`, `@apollo/client/react/react.cjs`, etc.), you now import from the entrypoint instead (e.g. `@apollo/client/react`).
172+
Your bundler is aware of the different module formats and uses the `exports` field of the package's `package.json` to resolve the right format.
133173

134174
<details>
135175
<summary><h3 style="display:inline">List of all changed imports</h3><br/>(click to expand)</summary>
136176

137177
The following entry points have been renamed:
138-
| previous entry point | new entry point |
178+
| Previous entry point | New entry point |
139179
| --------------------- | -------------------- |
140180
| `@apollo/client/core` | `@apollo/client` |
141181
| `@apollo/client/link/core` | `@apollo/client/link` |
@@ -144,7 +184,7 @@ The following entry points have been renamed:
144184
| `@apollo/client/testing/core` | `@apollo/client/testing` |
145185

146186
The following entry points have been removed:
147-
| previous entry point | reason |
187+
| Previous entry point | Reason |
148188
| --------------------- | -------------------- |
149189
| `@apollo/client/react/components` | The render prop components were already deprecated in Apollo Client 3.x and have been removed in 4. |
150190
| `@apollo/client/react/hoc` | The higher order components were already deprecated in Apollo Client 3.x and have been removed in 4. |
@@ -157,7 +197,7 @@ The following individual imports have been renamed, moved to new entry points an
157197

158198
<table>
159199
<thead>
160-
<tr><th>previous entry point</th><th>new entry point</th></tr>
200+
<tr><th>Previous entry point</th><th>New entry point</th></tr>
161201
<tr><td>previous import name</td><td>new import name</td></tr>
162202
</thead>
163203
<tbody>
@@ -408,20 +448,19 @@ The following individual imports have been renamed, moved to new entry points an
408448

409449
</details>
410450

411-
## Replacing removed exports
451+
### Replacing removed exports
412452

413-
If you used the [Codemod](#running-the-codemod) to update your imports, all imports for removed exports have been replaced with imports from `@apollo/client/v4-migration`. You should get a TypeScript error everywhere you use a removed export, and hovering your mouse over it, you get more information about why the export was removed and what you can use instead.
453+
If you used the [Codemod](#running-the-codemod) to update your imports, all removed exports moved to the `@apollo/client/v4-migration` entry point. You should get a TypeScript error everywhere you use a removed export. When you hover over the export, you'll get more information about why the export was removed along with migration instructions.
414454

415455
For a list of all removed imports and recommended actions, see [node_modules/@apollo/client/v4-migration.d.ts](https://app.unpkg.com/@apollo/client@^4.0.0-rc/files/v4-migration.d.ts)
416456

417-
## Update your ApolloClient creation
457+
## Update the initialization of `ApolloClient`
418458

419-
Some of the constructor options of `ApolloClient` have changed in Apollo Client 4. This section will help you update your `ApolloClient` creation code to the new options.
459+
Several of the constructor options of `ApolloClient` have changed in Apollo Client 4. This section provides instruction on migrating to the new options when initializing your `ApolloClient` instance.
420460

421-
### Implicitly create a new `HttpLink`
461+
### Explicitly provide `HttpLink`
422462

423-
The shorthand annotation where you could pass `uri`, `headers` or `credentials` directly into the `ApolloClient` constructor has been removed.
424-
Instead, you now need to create a new `HttpLink` instance and pass it to the `link` option of `ApolloClient`.
463+
The `uri`, `headers`, and `credentials` options used to implicitly create an `HttpLink` have been removed. You now need to create a new `HttpLink` instance and pass it to the `link` option of `ApolloClient`.
425464

426465
Although it was convenient, it created a direct coupling with `HttpLink`, so even users that were not using `HttpLink` had to ship it in their bundle. This change enables you to use any link implementation you want, without having to ship `HttpLink` if you don't need it.
427466

@@ -442,7 +481,7 @@ const client = new ApolloClient({
442481
});
443482
```
444483

445-
### Move `name` and `version` into the `clientAwareness` option
484+
### Migrating client awareness options
446485

447486
```ts
448487
const client = new ApolloClient({
@@ -455,9 +494,9 @@ const client = new ApolloClient({
455494
});
456495
```
457496

458-
### If using local state: add a new `LocalState` instance
497+
### Updating local state
459498

460-
If you are using `@client` fields, you should create a new `LocalState` instance and pass it to the `ApolloClient` constructor.
499+
When using `@client` fields, you now need to create a new `LocalState` instance and provide it as the `localState` option to the `ApolloClient` constructor.
461500

462501
```ts
463502
import {
@@ -472,7 +511,7 @@ const client = new ApolloClient({
472511
});
473512
```
474513

475-
Additionally, if you are using local resolvers, you should pass the `resolvers` option to the `LocalState` constructor instead of the `ApolloClient` constructor.
514+
Additionally, if you are using local resolvers with the `resolvers` option, you need to move the `resolvers` option to the `LocalState` constructor instead of the `ApolloClient` constructor.
476515

477516
```ts
478517
import {
@@ -493,7 +532,7 @@ const client = new ApolloClient({
493532
});
494533
```
495534

496-
### Change `connectToDevTools` to `devtools.enabled`
535+
### Change `connectToDevTools`
497536

498537
The `connectToDevTools` option has been replaced with a new `devtools` option that contains an `enabled` property.
499538

@@ -506,9 +545,9 @@ const client = new ApolloClient({
506545
});
507546
```
508547

509-
### Change `disableNetworkFetches` to `prioritizeCacheValues`
548+
### Change `disableNetworkFetches`
510549

511-
If you are using the `disableNetworkFetches` option, note that it has been renamed to `prioritizeCacheValues`.
550+
The `disableNetworkFetches` option has been renamed to `prioritizeCacheValues` to better describe its behavior.
512551

513552
```ts
514553
const client = new ApolloClient({

0 commit comments

Comments
 (0)