Skip to content

Commit

Permalink
Merge pull request #4636 from HaakonSvane/add-queryCacheKey-to-baseQu…
Browse files Browse the repository at this point in the history
…ery-api

feat(baseQuery): expose queryCacheKey in baseQuery
  • Loading branch information
EskiMojo14 authored Sep 24, 2024
2 parents c8f3739 + 8b510b4 commit f009cc9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/rtk-query/api/createApi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/toolkit/src/query/baseQueryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = unknown, E = unknown, M = unknown> =
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
56 changes: 55 additions & 1 deletion packages/toolkit/src/query/tests/buildInitiate.test.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<void, { arg1: string; arg2: string }>({
query: (args) => `queryUrl/${args.arg1}/${args.arg2}`,
}),
mutation: build.mutation<void, { arg1: string; arg2: string }>({
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,
)
})
})
23 changes: 21 additions & 2 deletions packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
],
Expand All @@ -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,
],
Expand Down Expand Up @@ -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,
],
])
})

Expand Down

0 comments on commit f009cc9

Please sign in to comment.