From a877759cdc70f6de382754956dbdc90b36e6ab5e Mon Sep 17 00:00:00 2001 From: HaakonSvane Date: Fri, 20 Sep 2024 10:00:49 +0200 Subject: [PATCH 1/3] expose queryCacheKey in in baseQuery. write tests --- packages/toolkit/src/query/baseQueryTypes.ts | 4 ++ .../toolkit/src/query/core/buildThunks.ts | 1 + .../src/query/tests/buildInitiate.test.tsx | 56 ++++++++++++++++++- .../toolkit/src/query/tests/createApi.test.ts | 23 +++++++- 4 files changed, 81 insertions(+), 3 deletions(-) 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, + ], ]) }) From 8eb2ef6102531cde3ec661c6a8dc9f478e58ab89 Mon Sep 17 00:00:00 2001 From: HaakonSvane Date: Fri, 20 Sep 2024 10:19:20 +0200 Subject: [PATCH 2/3] add entry for queryCacheKey in createApi docs --- docs/rtk-query/api/createApi.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/rtk-query/api/createApi.mdx b/docs/rtk-query/api/createApi.mdx index 24b205556d..615e0cc23d 100644 --- a/docs/rtk-query/api/createApi.mdx +++ b/docs/rtk-query/api/createApi.mdx @@ -91,6 +91,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 From 8b510b4415dba952b6dbcd2e725b2cb2ac9371ed Mon Sep 17 00:00:00 2001 From: HaakonSvane Date: Fri, 20 Sep 2024 10:20:46 +0200 Subject: [PATCH 3/3] add missing period in docs --- docs/rtk-query/api/createApi.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rtk-query/api/createApi.mdx b/docs/rtk-query/api/createApi.mdx index 615e0cc23d..d1f48588db 100644 --- a/docs/rtk-query/api/createApi.mdx +++ b/docs/rtk-query/api/createApi.mdx @@ -91,7 +91,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 + - `queryCacheKey`- The computed query cache key. - `extraOptions` - The value of the optional `extraOptions` property provided for a given endpoint #### baseQuery function signature