@@ -175,13 +175,19 @@ export type UpsertQueryDataThunk<
175
175
> = < EndpointName extends QueryKeys < Definitions > > (
176
176
endpointName : EndpointName ,
177
177
args : QueryArgFrom < Definitions [ EndpointName ] > ,
178
- upsertRecipe : UpsertRecipe < ResultTypeFrom < Definitions [ EndpointName ] > >
179
- ) => ThunkAction < PatchCollection , PartialState , any , AnyAction >
178
+ value : ResultTypeFrom < Definitions [ EndpointName ] >
179
+ ) => ThunkAction < void , PartialState , any , AnyAction >
180
180
181
181
/**
182
182
* An object returned from dispatching a `api.util.updateQueryData` call.
183
183
*/
184
184
export type PatchCollection = {
185
+ /**
186
+ * A boolean stating if there was already data in the query cache
187
+ *
188
+ * If there was no data in the cache no update operation is performed
189
+ */
190
+ emptyCache : boolean
185
191
/**
186
192
* An `immer` Patch describing the cache update.
187
193
*/
@@ -236,6 +242,7 @@ export function buildThunks<
236
242
api . endpoints [ endpointName ] as ApiEndpointQuery < any , any >
237
243
) . select ( args ) ( getState ( ) )
238
244
let ret : PatchCollection = {
245
+ emptyCache : true ,
239
246
patches : [ ] ,
240
247
inversePatches : [ ] ,
241
248
undo : ( ) =>
@@ -247,6 +254,7 @@ export function buildThunks<
247
254
return ret
248
255
}
249
256
if ( 'data' in currentState ) {
257
+ ret . emptyCache = false
250
258
if ( isDraftable ( currentState . data ) ) {
251
259
const [ , patches , inversePatches ] = produceWithPatches (
252
260
currentState . data ,
@@ -271,59 +279,21 @@ export function buildThunks<
271
279
}
272
280
273
281
const upsertQueryData : UpsertQueryDataThunk < EndpointDefinitions , State > =
274
- ( endpointName , args , upsertRecipe ) => ( dispatch , getState ) => {
275
- const currentState = (
276
- api . endpoints [ endpointName ] as ApiEndpointQuery < any , any >
277
- ) . select ( args ) ( getState ( ) )
278
- let ret : PatchCollection = {
279
- patches : [ ] ,
280
- inversePatches : [ ] ,
281
- undo : ( ) =>
282
- dispatch (
283
- api . util . patchQueryData ( endpointName , args , ret . inversePatches )
284
- ) ,
285
- }
286
- if ( 'data' in currentState ) {
287
- if ( isDraftable ( currentState . data ) ) {
288
- const [ , patches , inversePatches ] = produceWithPatches (
289
- currentState . data ,
290
- upsertRecipe
291
- )
292
- ret . patches . push ( ...patches )
293
- ret . inversePatches . push ( ...inversePatches )
294
- } else {
295
- const value = upsertRecipe ( currentState . data )
296
- ret . patches . push ( { op : 'replace' , path : [ ] , value } )
297
- ret . inversePatches . push ( {
298
- op : 'replace' ,
299
- path : [ ] ,
300
- value : currentState . data ,
301
- } )
302
- }
303
- dispatch ( api . util . patchQueryData ( endpointName , args , ret . patches ) )
304
- } else {
305
- ret . inversePatches . push ( {
306
- op : 'replace' ,
307
- path : [ ] ,
308
- value : undefined ,
282
+ ( endpointName , args , value ) => ( dispatch ) => {
283
+ dispatch (
284
+ (
285
+ api . endpoints [ endpointName ] as ApiEndpointQuery <
286
+ QueryDefinition < any , any , any , any , any > ,
287
+ Definitions
288
+ >
289
+ ) . initiate ( args , {
290
+ subscribe : false ,
291
+ forceRefetch : true ,
292
+ [ forceQueryFnSymbol ] : ( ) => ( {
293
+ data : value ,
294
+ } ) ,
309
295
} )
310
- dispatch (
311
- (
312
- api . endpoints [ endpointName ] as ApiEndpointQuery <
313
- QueryDefinition < any , any , any , any , any > ,
314
- Definitions
315
- >
316
- ) . initiate ( args , {
317
- subscribe : false ,
318
- forceRefetch : true ,
319
- [ forceQueryFnSymbol ] : ( ) => ( {
320
- data : upsertRecipe ( undefined ) ,
321
- } ) ,
322
- } )
323
- )
324
- }
325
-
326
- return ret
296
+ )
327
297
}
328
298
329
299
const executeEndpoint : AsyncThunkPayloadCreator <
0 commit comments