Query Functionality with `onCompleted` is broken in v3.0.0 release (CodeSandbox repro)
See original GitHub issueSome core lazy querying functionality through useLazyQuery
seems a bit off.
Specifically, when a lazy query (through a useLazyQuery
hook) returns an error, then Apollo seems to start endlessly querying the server. The same thing happens if you give it a fetchPolicy
of network-only
(or anything that doesn’t search the cache first).
Related: https://github.com/apollographql/react-apollo/issues/4044
At the same time, if the lazy query function (returned by useLazyQuery
) gets called more than 1 times, then no callbacks are fired. The onCompleted
doesn’t fire again. The onCompleted
fires on the 1st successful run of the “query” function and then consecutive invocations of the lazy query function, don’t trigger onCompleted
callback (they should, since you can’t have conditional business logic based on whether the data is cached or not!)
Related: https://github.com/apollographql/react-apollo/issues/4000
Funnily enough, using client.query()
works fine. All this used to work perfectly during the beta (I think I’ve verified that something occurred after @apollo/client@v3.0.0-beta.43
), but now it doesn’t.
UPDATE: I’ve added a codesandbox that reproduces it
Intended outcome:
- Lazy query errors shouldn’t trigger endless API querying
- A
network-only
fetch policy shouldn’t trigger endless API querying on a lazy query onCompleted
should fire every time a lazy query gets successfully executed
Actual outcome:
- Lazy query errors &
network-only
fetch policy start an endless loop of requests onCompeted
gets fired at most once regardless of the number of invocations
How to reproduce the issue:
- Take a lazy query and change the
fetchPolicy
tonetwork-only
or force an API error on a lazy query - Click a button that triggers a lazy query multiple time, while observing the number of invocations of the
onCompleted
callback.
Versions System: OS: macOS 10.15.2 Binaries: Node: 12.18.0 - /usr/local/bin/node Yarn: 1.19.0 - /usr/local/bin/yarn npm: 6.14.4 - /usr/local/bin/npm Browsers: Chrome: 83.0.4103.116 Firefox: 72.0.2 Safari: 13.0.4 npmPackages: @apollo/client: ^3.0.2 => 3.0.2 apollo-link-error: ^1.1.12 => 1.1.13
Issue Analytics
- State:
- Created 3 years ago
- Reactions:37
- Comments:52 (8 by maintainers)
Top GitHub Comments
@benjamn and @hwillson Thanks for tackling this.
Current State of things
As of the moment of writing, in
3.1.1
:cache-first
query withonCompleted
and/oronError
no longer re-renders endlessly 🎉network-only
query no longer pings the server endlessly 🎉Pending bugs
onCompleted
callback should always fire whenever a query is resolved (either from network or cache). https://github.com/apollographql/react-apollo/issues/4000Reproduction of pending bugs
You can observe both these bugs in the following codesandbox --> https://codesandbox.io/s/apollo-reference-equality-op12z?file=/src/App.js
To reproduce the
onCompleted
error: ChangefetchPolicy
tocache-first
, click “Run Query” multiple times and see howonCompleted
only fires onceTo reproduce the
variables
withuseLazyQuery
error: Click “Run Query” once and then start clicking “Change foo”. Clicking “change foo” should not cause a query to runAdditional Context
Out of the two bugs listed above (in the order that they are mentioned):
Thanks again!
This issue was fixed in https://github.com/apollographql/apollo-client/pull/6588, and will be coming in
@apollo/client@3.1.0
very soon.