-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
RTK Query: Is there a way to find out cached queries parameters related to specific tag/id to help manual updating the cache?? #2988
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
Comments
We really need to add redux-toolkit/packages/toolkit/src/query/core/buildSelectors.ts Lines 200 to 207 in f7a8282
If you have the time and energy, a PR adding this to our documentation would highly be appreciated. |
Hi, can I help add this to the docs? |
Sure, that would be very welcome! You don't even need to check it out locally for the most part - just press the Once you're done, create a PR from your fork :) |
Hey @phryneas, is there a recommended way to make RTK Query maintain a normalized cache? For example, if I have 2 queries for Is there a way to make it store them only one, in a cache where the key would be comprised of the items' Tags and ids? |
@dutzi RTK Query is per design not a normalized cache, it's a document cache. |
Your objective is really hard @henkkasoft. According to my experience, even if you get the cached queries to be updated using Let me give an example. For instance, you've disabled a user in a mutation (you toggled user.isDisabled in the database). Think about you made several user queries before, including the filter key isDisabled or not with different filter keys and also with several sorting parameters. According to your mutation (user.isDisabled is toggled), this user may needed to be inserted into a cached query which does not include that user before. If there is also some sorting parameters, the user may needed to be inserted in a right place according to sorting params. Or, this user may needed to be deleted from a cached query since the query param may consist isDisabled field as true or false, etc. Or, this user may needed to be only updated but if there is a sorting param affected, you should change the order of the user record in the cached query etc. I don't even want to mention pagination, makes it even more complex. Given example is for only mutating one field. Think about more than one fields are updated in the mutation. The situation goes more and more complex. So, the updating the related cached queries manually is hard, and error-prone. The only reliable way seems to provide But, I had a feature request from the authors, saying that, while providing the |
The docs were updated, so I'm closing this. If there are further feature requests, please file a new issue! |
I have understood that if you want to manually update cache you can do it with updateQueryData. You need the API endpoint parameters used in the query to update this specific response.
There are two problems I am facing here.
When I update some item with useUpdateItemMutation, it gives the updated item as response. Now if I want to update the cache manually instead of invalidating the id to prevent unnecessary API calls it is hard to perform.
Whereas invalidating id can be done by giving the tag and id and all the cached queries related to that are handled.
My application has list of items (as responses for searches with different kind of query parameters) and the same item can be in many of them. When I am performing the update I would need to make sure that the item is updated to all lists where it is.
I don't know if this is possible without invalidating?
I understand that RTK Query is not normalized and therefore I cannot update the tag/id directly but
could it be possible to add a way to get all cached API calls (including used parameters) related to spesific tag/id?
Then I could manually update the caches everywhere when needed.
This issue is related to this #1529 (comment)
The text was updated successfully, but these errors were encountered: