New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query with errorPolicy "all" cannot use onError, onCompleted effectively #6966
Comments
I'm currently having some troubles when setting I'm using |
It's not perfect but you can use |
Thanks @jwm0 ! Your suggestion is an adequate workaround. I do not love though that this workaround takes an imperative approach to a defect in the declarative But your solution is better than what we currently have as workaround! Thank you ❤️ |
I am testing with I can't find the official explanation on the website, when I found this PR #6492 and it looks like it was not expected on trigger Relevant issue for me in vue-apollo https://github.com/vuejs/vue-apollo/pull/1225/files#r679619257 |
I faced the same issue with I created an ErrorLink like this: private createErrorLink = () => {
return onError(
({ operation, response, forward, graphQLErrors, networkError }) => {
return forward(operation).map((response) => {
if (response?.data) {
response.data.errors = graphQLErrors;
}
return response;
});
}
);
}; Now inside my onCompleted callback I get my data as well as errors. You will have to tweak Apollo types a bit, because it does not have errors field by default. const [getAnalyses] = useLazyQuery<
GetAnalysesQuery & { errors: GraphQLError[] }
>(GetAnalysesDocument, {
variables: { environmentContexts },
client: quicksightApolloClient,
fetchPolicy: "no-cache",
onCompleted: ({ getAnalyses, errors }) => {
console.log("errors from BE", errors);
// other logic
},
onError: (error) => {
// we get here only in case of NetworkError
}); |
Hi 👋 ! A team of us are working on transitioning a production application to Apollo with use of hooks in React. We have the following use case:
error
anddata
may both have value)onError
,onCompleted
attributes to perform some side effectsIntended outcome:
I'd expect to be able to perform "completed" and "errored" functionality if the errorPolicy="all", since this would be a congruent outcome of the
data, error, loading
pattern commonly used when bothdata
anderror
may be populated with this error policy.Actual outcome:
If there is a partial error from one of the fields, only
onError
called (since I've defined both).I think issue can be found in this either/or method (here in react-apollo, or here in apollographql/apollo-client).
I wonder: could
onError
receivedata
as a 2nd ordinal argument in the event that there is both anerror
anddata
?How to reproduce the issue:
I've reproduced here: https://codesandbox.io/s/inspiring-surf-pcrgj?file=/src/App.js
Here's an inline example:
Solution Proposal
Possible solution 1: provide
data
as a 2nd ordinal argument toonError
One might also argue additionally (or alternatively) that error in the
onCompleted
callback should be made available. Hence, in the case of errorPolicy="all" this dichotomy between "completed" and "errored" is somewhat difficult to discern. Hence:Possible Solution 2: Perhaps if developer does not define
onError
anderrorPolicy
is "all", thenonCompleted
is always called with bothdata
anderror
?Possible Solution 3: Alternatively, and more generally it should just always call "onCompleted" with access to both error and data when both are defined (errorPolicy="all"). And we remove the notion of onError entirely?**
In summary, there are many solutions to this perceived bug, and the API seems unclear for this use case. These three solution proposals above are ranked in order from least to most invasive.
Version
@apollo/react-components@3.1.5
(FYI originally reported here, moving)
apollographql/react-apollo#403
The text was updated successfully, but these errors were encountered: