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.

Mutation /w `update` option triggers unexpected refetch of indirectly related "cache-and-network" queries

See original GitHub issue

Intended outcome:

I have an app with Apollo Client which displays a list of “posts” on which users can “vote” (upvote/downvote). The result of the vote mutation includes updates to post fields (vote_total & my_vote_value) which get merged into the normalized cache, so the update is reflected in the list of posts. This works great, since no extra requests are used to update the list of posts after the user “votes”. I can even use the optimisticResponse option to optimize UX.

The problem is when I change the fetchPolicy for the posts query from default (cache-first) to cache-and-network. I would expect this to cause the query to refetch after the query is redisplayed (which it does) but not any other random times.

Actual outcome:

After changing the fetchPolicy for the posts query to cache-and-network. the query is always refetched after a vote mutation.

Note that this happens even with the mutation’s refetchQueries option set explicitly to [].

How to reproduce the issue:

  1. Create or find an AC app where a mutation response includes updates to a query shown on-screen
  2. Change the fetch policy for the related query to cache-and-network
  3. Start the app, open it, open the network tab in Chrome devtools
  4. Trigger the mutation
  5. Observe that the related query is refetched after the mutation finished

Versions @apollo/client v3.3.13

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
benjamncommented, Apr 2, 2021

@zenflow A FetchPolicy like cache-and-network gets consistently (re)applied each time the cache is updated in a way that affects the query in question, so with cache-and-network you get a cache result and a network result each time, per the policy.

I understand this is probably not the behavior you want, but rather than introducing ad-hoc new fetch policies like cache-and-network-then-cache-first, we introduced the nextFetchPolicy option, so you can sequence your fetch policies in a more flexible way: https://github.com/apollographql/apollo-client/issues/6760#issuecomment-668188727

If you use fetchPolicy: "cache-and-network" plus nextFetchPolicy: "cache-first", your initial queries will receive both cache results and network results, but subsequent updates to the cache will not trigger a network request unless the cache update removed data relevant to the query, causing an incomplete cache read.

If you never want to make another network request, you can specify nextFetchPolicy: "cache-only". This leaves open the possibility of manually refetching the query, but it prevents any automatic/unexpected network requests.

I imagine one of those two nextFetchPolicy options is closer to what you want?

2reactions
eltonio450commented, Apr 20, 2021

Hello everyone,

Thanks for this

I feel the doc is not clear enough on this point.

What I understood so far of “cache-and-network” :

  • given a query A, if enough data is present in the cache, a network call is made while data is served to the component A (I use React). When the updated data comes back from the server, the cache is updated, and subsequently the component
  • if a concurrent query (B) updates the data of the cache used by query A, the data served to the component A is updated but no network call is triggered (I think it was working like this with Apollo Client 2)

What seems to be the current behavior :

  • given a query A, if enough data is present in the cache, a network call is made while data is served to the component (I use React). When the updated data comes back from the server, the cache is updated, and subsequently the component
  • if a concurrent query (B) updates the data of the first cache, the data served to the component is updated, and as the data has been updated, a network call is triggered for query A

What is not clear to me :

  • after the cache update by query B, if fetchPolicy = “ache-and-network”, is the network call triggered all the time or only if the data changed?
  • if query A is mounted a second time, will it use the fetchPolicy or the nextFetchPolicy?
  • in our case, query A triggers a refetch from query B, that triggers a refetch from query A, etc. Does it mean that we have a field that changes all the time?

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cache-and-network causes refetching from the server if a ...
I have a form that updates some of the data by using a useMutation but doesn't define a update option, meaning the cache...
Read more >
Prevent an Unnecessary Refetch with nextFetchPolicy and ...
These steps are helpful if you're seeing an unexpected refetch of a query after an update when using Apollo Client with cache-and-network ....
Read more >
Effective React Query Keys | TkDodo's blog
Learn how to structure React Query Keys effectively as your App grows.
Read more >
Apollo client refetch - OCentrum.pl
fullData: Raw data returned by the query (not transformed by the update prop) result.loading: Boolean indicating that a request is in flight (you...
Read more >
How to update the Apollo Client's cache after a mutation
Basically, the Apollo Client automatically inspects the queries and mutations' traffic on your behalf and uses the latest version of the data it ......
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