apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Allow cache eviction by predicate

Open Vincz opened this issue 5 years ago • 3 comments

Coming from here : https://github.com/apollographql/apollo-client/issues/6098#issuecomment-656863597

It would be great to be able to evict the cache using a predicate.

In the evict method, we could use a callback to check if a query should be clear or not :

For example:

cache.evict("ROOT_QUERY", "totalBalance", (variables, data) => {
    return variables.currency === 'USD' or variables.currency === 'EUR';
});

So, it would remove the cache where the currency is USD or EUR but not for CAD. This way, we could handle any specific use case and avoid unnecessary fetch.

ping @benjamn

Vincz avatar Jul 12 '20 09:07 Vincz

Ping @benjamn Hey :-) any news about this? I can try to look into it this week if you don't have time (but I'll be less efficient than you for sure)

Vincz avatar Aug 16 '20 10:08 Vincz

If this gets implemented can a similar args predicate API please be added to cache.modify. We have a scenario where items need to be removed from a list but only for a particular filter state. Thanks!

FinestV avatar Sep 03 '20 20:09 FinestV

I've been using something like the following to do this

        cache.modify({
          fields: {
            totalBalance: (existingTotalBalance, { fieldName, storeFieldName, DELETE }) => {
              const args = JSON.parse(storeFieldName.replace(`${fieldName}:`, ''))
              if (args.currency === "USD") {
                return DELETE
              }
              return existingTotalBalance
            }
          })

I would be great if there was a supported way of doing this or of accessing the args.

anark avatar Sep 18 '20 16:09 anark