Skip to content

Feature request: Utility to Update All Cached Query Results for an EndpointΒ #5074

@nanhk

Description

@nanhk

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);
      }
    })
  );
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions