Skip to content

Commit

Permalink
Pass baseQueryMeta into calculateProvidedBy (#1926)
Browse files Browse the repository at this point in the history
* Pass baseQueryMeta into calculateProvidedBy
* Assert that baseQueryMeta is available for invalidation mechanisms
* simplify type guards
* remove unused import

Co-authored-by: Lenz Weber <lenz.weber@mayflower.de>
  • Loading branch information
msutkowski and Lenz Weber committed Jan 21, 2022
1 parent 18c3815 commit e7a98eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import type {
BaseQueryError,
QueryReturnValue,
} from '../baseQueryTypes'
import { BaseQueryArg } from '../baseQueryTypes'
import type { RootState, QueryKeys, QuerySubstateIdentifier } from './apiState'
import { QueryStatus, CombinedState } from './apiState'
import { QueryStatus } from './apiState'
import type { StartQueryActionCreatorOptions } from './buildInitiate'
import type {
AssertTagTypes,
Expand All @@ -18,7 +17,7 @@ import type {
QueryDefinition,
ResultTypeFrom,
} from '../endpointDefinitions'
import { calculateProvidedBy, FullTagDescription } from '../endpointDefinitions'
import { calculateProvidedBy } from '../endpointDefinitions'
import type { AsyncThunkPayloadCreator, Draft } from '@reduxjs/toolkit'
import {
isAllOf,
Expand Down Expand Up @@ -513,7 +512,7 @@ export function calculateProvidedByThunk(
isFulfilled(action) ? action.payload : undefined,
isRejectedWithValue(action) ? action.payload : undefined,
action.meta.arg.originalArgs,
action.meta,
'baseQueryMeta' in action.meta ? action.meta.baseQueryMeta : undefined,
assertTagType
)
}
41 changes: 41 additions & 0 deletions packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
QueryDefinition,
} from '@reduxjs/toolkit/query'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
import type { FetchBaseQueryMeta } from '@reduxjs/toolkit/dist/query/fetchBaseQuery'

import {
ANY,
expectType,
Expand Down Expand Up @@ -722,3 +724,42 @@ describe('query endpoint lifecycles - onStart, onSuccess, onError', () => {
expect(storeRef.store.getState().testReducer.count).toBe(1)
})
})

test('providesTags and invalidatesTags can use baseQueryMeta', async () => {
let _meta: FetchBaseQueryMeta | undefined

type SuccessResponse = { value: 'success' }

const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'http://example.com' }),
tagTypes: ['success'],
endpoints: (build) => ({
query: build.query<SuccessResponse, void>({
query: () => '/success',
providesTags: (_result, _error, _arg, meta) => {
_meta = meta
return ['success']
},
}),
mutation: build.mutation<SuccessResponse, void>({
query: () => ({ url: '/success', method: 'POST' }),
invalidatesTags: (_result, _error, _arg, meta) => {
_meta = meta
return ['success']
},
}),
}),
})

const storeRef = setupApiStore(api)

await storeRef.store.dispatch(api.endpoints.query.initiate())

expect('request' in _meta! && 'response' in _meta!).toBe(true)

_meta = undefined

await storeRef.store.dispatch(api.endpoints.mutation.initiate())

expect('request' in _meta! && 'response' in _meta!).toBe(true)
})

0 comments on commit e7a98eb

Please sign in to comment.