Allow cache eviction by predicate
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
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.