From e7a98eba3fbef2137109dc68caff305f603f4d1d Mon Sep 17 00:00:00 2001 From: Matt Sutkowski Date: Fri, 21 Jan 2022 07:15:34 -0800 Subject: [PATCH] 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 --- .../toolkit/src/query/core/buildThunks.ts | 7 ++-- .../toolkit/src/query/tests/createApi.test.ts | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/toolkit/src/query/core/buildThunks.ts b/packages/toolkit/src/query/core/buildThunks.ts index 58496877aa..04f6e06ce9 100644 --- a/packages/toolkit/src/query/core/buildThunks.ts +++ b/packages/toolkit/src/query/core/buildThunks.ts @@ -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, @@ -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, @@ -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 ) } diff --git a/packages/toolkit/src/query/tests/createApi.test.ts b/packages/toolkit/src/query/tests/createApi.test.ts index 90c9acb772..a7c1fbbcb8 100644 --- a/packages/toolkit/src/query/tests/createApi.test.ts +++ b/packages/toolkit/src/query/tests/createApi.test.ts @@ -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, @@ -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({ + query: () => '/success', + providesTags: (_result, _error, _arg, meta) => { + _meta = meta + return ['success'] + }, + }), + mutation: build.mutation({ + 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) +})