Invalidating cache for one specific query

Hey everyone!

I thought that my problem can be solved super quickly, yet I had to acknowledge that it’s much more complicated (or just I haven’t figured it out yet).
I have a query (getExpensiveCalculation), which I fetch from the server for the very first time, and then I use the cached version of it to serve. Still, there are some operations that require refetching it, so after those I want to invalidate the cached version of it. Basically doing something like refetchQueries, but without immediately refetching, only if it’s requested.

The best I could do was to retrieve the associated objects and evict those, but that doesn’t solve my problem. I would want to remove the entire query from the cache, as if it had never been fetched. What’s the best way to tackle this? What did I overlook?

EDIT: I basically want to call cache.reset(), but I only want to remove one particular query (getExpensiveCalculation), and not everything.

1 Like

You can evict root query fields like this (omit the id property):
cache.evict({ fieldName: 'totalBalance', args: { currency: 'USD' } })
Does that help? When you say “I have a query (getExpensiveCalculation)”, do you mean that getExpensiveCalculation is the Operation Name or the Root Query Field?
https://github.com/apollographql/apollo-client/pull/6364
https://github.com/apollographql/apollo-client/issues/6098#issuecomment-608166111

2 Likes

The getExpensiveCalculation was the operation name.

On the other hand, huge thanks for your help, it totally helped!

1 Like