You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
63
63
64
64
This codemod consists of the following steps:
65
65
@@ -76,8 +76,7 @@ This codemod consists of the following steps:
76
76
fn<ApolloLink.Result>();
77
77
```
78
78
-`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:
81
80
```ts
82
81
import { createHttpLink } from"@apollo/client";
83
82
const link =createHttpLink({ uri: "https://example.com/graphql" });
@@ -87,10 +86,34 @@ This codemod consists of the following steps:
const link =newHttpLink({ uri: "https://example.com/graphql" });
89
88
```
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
+
```
91
101
-`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.
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
117
154
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:
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>
128
168
129
169
### Move from manual CJS/ESM imports to `exports`
130
170
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.
133
173
134
174
<details>
135
175
<summary><h3style="display:inline">List of all changed imports</h3><br/>(click to expand)</summary>
@@ -408,20 +448,19 @@ The following individual imports have been renamed, moved to new entry points an
408
448
409
449
</details>
410
450
411
-
## Replacing removed exports
451
+
###Replacing removed exports
412
452
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.
414
454
415
455
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)
416
456
417
-
## Update your ApolloClient creation
457
+
## Update the initialization of `ApolloClient`
418
458
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.
420
460
421
-
### Implicitly create a new`HttpLink`
461
+
### Explicitly provide`HttpLink`
422
462
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`.
425
464
426
465
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.
427
466
@@ -442,7 +481,7 @@ const client = new ApolloClient({
442
481
});
443
482
```
444
483
445
-
### Move `name` and `version` into the `clientAwareness` option
484
+
### Migrating client awareness options
446
485
447
486
```ts
448
487
const client =newApolloClient({
@@ -455,9 +494,9 @@ const client = new ApolloClient({
455
494
});
456
495
```
457
496
458
-
### If using local state: add a new `LocalState` instance
497
+
### Updating local state
459
498
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.
461
500
462
501
```ts
463
502
import {
@@ -472,7 +511,7 @@ const client = new ApolloClient({
472
511
});
473
512
```
474
513
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.
476
515
477
516
```ts
478
517
import {
@@ -493,7 +532,7 @@ const client = new ApolloClient({
493
532
});
494
533
```
495
534
496
-
### Change `connectToDevTools` to `devtools.enabled`
535
+
### Change `connectToDevTools`
497
536
498
537
The `connectToDevTools` option has been replaced with a new `devtools` option that contains an `enabled` property.
499
538
@@ -506,9 +545,9 @@ const client = new ApolloClient({
506
545
});
507
546
```
508
547
509
-
### Change `disableNetworkFetches` to `prioritizeCacheValues`
548
+
### Change `disableNetworkFetches`
510
549
511
-
If you are using the `disableNetworkFetches` option, note that it has been renamed to `prioritizeCacheValues`.
550
+
The `disableNetworkFetches` optionhas been renamed to `prioritizeCacheValues` to better describe its behavior.
0 commit comments