apollo-client
apollo-client copied to clipboard
fetchMore doesn't trigger callback on graphql/network error
Intended outcome:
When an error occurs while using fetchMore to get the next page (either response with errors or network error), the query should be notified of it when using errorPolicy: 'all'.
Actual outcome: GQL Request failed and the watch callback was not called.
How to reproduce the issue:
My case: Use relay pagination, have a query which uses { fetchPolicy: 'cache-and-network', nextFetchPolicy: 'cache-first', errorPolicy: 'all' }, have a callback which handles errors property. Load initial page, then either have invalid after cursor value in fetchMore to have grapql endpoint fail when parsing it, or go offline. Then try to load the next page.
Versions
System:
OS: Windows 10 10.0.18362
Binaries:
Node: 12.18.3 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.14.6 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 84.0.4147.125
Edge: Spartan (44.18362.449.0)
npmPackages:
@apollo/client: ^3.1.3 => 3.1.3
apollo-angular: ^2.0.3 => 2.0.3
I can confirm that this is reproducible on @apollo/client: 3.2.0 even with the default errorPolicy, fetchPolicy, nextFetchPolicy values
If someone can provide a small runnable reproduction that demonstrates this happening with @apollo/client@latest, we'll take a closer look. Thanks!
@hwillson Here is my reproduction (use branch fetchmore-error-callback): https://github.com/dylanwulf/react-apollo-error-template/tree/fetchmore-error-callback
If the error policy is none, then the fetchMore function will throw but onError will NOT be called. If the error policy is all, then fetchMore will NOT throw and the onError callback will STILL NOT be called.
I think part of the problem here is that the documentation is very unclear. When fetchMore results in an error, is it supposed to call onError? Is it supposed to throw? When the error policy is all, is onError supposed to be called or not? I have no idea.
I can confirm that this is reproducible on @apollo/client: 3.7.3 with errorPolicy: default | 'none'
Still happening
Confirm the problem
In my case, I am merging the previous and the new results. I had to throw the error first from withinupdateQuery
fetchMore({
variables: {
...getTemplateVersionsQueryVars,
},
updateQuery: (previousQueryResult, { fetchMoreResult }) => {
if (!fetchMoreResult) return previousQueryResult;
if (fetchMoreResult instanceof ApolloError) {
throw fetchMoreResult;
}
return {
getTemplateVersions: {
...fetchMoreResult.getTemplateVersions,
edges: [
...(previousQueryResult.getTemplateVersions.edges ?? []),
...(fetchMoreResult.getTemplateVersions.edges ?? []),
],
},
};
},
}).catch((error) => console.log({ fetchMoreError: error }));
https://stackoverflow.com/questions/54437901/how-to-error-handle-in-fetchmore-pagination/78734951#78734951
Linking in this issue for anyone that ends up here, https://github.com/apollographql/apollo-client/issues/12352