-
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
Updating cached data from multiple parameterized queries after a mutation #2991
Comments
It appears that From the docs...
This actually solves my current problem as I simply needed a way to re-run queries by name without needing to know what arguments were applied to the query, as they may differ throughout the application. If I have a query every time the query is run apollo will create a new key under
I was under the impression that the query could not be re-fetched without re-applying the arguments back to query in For those curious please see: apollo-client/src/core/QueryManager.ts Lines 1305 to 1322 in 3b5045d
and apollo-client/src/core/ObservableQuery.ts Line 207 in 3b5045d
|
Confirmed that this worked. Might be worth highlighting that the query name comes from the operation name. I missed that in steve-a-jones's example, and it tripped me up for a few minutes. So:
|
The solution with Is there any way round it? |
Also what happen if you and to update/refetch in ALL the variables not just the last variables applied |
A couple of peeps at Xero came up with a middleware-esque pattern to tackle this sort of concern: apollo-link-watched-mutation. |
Could we reopen this? I feel like the use case @snaumov mentions ("what if we are not to poll the data from a server, but instead write it to cache straight away") is still unsolved. I would be curious to hear from the Apollo team on this and know if they suggest using the Watched-Mutation pacakage or if there's another way? |
@SachaG I was able to solve the mentioned problem of not knowing which arguments to use for a cached query here: |
Good to know, thanks! In the end I decided to go with watched-mutation because my |
this was what came to my mind too, but I can't believe that this is the only way… like storing all the previous filters and updating them accordingly doesn't sound like an optimal solution to me, too many cases to handle. In my case there was a status change of an item from pending to failed, which in UI should be updated by filter with 3 statuses (in pending should be removed, failed: should be added, any: status should be updated) Should I just go with refetchQueries and that's it or do you have any ideas for this case? |
@snaumov Thanks a lot for that point! Worked for me. |
Perhaps this will be helpful. In my case, I have a typeahead which searches the backend and takes about 5-6 additional variables in addition to the keyword. I am not using watch queries (although this method also works with them). I needed a way to evict a whole bunch of queries from the cache based upon only a subset of the variables. For example I have the following queries which are cached (grossly simplified).
At some point the user adds a new I created a helper function (for Apollo Client 3) to accomplish this, as well as one for Apollo 2 that uses internal APIs. This function is passed to the Also, please note, I removed some additional code here for simplicity and this might break when passed invalid parameters, etcs. Usage:
If you are on Apollo2, you don't have cache.evict but you do something similar like this. Not this is not like RefetchQueries where is uses the client query name, this looks at the actual cache key from apollo which will be the query name:
|
I've created a package in an attempt to solve these issues and make complex cache updates as painless as possible. https://www.npmjs.com/package/apollo-augmented-hooks See https://github.com/appmotion/apollo-augmented-hooks/#--modifiers and the linked caching guide in particular. I've been using this in production for a while now and just made it open source so other people can benefit from it as well. |
Appears that the 2 related issues apollographql/react-apollo#708 and #1697 were closed without an efficient solution - so re-opening here as I am having the same issue.
The crux of the problem is this:
update
is non-trivial given that all co-located and parameterized queries that were affected by the create/delete mutation need to be re-run.update
call site.Interested to hear thoughts from the apollo team on an update pattern that fits here.
Thanks!
The text was updated successfully, but these errors were encountered: