Skip to content

Commit 1ae6834

Browse files
authored
Update migration section with info about the promise (#12961)
1 parent 231de94 commit 1ae6834

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

.size-limits.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43936,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38686,
4-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33476,
5-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27519
2+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43812,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38745,
4+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33456,
5+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27523
66
}

docs/source/data/queries.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ The promise resolves with an object that includes the error and any partial data
502502
const handleClick = async () => {
503503
const { data, error } = await getDogs();
504504

505-
if (error && CombinedGraphQLErrors.is(error)) {
505+
if (CombinedGraphQLErrors.is(error)) {
506506
// handle GraphQL errors returned by the query
507507
}
508508
};

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,58 @@ async function onUserInteraction() {
14451445
14461446
</Tip>
14471447
1448+
#### Changes to promise resolution with errors for the `execute` function
1449+
1450+
The promise returned from the `execute` function is now consistently resolved or rejected based on your configured `errorPolicy` when errors are returned. With an `errorPolicy` of `none`, the promise always rejects. With an `errorPolicy` of `all`, the promise always resolves and sets the `error` property on the result. With an `errorPolicy` of `ignore`, the promise always resolves and discards the errors.
1451+
1452+
Previously, Apollo Client 3 resolved the promise when GraphQL errors were returned when using an `errorPolicy` of `none`. Network errors always caused the promise to reject, regardless of your configured error policy. This change unifies the error behavior across all error types to ensure errors are handled consistently.
1453+
1454+
To migrate code that uses an `errorPolicy` of `none`, wrap the call to `execute` in a `try`/`catch` block and use `CombinedGraphQLErrors.is(error)` to check for GraphQL errors.
1455+
1456+
```ts disableCopy=true
1457+
const [execute] = useLazyQuery(QUERY, {
1458+
errorPolicy: "none",
1459+
});
1460+
1461+
async function onUserInteraction() {
1462+
const { data, error } = await execute({ variables });// [!code --:4]
1463+
if (error.graphQLErrors) {
1464+
// handle GraphQL errors
1465+
}
1466+
1467+
try {// [!code ++:6]
1468+
const { data } = await execute({ variables });
1469+
} catch (error) {
1470+
if (CombinedGraphQLErrors.is(error)) {
1471+
// handle GraphQL errors
1472+
}
1473+
}
1474+
}
1475+
```
1476+
1477+
To migrate code that uses an `errorPolicy` of `all`, read the error from the `error` property returned in the result. You can optionally remove any `try`/`catch` statements because the promise resolves for all error types.
1478+
1479+
```ts disableCopy=true
1480+
const [execute] = useLazyQuery(QUERY, {
1481+
errorPolicy: "all",
1482+
});
1483+
1484+
async function onUserInteraction() {
1485+
try {// [!code --:7]
1486+
const { data } = await execute({ variables });
1487+
} catch (error) {
1488+
if (error.graphQLErrors) {
1489+
// handle GraphQL errors
1490+
}
1491+
}
1492+
1493+
const { data, error } = await execute({ variables });// [!code ++:4]
1494+
if (CombinedGraphQLErrors.is(error)) {
1495+
// handle GraphQL errors
1496+
}
1497+
}
1498+
```
1499+
14481500
### Changes to `useMutation`
14491501
14501502
`useMutation` has been modified to work with our [philosophy](#a-note-about-hook-usage) that React hooks should be used to synchronize hook state with your component.

0 commit comments

Comments
 (0)