useQuery fails to return cached data when variables are changed
See original GitHub issueConsider the following queries
useQuery(POSTS, {
variables: {
offset: currPage * 20
}
})
where currPage
is a React local state variable. It will get updated when user paginates
Intended outcome:
When currPage=1
, new data is fetched, when currPage=2
, new data is fetched… When user paginates to the previous page (page 1), because query with currPage=1
is already fetched, it should just read the cache
Actual outcome:
When user paginates from page 1 to page 2, new data for page 2 is fetched, however, when user paginates back to page 1, cache is not read, data is still displayed for page 2
How to reproduce the issue:
Versions
@apollo/client: ^3.0.0-beta.14
react-apollo: ^3.1.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:16
- Comments:18
Top Results From Across the Web
Apollo client useQuery fails to return cached data when ...
Consider the following queries useQuery(POSTS, { variables: { offset: currPage * 20 } }). where currPage is a React local state variable.
Read more >Interacting with cached data - Apollo GraphQL Docs
If your cache doesn't contain all of the data necessary to fulfill a specified query, readQuery throws an error. It never attempts to...
Read more >React Apollo: Understanding Fetch Policy with useQuery
A cache-only query throws an error if the cache does not contain data for all requested fields. cache-and-network, Apollo Client executes the ...
Read more >useQuery | TanStack Query Docs
When a query's cache becomes unused or inactive, that cache data will be ... the component will only re-render when the data or...
Read more >Tips and Tricks for working with Apollo Cache - Medium
That data is stored in Apollo's cache, based on the name of the query you execute, as well as any variables you pass...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
I am just surprised no one from Apollo team has responded to this issue. It seems to be a big bug to me 😦
@jsmircic I don’t think this is by design. First, it works on previous versions. Second, If a query with a given variable has already been fired, data should be cached so that when this query is fired again, no network request should be needed, data should be returned from cache.
fetchMore
is used for fetching more data (new variables). However, this issue is related to read cached data, not fetch more data