Skip to content

Commit f009cc9

Browse files
authored
Merge pull request #4636 from HaakonSvane/add-queryCacheKey-to-baseQuery-api
feat(baseQuery): expose queryCacheKey in baseQuery
2 parents c8f3739 + 8b510b4 commit f009cc9

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const { useGetPokemonByNameQuery } = pokemonApi
9696
- `endpoint` - The name of the endpoint.
9797
- `type` - Type of request (`query` or `mutation`).
9898
- `forced` - Indicates if a query has been forced.
99+
- `queryCacheKey`- The computed query cache key.
99100
- `extraOptions` - The value of the optional `extraOptions` property provided for a given endpoint
100101

101102
#### baseQuery function signature

packages/toolkit/src/query/baseQueryTypes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export interface BaseQueryApi {
1818
* invalidated queries.
1919
*/
2020
forced?: boolean
21+
/**
22+
* Only available for queries: the cache key that was used to store the query result
23+
*/
24+
queryCacheKey?: string
2125
}
2226

2327
export type QueryReturnValue<T = unknown, E = unknown, M = unknown> =

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ export function buildThunks<
381381
type: arg.type,
382382
forced:
383383
arg.type === 'query' ? isForcedQuery(arg, getState()) : undefined,
384+
queryCacheKey: arg.type === 'query' ? arg.queryCacheKey : undefined,
384385
}
385386

386387
const forceQueryFn =

packages/toolkit/src/query/tests/buildInitiate.test.tsx

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setupApiStore } from '../../tests/utils/helpers'
1+
import { setupApiStore } from '@internal/tests/utils/helpers'
22
import { createApi } from '../core'
33
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
44
import { fakeBaseQuery } from '../fakeBaseQuery'
@@ -119,3 +119,57 @@ describe('calling initiate without a cache entry, with subscribe: false still re
119119
).toBe(false)
120120
})
121121
})
122+
123+
describe('calling initiate should have resulting queryCacheKey match baseQuery queryCacheKey', () => {
124+
const baseQuery = vi.fn(() => ({ data: 'success' }))
125+
function getNewApi() {
126+
return createApi({
127+
baseQuery,
128+
endpoints: (build) => ({
129+
query: build.query<void, { arg1: string; arg2: string }>({
130+
query: (args) => `queryUrl/${args.arg1}/${args.arg2}`,
131+
}),
132+
mutation: build.mutation<void, { arg1: string; arg2: string }>({
133+
query: () => 'mutationUrl',
134+
}),
135+
}),
136+
})
137+
}
138+
let api = getNewApi()
139+
beforeEach(() => {
140+
baseQuery.mockClear()
141+
api = getNewApi()
142+
})
143+
144+
test('should be a string and matching on queries', () => {
145+
const { store: storeApi } = setupApiStore(api, undefined, {
146+
withoutTestLifecycles: true,
147+
})
148+
const promise = storeApi.dispatch(
149+
api.endpoints.query.initiate({ arg2: 'secondArg', arg1: 'firstArg' }),
150+
)
151+
expect(baseQuery).toHaveBeenCalledWith(
152+
expect.any(String),
153+
expect.objectContaining({
154+
queryCacheKey: promise.queryCacheKey,
155+
}),
156+
undefined,
157+
)
158+
})
159+
160+
test('should be undefined and matching on mutations', () => {
161+
const { store: storeApi } = setupApiStore(api, undefined, {
162+
withoutTestLifecycles: true,
163+
})
164+
storeApi.dispatch(
165+
api.endpoints.mutation.initiate({ arg2: 'secondArg', arg1: 'firstArg' }),
166+
)
167+
expect(baseQuery).toHaveBeenCalledWith(
168+
expect.any(String),
169+
expect.objectContaining({
170+
queryCacheKey: undefined,
171+
}),
172+
undefined,
173+
)
174+
})
175+
})

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ describe('endpoint definition typings', () => {
314314
getState: expect.any(Function),
315315
signal: expect.any(Object),
316316
type: expect.any(String),
317+
queryCacheKey: expect.any(String),
317318
}
318319
beforeEach(() => {
319320
baseQuery.mockClear()
@@ -355,6 +356,7 @@ describe('endpoint definition typings', () => {
355356
abort: expect.any(Function),
356357
forced: expect.any(Boolean),
357358
type: expect.any(String),
359+
queryCacheKey: expect.any(String),
358360
},
359361
undefined,
360362
],
@@ -368,6 +370,7 @@ describe('endpoint definition typings', () => {
368370
abort: expect.any(Function),
369371
forced: expect.any(Boolean),
370372
type: expect.any(String),
373+
queryCacheKey: expect.any(String),
371374
},
372375
undefined,
373376
],
@@ -499,8 +502,24 @@ describe('endpoint definition typings', () => {
499502
expect(baseQuery.mock.calls).toEqual([
500503
['modified1', commonBaseQueryApi, undefined],
501504
['modified2', commonBaseQueryApi, undefined],
502-
['modified1', { ...commonBaseQueryApi, forced: undefined }, undefined],
503-
['modified2', { ...commonBaseQueryApi, forced: undefined }, undefined],
505+
[
506+
'modified1',
507+
{
508+
...commonBaseQueryApi,
509+
forced: undefined,
510+
queryCacheKey: undefined,
511+
},
512+
undefined,
513+
],
514+
[
515+
'modified2',
516+
{
517+
...commonBaseQueryApi,
518+
forced: undefined,
519+
queryCacheKey: undefined,
520+
},
521+
undefined,
522+
],
504523
])
505524
})
506525

0 commit comments

Comments
 (0)