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.

Skip in React useQuery is not respected and onCompleted is behaving erratically in response

See original GitHub issue

Intended outcome:

I’m trying to use skip in useQuery as a way to avoid getting errors from the GraphQL for variables that are already known to be wrong. If I understood the docs correctly, network requests should only be made if skip === true and onCompleted should only be called after a successful network request.

Actual outcome:

Even when skip is set to true, onCompleted seems to get called and the network panel of the browser dev tools show that there has indeed been a request. Also, if skip is true at the start, onCompleted still is called once althought at this time there is no network activity.

How to reproduce the issue:

I have created a short demo repository here. It is an app that lists all continents using https://countries.trevorblades.com/ as a list of buttons. A click on the a button triggers the retrieval of this specific continent in a separate component. A continent with an invalid code (undefined) has been manually added to this list. The Continent component accepts either a string or undefined as continent code. This is for demonstration purposes, in this particular case rendering of the Continent component could otherwise be skipped altogether in case the code is a known invalid value.

After the demo has been loaded (best with Open In New Window to be able to see the network tab), please notice that onCompleted has already been called once even though no continent has been selected yet. To demonstrate, I suggest the following clicks:

  1. Button Asia: everything works as expected, AS is added to the list of codes by onCompleted
  2. Button North America: all ok, NA is added
  3. Button Europe: Still ok
  4. Button [Code undefined]: Although skip is now true, a network request has been made and onCompleted has been called. The result of the network request was 400 - Bad request but onError has not been called
  5. Button Europe: The query is called and the continent is set to Europe but onCompleted doesn’t seem to have been called
  6. Button North America: NA is added to the list, the selected continent is correct, everything is OK again until [Code undefined] is pressed again, then it goes back to the behavior described after Point 4

Versions

@apollo/client: 3.0.0-RC.9
graphql: 15.1.0
react: 16.13.1
react-scripts: 3.4.1

I hope this reproduction is OK, I’m not sure how to better describe the problem. Kind regards, Daniel

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

2reactions
yngwicommented, Jul 15, 2020

As far as I can tell fixed in @apollo/client: 3.0.1

0reactions
yngwicommented, Jul 15, 2020

Still the same behavoiur with @apollo/client: 3.0.0 final.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries - Apollo GraphQL Docs
This article shows how to fetch GraphQL data in React with the useQuery hook ... If you set errorPolicy to all , useQuery...
Read more >
reactjs - Skip argument is being ignored in useQuery hook ...
Apollo Client has issues. The bug in skip is an annoying one. You can abandon GraphQL, or work around them. I recommend abandoning...
Read more >
Testing Apollo Components using react-testing-library
This means if we start requesting new data, our tests will fail even though our component behavior is unchanged. Not schema-driven. MockProvider ...
Read more >
onCompleted in useQuery not working : r/learnprogramming
So in my code for some reason the onCompleated in my useQuery react hook is not being triggered for some reason. I've looked...
Read more >
use-safe-query - npm
Implementation to adapt apollo's useQuery to use both errors and data ... and "data" in query response errorPolicy: "all", // skip so we...
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