-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Option to invalidate document cache with a query #3223
Comments
We actually explicitly haven't implemented a mechanism for this and there's some older issues around this request. We do however want to encourage everyone to copy the default cache and modify it as needed, if they get to a point where it doesn't serve their use-case. This is similar to Apollo's I understand that there's a middle ground here and we've discussed in the past how to lower the migration cost of moving to Graphcache, but I still believe this isn't it. Basically, if your query is just "local" and doesn't affect the other queries, since they're only passively modified, then as you said the side-effect that causes queries to be considered stale comes from something else. If this is not a mutation but a subscription, I'd actually be more inclined to apply the same invalidation logic that mutations have to subscriptions optionally. In other words and TL;Dr:
I'd still enforce that mutations represent changes of data, and queries and subscriptions represent data that is (to some extent) unchanged. Does that make sense to you? ✌️ |
Yes, that would do it as well and is indeed a better option. Do you want me to open a PR for this? It's in fact just about adding I tried switching to Graphcache to get access to cache invalidation. In our case it would be a huge bandwidth saver if I could invalidate a single item in the list instead of the whole list as with document cache. This however proved challenging because the page is displaying paginated data in the form of relay's Connection. eg.
By Is it possible I was trying to update the cache wrong? Because it seems that every cursor-paginated list must be suffering from this issue. Is there maybe another way how to do cache updates for paginated data like this? Not to mention if you have multiple grid queries with different arguments - maybe it's ok to update cache results of 1 grid query by passing all needed arguments to subscription, but its impossible to know what other grid queries have been made. |
@hizo: I just submitted a small PR for this. Basically, I want to make sure that an in-code comment explains why it's limited to
You don't necessarily have to, no. You can use You can also re-fetch the entire query in Graphcache, which is what I was rather referring to, by using |
@kitten I tried For the above example However running
Let's say I am able to get arguments from here and run There is indeed 1 other fact I didn't mention. Before grid data is fetched, I first fetch grid configuration under the same field. So:
For some reasons we don't fetch data and config together as we have a custom utility to fetch data (via imperatively calling When I run Is this something to do with the commutativity as described in the docs ? Similarly, I was only able to invalidate with the above call, nothing else worked. I tried:
|
I obviously don't know anything about your utility, but if it's using |
That must be it yes, it's using |
However that actually doesn't solve the issue - I don't actually need the operation to refetch straight away, rather on a next page visit, that means the utility will trigger the queries again, but this time it will get results from |
Summary
I found useful invalidating document cache by providing
additionalTypenames
to mutation.I have a specific usecase, where I'd want to do the same with a query. From default cache exchange source it's clear that queries only "mark" the results with
additionalTypenames
.I currently have a working solution, by copying over the exchange's source and tweaking it and I'm interested in creating a PR to contribute back.
Why
graphcache
as document cache suits yourequestPolicy: 'network-first'
for the query you need to invalidate as that would require you to implement additional logic when to apply the policy (often complicated)Usecase:
On a specific event (subscription in my case) I imperatively trigger a query via
client.query
. This query should in turn invalidate some results in cache (list of items), since it fetches a fresh item from backend. As I use document cache, subscription doesn't invalidate anything in cache andurql
doesn't provide a way to access cache. The triggered query following a subscription callback is the only place I can invalidate.Proposed Solution
By providing an option in
OperationContext
, eg.I was able to invalidate cache while the request with the provided context option was passing through cache exchange.
The logic to invalidate cache is already present, so I just abstracted it into a function and added
Would you be interested in this?
The text was updated successfully, but these errors were encountered: