1- import type { Action , PayloadAction , UnknownAction } from '@reduxjs/toolkit'
1+ import type { PayloadAction } from '@reduxjs/toolkit'
22import {
33 combineReducers ,
44 createAction ,
@@ -23,7 +23,6 @@ import type {
2323 QueryCacheKey ,
2424 SubscriptionState ,
2525 ConfigState ,
26- QueryKeys ,
2726 InfiniteQuerySubState ,
2827 InfiniteQueryDirection ,
2928} from './apiState'
@@ -36,18 +35,14 @@ import type {
3635 MutationThunk ,
3736 QueryThunk ,
3837 QueryThunkArg ,
39- RejectedAction ,
4038} from './buildThunks'
4139import { calculateProvidedByThunk } from './buildThunks'
4240import {
4341 isInfiniteQueryDefinition ,
4442 type AssertTagTypes ,
45- type DefinitionType ,
4643 type EndpointDefinitions ,
4744 type FullTagDescription ,
48- type QueryArgFrom ,
4945 type QueryDefinition ,
50- type ResultTypeFrom ,
5146} from '../endpointDefinitions'
5247import type { Patch } from 'immer'
5348import { isDraft } from 'immer'
@@ -61,6 +56,7 @@ import {
6156import type { ApiContext } from '../apiTypes'
6257import { isUpsertQuery } from './buildInitiate'
6358import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
59+ import type { UnwrapPromise } from '../tsHelpers'
6460
6561/**
6662 * A typesafe single entry to be upserted into the cache
@@ -279,6 +275,7 @@ export function buildSlice({
279275 substate . fulfilledTimeStamp = meta . fulfilledTimeStamp
280276 } )
281277 }
278+
282279 const querySlice = createSlice ( {
283280 name : `${ reducerPath } /queries` ,
284281 initialState : initialState as QueryState < any > ,
@@ -486,6 +483,11 @@ export function buildSlice({
486483 } ,
487484 } )
488485
486+ type CalculateProvidedByAction = UnwrapPromise <
487+ | ReturnType < ReturnType < QueryThunk > >
488+ | ReturnType < ReturnType < InfiniteQueryThunk < any > > >
489+ >
490+
489491 const invalidationSlice = createSlice ( {
490492 name : `${ reducerPath } /invalidation` ,
491493 initialState : initialState as InvalidationState < string > ,
@@ -562,26 +564,51 @@ export function buildSlice({
562564 . addMatcher (
563565 isAnyOf ( isFulfilled ( queryThunk ) , isRejectedWithValue ( queryThunk ) ) ,
564566 ( draft , action ) => {
565- const providedTags = calculateProvidedByThunk (
566- action ,
567- 'providesTags' ,
568- definitions ,
569- assertTagType ,
570- )
571- const { queryCacheKey } = action . meta . arg
567+ writeProvidedTagsForQuery ( draft , action )
568+ } ,
569+ )
570+ . addMatcher (
571+ querySlice . actions . cacheEntriesUpserted . match ,
572+ ( draft , action ) => {
573+ for ( const { queryDescription : arg , value } of action . payload ) {
574+ const action : CalculateProvidedByAction = {
575+ type : 'UNKNOWN' ,
576+ payload : value ,
577+ meta : {
578+ requestStatus : 'fulfilled' ,
579+ requestId : 'UNKNOWN' ,
580+ arg,
581+ } ,
582+ }
572583
573- invalidationSlice . caseReducers . updateProvidedBy (
574- draft ,
575- invalidationSlice . actions . updateProvidedBy ( {
576- queryCacheKey,
577- providedTags,
578- } ) ,
579- )
584+ writeProvidedTagsForQuery ( draft , action )
585+ }
580586 } ,
581587 )
582588 } ,
583589 } )
584590
591+ function writeProvidedTagsForQuery (
592+ draft : InvalidationState < string > ,
593+ action : CalculateProvidedByAction ,
594+ ) {
595+ const providedTags = calculateProvidedByThunk (
596+ action ,
597+ 'providesTags' ,
598+ definitions ,
599+ assertTagType ,
600+ )
601+ const { queryCacheKey } = action . meta . arg
602+
603+ invalidationSlice . caseReducers . updateProvidedBy (
604+ draft ,
605+ invalidationSlice . actions . updateProvidedBy ( {
606+ queryCacheKey,
607+ providedTags,
608+ } ) ,
609+ )
610+ }
611+
585612 // Dummy slice to generate actions
586613 const subscriptionSlice = createSlice ( {
587614 name : `${ reducerPath } /subscriptions` ,
0 commit comments