fetchMore and fetchPolicy: cache-and-network does not work together
See original GitHub issueIntended outcome:
In case of fetchMore
additional request should not be executed or should use last variables (passed to fetchMore
)
Actual outcome: The request executes with wrong variables
How to reproduce the issue:
function MyComponent() {
const q = useQuery(query, { variables: { offset: 0 }, fetchPolicy: 'cache-and-network' });
...
<button id="button" onClick={() => q.fetchMore({ variables: { offset: 10 } })}>Click me</button>
}
That’s what is happening:
<MyComponent />
is rendered => request to the server is executed with{ offset: 0 }
- Button
#button
clicked => request to the server is executed with{ offset: 10 }
=> request to the server is executed with{ offset: 0 }
In other words the request, which fetches fresh data from the server uses variables passed to the hook, but to to fetchMore
Versions
System:
OS: Windows 10 10.0.19041
Binaries:
Node: 12.18.0
Yarn: 1.22.4
npm: 6.14.4
Browsers:
Chrome: 84.0.4147.125
npmPackages:
@apollo/client: ~3.1.2 => 3.1.3
apollo: ~2.30.2 => 2.30.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:33
- Comments:31 (5 by maintainers)
Top Results From Across the Web
Using fetchPolicy='cache-and-network' in useQuery in apollo ...
I am using the apollo-client for my graphql setup. I have a paginated call for which i use the fetchMore function returned by...
Read more >Interacting with cached data - Apollo GraphQL Docs
Since these queries are cached by both the initial query and their parameters, a problem arises when later retrieving or updating paginated queries...
Read more >Pagination - Client (React) - Apollo GraphQL Docs
In Apollo, the easiest way to do pagination is with a function called fetchMore , which is included in the result object returned...
Read more >subscribeToMore and fetchMore for the same querry using ...
I've been learning Graphql and started building fullstack app using Apollo. But I ran into a problem, where I have a query for...
Read more >Understanding Apollo Fetch Policies | by Galen Corey - Medium
This becomes especially noticeable when you are working with data ... If you do not set a fetch policy yourself, this is what...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
See also #6327 and #6833, especially this comment.
Looks like the proper policy combination in 2021 for paging+cache is
After thinking more about this, I agree with @Akryum the extra network fetch after
fetchMore
is not a reasonable consequence of usingnetwork-only
orcache-and-network
fetch policies.Yes, you may be able to work around the problem with
nextFetchPolicy: "cache-first"
, but I no longer think usingnextFetchPolicy
should be necessary in this case. As long as the cache read triggered byfetchMore
is complete (in thediff.complete
sense), I think we should deliver that cache result immediately, and skip the network. In other words, reacting to afetchMore
request is not one of the situations where we want/need to reapply the original query’sfetchPolicy
literally, so I think it should be safe to ignore thefetchPolicy
here, and just deliver the result.I’m still investigating the consequences of these changes, but I think/hope we can remove the
this.reobserve()
from thefinally
afterfetchMore
, and change theQueryInfo
code as follows:The
observe
method ofObservableQuery
delivers the current result only, whereasreobserve
reapplies thefetchPolicy
, potentially delivering multiple results.I hope to have more answers soon. Thanks to @akryum for sharing your findings!