Feature request: Utility to Update All Cached Query Results for an Endpoint
It would be helpful to have a built-in utility that enables updating all cached query results for a given endpoint, regardless of the specific query arguments used. This pattern comes up in many applications where a change to a single resource (e.g. after a mutation) needs to be reflected across multiple cached queries ā such as paginated lists, filtered views, or entity detail views.
Currently, this requires manually accessing the cache, identifying all relevant entries, and iterating through them with updateQueryData. While possible, this approach adds boilerplate and increases the risk of inconsistencies. A built-in method (e.g. updateAllQueryData) could simplify this common use case and help encourage consistent cache update strategies.
Boilerplate code š
mutationResult.then(({ data }) => {
cachedArgs.forEach((args) => {
dispatch(
api.util.updateQueryData('getItems', args, (draft) => {
const item = draft.items.find((i) => i.id === data.id);
if (item) {
Object.assign(item, data);
}
})
);
});
});
Hypothetical Build-in Code š
mutationResult.then(({ data }) => {
dispatch(
api.util.updateAllQueryData('getItems', (draft) => {
const item = draft.items.find((i) => i.id === data.id);
if (item) {
Object.assign(item, data);
}
})
);
});
Would tags not solve this issue? https://redux-toolkit.js.org/rtk-query/usage/automated-refetching#invalidating-everything-of-a-type
If you added a general tag to an endpoint and used invalidateTags it would invalidate an entire endpoint.
@riqts Thank you. The invalidateTags is effective for triggering refetches, but it does not address scenarios requiring immediate cache updates post-mutation. Refetching can introduce latency and UI flicker, particularly when the updated data is already available in the mutation response.
In many cases, it is desirable to synchronously update all cached query results without refetching or manually iterating over cache entries.
Therefore, Iād like to suggest that a built-in updateAllQueryData utility could make this process easier and help reduce boilerplate.
@nanhk you can use tags in combination with selectInvalidatedBy to get all cache keys tagged in a specific way and then update them.
True, but there's no nice way to do it as a single batched multi-patch dispatch right now.
@EskiMojo14 did throw together a POC of what that might look like a while back:
- https://github.com/reduxjs/redux-toolkit/pull/4110
but in general we don't really offer options for "batched" updates right now, and that's something I've noted we're lacking.