Skip to content

Commit e7a98eb

Browse files
msutkowskiLenz Weber
and
Lenz Weber
authored
Pass baseQueryMeta into calculateProvidedBy (#1926)
* Pass baseQueryMeta into calculateProvidedBy * Assert that baseQueryMeta is available for invalidation mechanisms * simplify type guards * remove unused import Co-authored-by: Lenz Weber <[email protected]>
1 parent 18c3815 commit e7a98eb

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import type {
55
BaseQueryError,
66
QueryReturnValue,
77
} from '../baseQueryTypes'
8-
import { BaseQueryArg } from '../baseQueryTypes'
98
import type { RootState, QueryKeys, QuerySubstateIdentifier } from './apiState'
10-
import { QueryStatus, CombinedState } from './apiState'
9+
import { QueryStatus } from './apiState'
1110
import type { StartQueryActionCreatorOptions } from './buildInitiate'
1211
import type {
1312
AssertTagTypes,
@@ -18,7 +17,7 @@ import type {
1817
QueryDefinition,
1918
ResultTypeFrom,
2019
} from '../endpointDefinitions'
21-
import { calculateProvidedBy, FullTagDescription } from '../endpointDefinitions'
20+
import { calculateProvidedBy } from '../endpointDefinitions'
2221
import type { AsyncThunkPayloadCreator, Draft } from '@reduxjs/toolkit'
2322
import {
2423
isAllOf,
@@ -513,7 +512,7 @@ export function calculateProvidedByThunk(
513512
isFulfilled(action) ? action.payload : undefined,
514513
isRejectedWithValue(action) ? action.payload : undefined,
515514
action.meta.arg.originalArgs,
516-
action.meta,
515+
'baseQueryMeta' in action.meta ? action.meta.baseQueryMeta : undefined,
517516
assertTagType
518517
)
519518
}

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

+41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type {
55
QueryDefinition,
66
} from '@reduxjs/toolkit/query'
77
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
8+
import type { FetchBaseQueryMeta } from '@reduxjs/toolkit/dist/query/fetchBaseQuery'
9+
810
import {
911
ANY,
1012
expectType,
@@ -722,3 +724,42 @@ describe('query endpoint lifecycles - onStart, onSuccess, onError', () => {
722724
expect(storeRef.store.getState().testReducer.count).toBe(1)
723725
})
724726
})
727+
728+
test('providesTags and invalidatesTags can use baseQueryMeta', async () => {
729+
let _meta: FetchBaseQueryMeta | undefined
730+
731+
type SuccessResponse = { value: 'success' }
732+
733+
const api = createApi({
734+
baseQuery: fetchBaseQuery({ baseUrl: 'http://example.com' }),
735+
tagTypes: ['success'],
736+
endpoints: (build) => ({
737+
query: build.query<SuccessResponse, void>({
738+
query: () => '/success',
739+
providesTags: (_result, _error, _arg, meta) => {
740+
_meta = meta
741+
return ['success']
742+
},
743+
}),
744+
mutation: build.mutation<SuccessResponse, void>({
745+
query: () => ({ url: '/success', method: 'POST' }),
746+
invalidatesTags: (_result, _error, _arg, meta) => {
747+
_meta = meta
748+
return ['success']
749+
},
750+
}),
751+
}),
752+
})
753+
754+
const storeRef = setupApiStore(api)
755+
756+
await storeRef.store.dispatch(api.endpoints.query.initiate())
757+
758+
expect('request' in _meta! && 'response' in _meta!).toBe(true)
759+
760+
_meta = undefined
761+
762+
await storeRef.store.dispatch(api.endpoints.mutation.initiate())
763+
764+
expect('request' in _meta! && 'response' in _meta!).toBe(true)
765+
})

0 commit comments

Comments
 (0)