Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(baseQuery): expose queryCacheKey in baseQuery #4636

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rtk-query/api/createApi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
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