Mutation /w `update` option triggers unexpected refetch of indirectly related "cache-and-network" queries
See original GitHub issueIntended 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:
- Create or find an AC app where a mutation response includes updates to a query shown on-screen
- Change the fetch policy for the related query to
cache-and-network
- Start the app, open it, open the network tab in Chrome devtools
- Trigger the mutation
- Observe that the related query is refetched after the mutation finished
Versions @apollo/client v3.3.13
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
@zenflow A
FetchPolicy
likecache-and-network
gets consistently (re)applied each time the cache is updated in a way that affects the query in question, so withcache-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 thenextFetchPolicy
option, so you can sequence your fetch policies in a more flexible way: https://github.com/apollographql/apollo-client/issues/6760#issuecomment-668188727If you use
fetchPolicy: "cache-and-network"
plusnextFetchPolicy: "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?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” :
What seems to be the current behavior :
What is not clear to me :
Thank you!