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.

Query Functionality with `onCompleted` is broken in v3.0.0 release (CodeSandbox repro)

See original GitHub issue

Some 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 to network-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:closed
  • Created 3 years ago
  • Reactions:37
  • Comments:52 (8 by maintainers)

github_iconTop GitHub Comments

24reactions
3nvicommented, Jul 30, 2020

@benjamn and @hwillson Thanks for tackling this.

Current State of things

As of the moment of writing, in 3.1.1:

  • An errorred query no longer re-renders endlessly 🎉
  • A cache-first query with onCompleted and/or onError no longer re-renders endlessly 🎉
  • A network-only query no longer pings the server endlessly 🎉

Pending bugs

Currently only network-only requests have their onCompleted fired after every query execution. For cache-first ones, the onCompleted fires only once. Unless this is documented somewhere, I would flag this as a bug since our onCompleted logic shouldn’t have to account for the origin of this data, i.e. It should be cache agnostic

This should definitely happen for useQuery hooks, but not for useLazyQuery hooks (since they wouldn’t be lazy anymore). Again if this is the expected functionality it should ideally be documented

Reproduction 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: Change fetchPolicy to cache-first, click “Run Query” multiple times and see how onCompleted only fires once

  • To reproduce the variables with useLazyQuery error: Click “Run Query” once and then start clicking “Change foo”. Clicking “change foo” should not cause a query to run

Additional Context

Out of the two bugs listed above (in the order that they are mentioned):

  • The first is within the scope of this ticket and a requirement in order to close it
  • The second isn’t necessarily related to this ticket (although mentioned) and I understand that it may require a more complex solution through a modification of your query observables. Thus, I’m willing to port (2) to another issue in order to help close the existing one

Thanks again!

13reactions
hwillsoncommented, Jul 22, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

apollo-client-error onCompleted v3 - CodeSandbox
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >
material-ui/core/CHANGELOG.md - UNPKG
- Many bug fixes based on people migrating from v3 to v4. 178, - Responsive font sizes (#14573) @n-batalha.
Read more >
server list query returned an error system.threading.tasks ...
apollographql/apollo-clientQuery Functionality with `onCompleted` is broken in v3.0.0 release (CodeSandbox repro)#6636. Created over 2 years ago.
Read more >
@apollo/client - Awesome JS
The client options ( variables , context , etc.) used for mutation calls are now available as the second argument to the onCompleted...
Read more >
Tutorials – Codrops
We take the total duration of the animation as 3. ... backHalf.length); } function createBoxElements() { for (let halfIdx = 0; halfIdx <...
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