-
Notifications
You must be signed in to change notification settings - Fork 787
onError prop does not fire when errorPolicy is set to all #2590
Comments
I am also seeing this and I just spent hours debugging this. The Hey @peggyrayzis is there an ETA for a fix on this? Thanks! |
Also in the boat of "hours debugging this". If nobody's picking it up I might give it a go myself |
Happy to review a PR for this - thanks! |
Hey @theobr, did you happen to make progress on this? Otherwise, any suggested alternative to this in the meantime? Thanks! |
Related: #2374 |
FWIW, this behavior is entirely my fault (well, I guess @hwillson may share a little bit of the blame). It seems to me as though this requires a little bit of thought:
My opinion (stated in that original PR) is that it feels like a bad idea to call @astorije has also pointed out that this is problematic if you need to set some state based on errors, because mutating state in a render prop is a no-no, so the current behavior is unsatisfactory for cases where you are setting state based on errors. Is there another approach we're missing? Maybe a |
@amacleay, an alternative could be to always call |
React Apollo has been refactored to use React Hooks behind the scenes for everything, which means a lot has changed since this issue was opened (and a lot of outstanding issues have been resolved). We'll close this issue off, but please let us know if you're still encountering this problem using React Apollo >= 3.1.0. Thanks! |
@hwillson this problem still persists with version >= 3.1.0 To recap with an example: const result = useQuery<TData, TVariables>(query, {
fetchPolicy: 'cache-and-network',
errorPolicy: 'all',
...options,
onError: error => {
const { graphQLErrors } = error;
const isUnauthenticated = graphQLErrors.find(
error => !!error.extensions && error.extensions.code === 'UNAUTHENTICATED'
);
if (isUnauthenticated) {
navigation.navigate('AuthLoading');
}
},
}); With an I suppose for now I can do some sort of workaround like: const result = useQuery<TData, TVariables>(query, {
fetchPolicy: 'cache-and-network',
errorPolicy: 'all',
...options,
});
const { error } = result;
React.useEffect(() => {
const { graphQLErrors } = error;
const isUnauthenticated = graphQLErrors.find(
error => !!error.extensions && error.extensions.code === 'UNAUTHENTICATED'
);
if (isUnauthenticated) {
navigation.navigate('AuthLoading');
}
},
}, [error]) but the documentation surrounding this is confusing |
Just following up on this: is @audiolion's workaround really the best way to handle errors with Apollo right now? Would really appreciate any helpful resources around error handling as it is still one of the most ambiguous parts of GraphQL. |
See https://github.com/apollographql/apollo-client/blob/905001e40cfadf841aa5a78aa428ab09f8cdf108/packages/apollo-client/src/core/ObservableQuery.ts#L215.
If fetchPolicy is set to
all
, then theerrors
property is set instead of theerror
property. This should be accounted for below:react-apollo/src/Query.tsx
Line 241 in 1e0bdf7
The text was updated successfully, but these errors were encountered: