-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
docs: selectInvalidedBy for multiple cache updates across queries
#5226
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
base: master
Are you sure you want to change the base?
Changes from all commits
c0d0292
b09e967
e61d33d
905050e
460d080
c968813
60eac6e
1a9d2ee
c7f5662
11481f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -224,6 +224,104 @@ const api = createApi({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ### Updates Across Queries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The entity you want to update might be present in multiple differently shaped queries, such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (infinite) lists, or even inside another entity. If you don't want to invalidate those instead, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| you can avoid manually looping through every single cache entry by using [selectInvalidatedBy](https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils#selectinvalidatedby): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| you can avoid manually looping through every single cache entry by using [selectInvalidatedBy](https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils#selectinvalidatedby): | |
| you can avoid manually looping through every single cache entry by using [selectInvalidatedBy](../api/created-api/api-slice-utils.mdx#selectinvalidatedby): |
Copilot
AI
Apr 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing an error here will crash the app during onQueryStarted if any other query provides the same tag but isn’t explicitly handled. For a docs example, it’s safer to ignore/skip unknown endpoints (or handle them with a no-op/log) so adding a new tagged query doesn’t introduce a runtime failure.
| const patches = entries.map(entry => { | |
| switch (entry.endpointName) { | |
| case 'getPost': | |
| return dispatch( | |
| api.util.updateQueryData( | |
| entry.endpointName, | |
| entry.originalArgs, | |
| (draft) => { | |
| Object.assign(draft, patch) | |
| }, | |
| ), | |
| ) | |
| case 'getPosts': | |
| return dispatch( | |
| api.util.updateQueryData( | |
| entry.endpointName, | |
| entry.originalArgs, | |
| (draft) => { | |
| draft.pages.forEach(page => { | |
| page.forEach(entity => { | |
| if (entity.id === id) Object.assign(entity, patch) | |
| }) | |
| }) | |
| }, | |
| ), | |
| ) | |
| default: | |
| throw new Error(`Unknown endpoint (${entry.endpointName}) in updatePost's optimistic update`) | |
| } | |
| }) | |
| const patches = entries.reduce((patches, entry) => { | |
| switch (entry.endpointName) { | |
| case 'getPost': | |
| patches.push( | |
| dispatch( | |
| api.util.updateQueryData( | |
| entry.endpointName, | |
| entry.originalArgs, | |
| (draft) => { | |
| Object.assign(draft, patch) | |
| }, | |
| ), | |
| ), | |
| ) | |
| break | |
| case 'getPosts': | |
| patches.push( | |
| dispatch( | |
| api.util.updateQueryData( | |
| entry.endpointName, | |
| entry.originalArgs, | |
| (draft) => { | |
| draft.pages.forEach(page => { | |
| page.forEach(entity => { | |
| if (entity.id === id) Object.assign(entity, patch) | |
| }) | |
| }) | |
| }, | |
| ), | |
| ), | |
| ) | |
| break | |
| default: | |
| break | |
| } | |
| return patches | |
| }, []) |
Copilot
AI
Apr 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the rollback handler, the callback parameter name patch shadows the outer patch object from the mutation args. Renaming the inner variable (eg patchResult/patchAction) would make the example easier to follow.
| queryFulfilled.catch(() => patches.forEach(patch => patch.undo())) | |
| queryFulfilled.catch(() => patches.forEach((patchResult) => patchResult.undo())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wording is a bit unclear here: “invalidate those instead” reads like it refers to entities rather than queries. Consider rephrasing to “invalidate those queries instead” or “invalidate them instead” for clarity.