useQuery hook - errorPolicy 'all' - onError callback not called following version 3.4.17
See original GitHub issueIntended outcome:
When you define errorPolicy: 'all'
in a useQuery
hook and provide an onError
callback we expected this to be called when receiving a response with both data and Graphql errors. Please see the simple example below.
function Dog({ name, onError, onCompleted }) {
const { loading, error, data } = useQuery(GET_DOG_QUERY, {
variables: { name },
onCompleted,
onError,
errorPolicy: "all"
});
if (loading) return <p>Loading...</p>;
if (error) return <p>Error!</p>;
return (
<p>
{data.dog.name} is a {data.dog.breed}
</p>
);
}
Actual outcome:
The onError
hook is never called following version 3.4.17
.
How to reproduce the issue: You can replicate this problem by running the tests within the following code sandbox:
https://codesandbox.io/s/sweet-minsky-5qe67r?file=/src/index.test.js
Note the tests will pass when you change the version of @apollo/client
to 3.4.17
. The version on the code sandbox is 3.5.0
.
Versions
Issue appears to be present on all versions following 3.4.17
.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
onError and onCompleted callbacks do not work in useQuery ...
Intended outcome: onCompleted or onErorr callbacks should be called on refetch or polling in useQuery hook, according to the documentation ...
Read more >ApolloClient useQuery onError callback doesn't return errors
Might try setting the error policy to errorPolicy: 'all',.
Read more >Handling Errors with Apollo Client (React) - YouTube
In this video I talk about handling errors when running queries and mutations using Apollo Client in a React project.Table of contents:00:00 ...
Read more >React Query Tutorial - 11 - Success and Error Callbacks
Courses - https://learn.codevolution.dev/ Support UPI - https://support.codevolution.dev/ Support PayPal ...
Read more >@apollo/client | Yarn - Package Manager
A fully-featured caching GraphQL client. apollo, graphql, react, hooks. readme. Apollo Client. npm ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What is the intended behaviour following version
3.4.17
?From version
3.5.0
theonCompleted
callback is called and the error property returned from the hook also contains the graphql errors. After reading the documentation there appears to be some confusion here:Our response has partial errors so I’m assuming after reading the above that the
onCompleted
callback should not be called as we don’t have zero errors and our error policy is notignore
.When you compare this to the onError callback documentation below:
In our case given the above, I would expect the
onError
callback to always be called as we have errors and the errorPolicy is not ignore.I don’t believe there is anything wrong with my mock or implementation here. It just appears that the documentation is slightly confusing.
Can you confirm that following version
3.4.17
the onError callback will never be called when error policy is ‘all’?If the above is true, can we update the documentation to reflect all error policy scenarios?
Hi @bignimbus, thanks for the quick feedback. After looking into this further, it appears this was added as a bug between v3.4.4 and v3.4.5 (https://github.com/apollographql/apollo-client/compare/v3.4.4...v3.4.5), if you change the library version in the code sandbox you’ll see that my test starts passing from v3.4.5. I haven’t dived too much into the code but appears this was a regression which would cause the onError callback to be called until v3.5.0.
Nevermind, it appears that our application code is expecting this so when we update to v3.5.0 a few of our tests fail as the error is on the incorrect property which is causing our confusion. Sorry I’m surprised I didn’t notice this at the beginning. I’ll try and look into this further over the next few weeks as we would really like to upgrade. I’ll let you know if I run into anything else.
Thanks again for your teams help 🙇 I’ll close this ticket.