Skip to content

Can I get all cache entries of a query in updateQueryData? #1312

Answered by kira-krul
kira-krul asked this question in Q&A
Discussion options

You must be logged in to vote

Created a helper function to get them. Typescript isn't my forte so it's not the most clean or user-friendly way to do it but hey it works. Here's the function in case someone needs it:

import { Api } from '@reduxjs/toolkit/dist/query';
import api from 'service/api/api';
import store from 'service/store';

function getQueryCacheEntries<
  Endpoints extends keyof Api<any, any, any, any>['endpoints'] = string
>(query?: Endpoints) {
  const { queries } = store.getState()[api.reducerPath];
  let allQueries = [];
  for (let queryCache of Object.values(queries)) {
    if (!query || queryCache?.endpointName === query) {
      allQueries.push(queryCache);
    }
  }
  return allQueries;
}

export d…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@kira-krul
Comment options

@kira-krul
Comment options

@phryneas
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by kira-krul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants