[3.0 Beta 41] Evict API does not support eviction based on arguments
See original GitHub issueHello! I am playing around with the 3.0 eviction API. I see that we can evict by dataId
and fieldName
. If I make two queries for a user’s total balance for their currencies, they get stored in the cache under the ROOT_QUERY like this:
totalBalance({ currency: 'USD' })
totalBalance({ currency: 'CAD' })
If they they added a new USD account, I want to invalidate their USD total balance by doing:
cache.evict('ROOT_QUERY', 'totalBalance({ currency: "USD" })')
But that isn’t a valid field name, instead I need to do:
cache.evict('ROOT_QUERY', 'totalBalance')
But that invalidates both my cached queries. I would think that there should be a way to invalidate queries based on their arguments, since in some applications there may be many of the same query type made with different filters etc and that users can perform actions to invalidate subsets of these.
The issue is here: https://github.com/apollographql/apollo-client/blob/master/src/cache/inmemory/entityStore.ts#L130
it checks for modifiers based on the fieldName
not the storeFieldName
. It would be great if it could first check modifiers for the storeFieldName too, and have that modifier take precedence over the fieldName modifier. Thoughts?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:7 (4 by maintainers)
Top GitHub Comments
Are you comfortable with having to pass exactly the same arguments that were originally used to store the field in the cache? The only reason I ask: I don’t think we can reasonably match subsets of fields if you pass some but not all of the original arguments.
Of course, passing only an ID and a field name will continue to evict all variations of the field, regardless of arguments.
If that feels right, then I think we could extend the API with another argument:
If I recall correctly, the reason I didn’t add arguments to the
cache.evict
API originally was that theEntityCache
did not have easy access to thePolicies
object, but that changed with commit 9dcc33c9c366412b9ac50ed0c0b373a88145a32b.Since we now support evicting by specific arguments (see #6141 and #6364), I think this issue can be closed. Still, I like your idea of an eviction predicate function @Vincz. Can you open a separate feature request for that? Feel free to refer back to this discussion for background/context.