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.

useQuery fails to return cached data when variables are changed

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Reactions:16
  • Comments:18

github_iconTop GitHub Comments

13reactions
zicodengcommented, Dec 10, 2019

I am just surprised no one from Apollo team has responded to this issue. It seems to be a big bug to me 😦

10reactions
zicodengcommented, Dec 11, 2019

@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

Read more comments on GitHub >

github_iconTop 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 >

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