diff --git a/docs/rtk-query/api/createApi.mdx b/docs/rtk-query/api/createApi.mdx index 50a64201fa..6e99e1476e 100644 --- a/docs/rtk-query/api/createApi.mdx +++ b/docs/rtk-query/api/createApi.mdx @@ -96,6 +96,7 @@ export const { useGetPokemonByNameQuery } = pokemonApi - `endpoint` - The name of the endpoint. - `type` - Type of request (`query` or `mutation`). - `forced` - Indicates if a query has been forced. + - `queryCacheKey`- The computed query cache key. - `extraOptions` - The value of the optional `extraOptions` property provided for a given endpoint #### baseQuery function signature diff --git a/packages/toolkit/src/query/baseQueryTypes.ts b/packages/toolkit/src/query/baseQueryTypes.ts index 9af22a3f7e..0553426bf1 100644 --- a/packages/toolkit/src/query/baseQueryTypes.ts +++ b/packages/toolkit/src/query/baseQueryTypes.ts @@ -18,6 +18,10 @@ export interface BaseQueryApi { * invalidated queries. */ forced?: boolean + /** + * Only available for queries: the cache key that was used to store the query result + */ + queryCacheKey?: string } export type QueryReturnValue = diff --git a/packages/toolkit/src/query/core/buildThunks.ts b/packages/toolkit/src/query/core/buildThunks.ts index 740a11404a..8d9b69a2f9 100644 --- a/packages/toolkit/src/query/core/buildThunks.ts +++ b/packages/toolkit/src/query/core/buildThunks.ts @@ -381,6 +381,7 @@ export function buildThunks< type: arg.type, forced: arg.type === 'query' ? isForcedQuery(arg, getState()) : undefined, + queryCacheKey: arg.type === 'query' ? arg.queryCacheKey : undefined, } const forceQueryFn = diff --git a/packages/toolkit/src/query/tests/buildInitiate.test.tsx b/packages/toolkit/src/query/tests/buildInitiate.test.tsx index f7022acf30..08be5a3558 100644 --- a/packages/toolkit/src/query/tests/buildInitiate.test.tsx +++ b/packages/toolkit/src/query/tests/buildInitiate.test.tsx @@ -1,4 +1,4 @@ -import { setupApiStore } from '../../tests/utils/helpers' +import { setupApiStore } from '@internal/tests/utils/helpers' import { createApi } from '../core' import type { SubscriptionSelectors } from '../core/buildMiddleware/types' import { fakeBaseQuery } from '../fakeBaseQuery' @@ -119,3 +119,57 @@ describe('calling initiate without a cache entry, with subscribe: false still re ).toBe(false) }) }) + +describe('calling initiate should have resulting queryCacheKey match baseQuery queryCacheKey', () => { + const baseQuery = vi.fn(() => ({ data: 'success' })) + function getNewApi() { + return createApi({ + baseQuery, + endpoints: (build) => ({ + query: build.query({ + query: (args) => `queryUrl/${args.arg1}/${args.arg2}`, + }), + mutation: build.mutation({ + query: () => 'mutationUrl', + }), + }), + }) + } + let api = getNewApi() + beforeEach(() => { + baseQuery.mockClear() + api = getNewApi() + }) + + test('should be a string and matching on queries', () => { + const { store: storeApi } = setupApiStore(api, undefined, { + withoutTestLifecycles: true, + }) + const promise = storeApi.dispatch( + api.endpoints.query.initiate({ arg2: 'secondArg', arg1: 'firstArg' }), + ) + expect(baseQuery).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + queryCacheKey: promise.queryCacheKey, + }), + undefined, + ) + }) + + test('should be undefined and matching on mutations', () => { + const { store: storeApi } = setupApiStore(api, undefined, { + withoutTestLifecycles: true, + }) + storeApi.dispatch( + api.endpoints.mutation.initiate({ arg2: 'secondArg', arg1: 'firstArg' }), + ) + expect(baseQuery).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + queryCacheKey: undefined, + }), + undefined, + ) + }) +}) diff --git a/packages/toolkit/src/query/tests/createApi.test.ts b/packages/toolkit/src/query/tests/createApi.test.ts index ae745b18b2..7c277b4618 100644 --- a/packages/toolkit/src/query/tests/createApi.test.ts +++ b/packages/toolkit/src/query/tests/createApi.test.ts @@ -314,6 +314,7 @@ describe('endpoint definition typings', () => { getState: expect.any(Function), signal: expect.any(Object), type: expect.any(String), + queryCacheKey: expect.any(String), } beforeEach(() => { baseQuery.mockClear() @@ -355,6 +356,7 @@ describe('endpoint definition typings', () => { abort: expect.any(Function), forced: expect.any(Boolean), type: expect.any(String), + queryCacheKey: expect.any(String), }, undefined, ], @@ -368,6 +370,7 @@ describe('endpoint definition typings', () => { abort: expect.any(Function), forced: expect.any(Boolean), type: expect.any(String), + queryCacheKey: expect.any(String), }, undefined, ], @@ -499,8 +502,24 @@ describe('endpoint definition typings', () => { expect(baseQuery.mock.calls).toEqual([ ['modified1', commonBaseQueryApi, undefined], ['modified2', commonBaseQueryApi, undefined], - ['modified1', { ...commonBaseQueryApi, forced: undefined }, undefined], - ['modified2', { ...commonBaseQueryApi, forced: undefined }, undefined], + [ + 'modified1', + { + ...commonBaseQueryApi, + forced: undefined, + queryCacheKey: undefined, + }, + undefined, + ], + [ + 'modified2', + { + ...commonBaseQueryApi, + forced: undefined, + queryCacheKey: undefined, + }, + undefined, + ], ]) })