Skip to content

Commit 0a09828

Browse files
authored
Migration docs improvements (#13034)
1 parent d27cff4 commit 0a09828

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

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

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ End: Inserted by Apollo Client 3->4 migration codemod.
366366
367367
### Running the codemod
368368
369+
<Note>
370+
371+
The codemod requires a Node.js version that supports loading ECMAScript modules with `require`. See the table in the [Node.js documentation](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require) for the versions that support this feature (you might need to expand the "History" section to see the table.)
372+
373+
</Note>
374+
369375
To run the codemod, use the following command:
370376
371377
```sh showLineNumbers=false
@@ -378,6 +384,12 @@ This command behaves similarly to [`jscodeshift`](https://github.com/facebook/js
378384
379385
</Note>
380386
387+
<Note>
388+
389+
This example targets the `src` directory with the codemod. Replace `src` with the file pattern applicable to your file structure if it differs.
390+
391+
</Note>
392+
381393
For more details on the available options, run the command using the `--help` option.
382394
383395
```sh showLineNumbers=false
@@ -393,12 +405,6 @@ npx @apollo/client-codemod-migrate-3-to-4 --parser ts --extensions ts src
393405
npx @apollo/client-codemod-migrate-3-to-4 --parser tsx --extensions tsx src
394406
```
395407
396-
<Note>
397-
398-
This example targets the `src` directory with the codemod. Replace `src` with the file pattern applicable to your file structure if it differs.
399-
400-
</Note>
401-
402408
### Running specific modifications
403409
404410
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:
@@ -1032,6 +1038,60 @@ If you rely on the initial value to read data from the subscription, we recommen
10321038
10331039
</Caution>
10341040
1041+
### `ObservableQuery` no longer inherits from `Observable`
1042+
1043+
`ObservableQuery` instances returned from `client.watchQuery` no longer inherit from `Observable`. This might cause TypeScript errors when using `ObservableQuery` with some RxJS utilities that expect `Observable` instances, such as [`firstValueFrom`](https://rxjs.dev/api/index/function/firstValueFrom).
1044+
1045+
Convert `ObservableQuery` to an `Observable` using the [`from`](https://rxjs.dev/api/index/function/from) function.
1046+
1047+
```ts
1048+
import { from } from "rxjs";
1049+
1050+
const observable = from(observableQuery);
1051+
// ^? Observable<ObservableQuery.Result<MaybeMasked<TData>>>
1052+
```
1053+
1054+
<Note>
1055+
1056+
`ObservableQuery` implements the [`Subscribable`](https://rxjs.dev/api/index/interface/Subscribable) interface, so you can call `subscribe` directly without converting to an `Observable`. It also implements the `pipe` function, so you can chain operators.<br/><br/>
1057+
1058+
Use `from` only for functions that require an `Observable` instance.
1059+
1060+
</Note>
1061+
1062+
### Removal of `Observable` utilities
1063+
1064+
Apollo Client 3 included several utilities for working with `Observable` instances from the `zen-observable` library. Apollo Client 4 removes these utilities in favor of RxJS's operators.
1065+
1066+
#### `fromError`
1067+
1068+
Use the [`throwError`](https://rxjs.dev/api/index/function/throwError) function instead.
1069+
1070+
```ts
1071+
const observable = fromError(new Error("Oops"));// [!code --]
1072+
const observable = throwError(() => {// [!code ++:3]
1073+
return new Error("Oops");
1074+
});
1075+
```
1076+
1077+
#### `fromPromise`
1078+
1079+
Use the [`from`](https://rxjs.dev/api/index/function/from) function instead.
1080+
1081+
```ts
1082+
const observable = fromPromise(promise);// [!code --]
1083+
const observable = from(promise);// [!code ++]
1084+
```
1085+
1086+
#### `toPromise`
1087+
1088+
Use the [`firstValueFrom`](https://rxjs.dev/api/index/function/firstValueFrom) function instead.
1089+
1090+
```ts
1091+
const promise = toPromise(observable);// [!code --]
1092+
const promise = firstValueFrom(observable);// [!code ++]
1093+
```
1094+
10351095
## New error handling
10361096
10371097
Error handling in Apollo Client 4 has changed significantly to be more predictable and intuitive.

0 commit comments

Comments
 (0)