-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Is there a way to delete cache query? #6795
Comments
Data in the cache represents the accumulated, often overlapping contributions of multiple queries, so it doesn't make sense to delete a query from the cache, because you might be deleting data that other queries are depending on. However, you can evict objects by their IDs with |
This blog might help, see the Eviction API. It has a good example unlike the Apollo Docs 🙄 |
We had a lot of queries we were running & got curious about how we could clean up better after ourselves & make debugging easier. This ticket shows up right away when we went to search. @benjamn started with a good hint, & the blog @abhagsain documents this quite well! It was still a bit to digest but good. It comes down to evicting a field from the root query object, like so:
For what it's worth, our team would love to have an easier way to do this. We are big fans of the React Hooks. Perhaps useQuery results could return an |
@benjamn @abhagsain @rektide I've read @danReynolds article. Trying the same things to evict my countries query result. Couldn't remove it from the cache. You can seet it here sandbox What am I missing? |
Hey @killjoy2013 You're using a newer version of AC (3.2.5). I downgraded to AC 3.0.0 and eviction working fine (IDK Why evict isn't working in the newer version need to see the changelogs) but for some reason Refetching again returns no data from the server. No Clue :\ Checkout this sandbox |
I gave it a quick look and the eviction does work, if you put a debugger after the hope that makes sense! Happy to chat more. |
Hey @abhagsain, tried your sandbox. It clears the cache nicely. However, hitting query button again don't get the results again. Can you please give it a try? |
Hey @danReynolds, thank you for the explaination. Tried your suggestion |
@danReynolds @benjamn @abhagsain I removed the Of course this is not as elegant as using |
Oh if your goal is to evict without causing a network request then you can use the lazy query and a fetch policy of 'standby' which will get the data but never update. You can also accomplish this by using a network-only policy for first render and then switch to cache-only after you get data |
Actually, in my real use case, countries are selectable as well. To do this, I'm using type policies and reactive variables sandbox. For this senario, useLazyQuery hook should be running the way it already is. Tweaking useLazyQuery can cause other issues and a simple senario can become unnecessarily complicated. Instead, if we can somehow make |
@danReynolds There is a broadcast parameter in cache.evict ! using like this completely solved it;
|
I am developing a offline first app and therefore I need to constantly read/write queries offline and cleanup my cache. I basically have to run the all logic on the front end. The mutation barely return anything (some timestamp for conflict resolution). That being said I am struggling to have an easy way to remove unreachable and dangling reference. Here is how I do it right now. Let's take this use case I have:
What I will end up doing is:
I have basically
You can see that I need three step in order to clean up everything. Since the app is offline first I really need to clean up my mess. So yeah this was more of a feedback than trying to find a solution. At the moment I have not found an easier way to deal with my local state. Also I do not like dealing with the cache directly, I would rather only use the client and its abstraction. Using client.cache.evict feels a bit embarassing to me. Especially since I do not have the types with the cache methods (I do with the client and graphql-gen) What I would like to eventually have is one easy way from apollo to says
which basically contains all of what I am doing manually right now. |
Hey @danReynolds, we need to set the fetch-policy of a query hook while we create it. Is it possible to change the fetch-policy later on? So as to be able to run the same query hook with different fetch-policies depending on the situation? |
added nextFetchPolicy: "no-cache" to useQuery can remove the query from cache with cache.evict. but the ui doesn't update |
@raymclee it's awesome! That's what I've been looking for. There's almost nothing about Thanks & regards |
If it helps I wrote https://github.com/NerdWalletOSS/apollo-invalidation-policies to make eviction across multiple queries somewhat more organized, let me know if there's a feature that would make it easier for you |
Is there a way to get |
@benjamn you said "Data in the cache represents the accumulated, often overlapping contributions of multiple queries, so it doesn't make sense to delete a query from the cache". I have the following scenario:
At this point, wouldn't you agree that it makes perfect sense to delete the query that fetches the individual item I just deleted? At the moment, when I try to update the cache, it triggers that query to run again and of course I get an error from the server because that item doesn't exist anymore. (Edit: I worked out that Apollo doesn't work correctly with I'm struggling to work out the 'correct' way to do this sort of thing (for this case and the case where I have items and sub-items). Is it It would be amazing to get one more example in the docs, next to https://www.apollographql.com/docs/react/caching/cache-interaction/#example-removing-an-item-from-a-list, that showed this surely pretty-common requirement. |
…or cache.evict(options) Tried this without looking at which options were required/not required and realized that just providing a fieldName is sufficient when i want to evict all cached entries for a specific gql query. I also found that @benjamn mentioned the same solution/implied that `id` is optional in apollographql#6795 (comment)
Hey @davidgilbertson did you ever figure out a clean way to do this? My team is also running into this issue and haven't found any nice ways to solve this issue. |
@davidmillercode I don't quite recall what I did to resolve this (I've moved on from the project), sorry. |
…or cache.evict(options) Tried this without looking at which options were required/not required and realized that just providing a fieldName is sufficient when i want to evict all cached entries for a specific gql query. I also found that @benjamn mentioned the same solution/implied that `id` is optional in #6795 (comment)
We were able to utilize the |
The docs have been updated for this issue in #8146, so it looks like this should be good to be closed! |
If I have cache
I read the articles that update cache using
readQuery
,writeQuery
But what I want to is, removing whole query itself. It like removing key:val from object.
How to delete whole cache query?
The text was updated successfully, but these errors were encountered: