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.

fetchPolicy "cache-and-network" forced to refetch after unrelated query finishes

See original GitHub issue

The application initializes:

  • Subscribes to a watchQuery() with fetchPolicy “cache-and-network” and waits for a response
  • Creates other unrelated plain query() and quickly receives the response

Intended outcome: I would expect apollo client to process the response of query() and keep waiting for the first watchQuery result.

Actual outcome: The plain query sends a notification that triggers a reobserve in the watchQuery, which makes a refetch.

Step by step

Plain query finishes, calls stopQuery() https://github.com/apollographql/apollo-client/blob/bd778d17411863bcefbb3302befe8ae6c3276e44/src/core/QueryManager.ts#L494

Which makes a broadcast to all queries https://github.com/apollographql/apollo-client/blob/bd778d17411863bcefbb3302befe8ae6c3276e44/src/core/QueryManager.ts#L653

watchQuery thinks it should be notified in shouldNotify https://github.com/apollographql/apollo-client/blob/bd778d17411863bcefbb3302befe8ae6c3276e44/src/core/QueryInfo.ts#L209-L214

Previously, watchQuery created a listener and it’s now called. https://github.com/apollographql/apollo-client/blob/bd778d17411863bcefbb3302befe8ae6c3276e44/src/core/QueryInfo.ts#L183

reobserve() then makes watchQuery to refetch again

Versions 3.2.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
joshjgcommented, Oct 14, 2020

Here is a repro The bug is introduced somewhere between these two versions: https://github.com/apollographql/apollo-client/compare/v3.1.0-pre.3...v3.1.0-pre.4

Of those commits only this one looks relevant: https://github.com/apollographql/apollo-client/commit/1cd1df9b6aa3c70eed6ddc378451c7f9cc79514a

Sure enough, adding nextFetchPolicy: 'cache-first' to my query solves the issue and seems like a reasonable workaround.

3reactions
TomerKidronCTRLcommented, Oct 20, 2022

This bug still occurs on 3.7.0, is it going to be fixed soon?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced topics on caching in Apollo Client
To reset the cache without refetching active queries, use client. ... This can be useful if you want to force your UI to...
Read more >
Prevent an Unnecessary Refetch with nextFetchPolicy and ...
I recently figured out the cause of a strange issue due to a change in how the cache-and-network fetch policy works in Apollo...
Read more >
zino-app - Bountysource
Notice that the code does not throw an exception and never finishes. ... cacheAndNetwork, Call a graphql Query After that change the fetch...
Read more >
you are given q queries and in each query
For example, you can use this to switch back to a cache-first fetch policy after using cache-and-network or network-only for a single ...
Read more >
How to force browsers to reload cached CSS and JS files?
However, there is some discussion as to whether or not the browser would cache a file with a query string. (Remember, we want...
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