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.

cache.modify INVALIDATE doesn't result in query refetching data

See original GitHub issue

Intended outcome:

My motivation is to invalidate data in cache, so that queries can refetch invalidated data. (For that reason I just return _id from mutation. I know I can return data with mutation, but this scenario has implication on invalidating data on mutations with side effects.)

For that purpose I’m using cache.modify and INVALIDATE flag to invalidate data in cache based on docs.

My expectation is that invalidation will result in query refetching data from server because data is not valid any more.

I also tried evict and it works, but my impression is that invalidate mechanism should allow us to refetch data based on dirty status. Did I miss something in code or did I misunderstood it? Thanks!

Codesandbox

Actual outcome:

Query that fetches all todos is reobserved but it doesn’t refetch data.

I tried to debug it and it seems cache diff is non existent. After cache.modify invalidation, query is reobserved but data is resolved from cache instead refetched.

What I found is that INVALIDATE is translated into dirty status here, but Query when calculating diff doesn’t see any difference here

How to reproduce the issue:

Open Codesandbox and try to change status of todo item.

It doesn’t change.

Versions

System: OS: macOS 10.15.5 Binaries: Node: 14.2.0 - ~/.nvm/versions/node/v14.2.0/bin/node Yarn: 1.22.5 - ~/.yvm/versions/v1.22.5/bin/yarn npm: 6.14.4 - ~/.nvm/versions/node/v14.2.0/bin/npm Browsers: Chrome: 85.0.4183.121 Firefox: 75.0 Safari: 13.1.1 npmPackages: @apollo/client: 3.2.0 => 3.2.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
omermancommented, Mar 17, 2021

@benjamn I don’t understand how reobserveQuery is a solution to this problem… I mean, INVALIDATE indicator should refetch, this is why we all came to this thread, it is intuitive that it would work that way, it is expected to trigger a refetch, no matter how the fetch policy is defined. I think I speak for everyone here when I say that we need a way to mark a query as “needs to refetch next time it is observed”, outside react & hooks context, using purely the Apollo client.

13reactions
tafelitocommented, Nov 7, 2020

@benjamn what about queries that are not currently active?

I mean, I have 2 pages, in page 1 I have a list of items, in page 2 I have a function where I can query some data or do something and I need to invalidate data from page 1. Then when I navigate to page 1, I want to show existing data but since cache data is invalidated, I want to refetch data form the server. The fetch policy in page 1 query is cache first

The way I see INVALIDATE or how I would like to use it in this case is whenever I go to page 1, if data is valid and fetchPolicy is cache-first, then no network request is made, but if somewhere if my app I invalidate the data for a particular fieldName, next time, even with cache-first, a network request should be made and cached (invalidated) data should be returned before the request is made.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refetching queries in Apollo Client - Apollo GraphQL Docs
Local cache updates and refetching work especially well in combination: your ... cache data is to use cache.modify and its INVALIDATE sentinel object:....
Read more >
How to invalidate cached data in Apollo and handle updating ...
This means that in our update functions, we can simply loop through our cache and delete all entries where the key starts with...
Read more >
Cache invalidation; refetch practices when nothing says the ...
open channel ( /soccer ), for the first time, data is not present in cache, query is executed and result stored in cache;...
Read more >
Query Invalidation | TanStack Query Docs
Waiting for queries to become stale before they are fetched again doesn't ... use normalized caches would attempt to update local queries with...
Read more >
Advanced Topics on Caching – Angular
The no-cache policy does not write to the cache with the response. ... be used to update the result of a query based...
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