Apollo-feature-requests: Allow cache eviction by predicate

Created on 12 Jul 2020  路  3Comments  路  Source: apollographql/apollo-feature-requests

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

Most helpful comment

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.

All 3 comments

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)

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!

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.

Was this page helpful?
0 / 5 - 0 ratings