fetchPolicy "cache-and-network" forced to refetch after unrelated query finishes
See original GitHub issueThe 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:
- Created 3 years ago
- Reactions:10
- Comments:8 (5 by maintainers)
Top GitHub Comments
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.This bug still occurs on 3.7.0, is it going to be fixed soon?