Skip in React useQuery is not respected and onCompleted is behaving erratically in response
See original GitHub issueIntended 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:
- Button
Asia
: everything works as expected,AS
is added to the list of codes byonCompleted
- Button
North America
: all ok,NA
is added - Button
Europe
: Still ok - Button
[Code undefined]
: Althoughskip
is nowtrue
, a network request has been made andonCompleted
has been called. The result of the network request was400 - Bad request
butonError
has not been called - Button
Europe
: The query is called and the continent is set toEurope
butonCompleted
doesn’t seem to have been called - 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:
- Created 3 years ago
- Reactions:2
- Comments:6
As far as I can tell fixed in @apollo/client: 3.0.1
Still the same behavoiur with @apollo/client: 3.0.0 final.