question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useQuery hook - errorPolicy 'all' - onError callback not called following version 3.4.17

See original GitHub issue

Intended 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.

Screen Shot 2022-06-20 at 12 36 23 pm

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:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jwarykowskicommented, Jun 20, 2022

What is the intended behaviour following version 3.4.17?

From version 3.5.0 the onCompleted 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:

A callback function that’s called when your query successfully completes with zero errors (or if errorPolicy is ignore and partial data is returned).

This function is passed the query’s result data.

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 not ignore.

When you compare this to the onError callback documentation below:

A callback function that’s called when the query encounters one or more errors (unless errorPolicy is ignore).

This function is passed an ApolloError object that contains either a networkError object or a graphQLErrors array, depending on the error(s) that occurred.

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?

1reaction
jwarykowskicommented, Sep 18, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found