@@ -340,7 +340,6 @@ export interface QueryExtraOptions<
340
340
*
341
341
* createApi({
342
342
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
343
- * tagTypes: ['Posts'],
344
343
* endpoints: (build) => ({
345
344
* // Example: an endpoint with an API client passed in as an argument,
346
345
* // but only the item ID should be used as the cache key
@@ -370,18 +369,57 @@ export interface QueryExtraOptions<
370
369
serializeQueryArgs ?: SerializeQueryArgs < QueryArg >
371
370
372
371
/**
373
- * Can be provided to merge the current cache value into the new cache value .
372
+ * Can be provided to merge an incoming response value into the current cache data .
374
373
* If supplied, no automatic structural sharing will be applied - it's up to
375
374
* you to update the cache appropriately.
376
375
*
376
+ * Since RTKQ normally replaces cache entries with the new response, you will usually
377
+ * need to use this with the `serializeQueryArgs` or `forceRefetch` options to keep
378
+ * an existing cache entry so that it can be updated.
379
+ *
377
380
* Since this is wrapped with Immer, you , you may either mutate the `currentCacheValue` directly,
378
- * or return a new value, but _not_ both at once. *
381
+ * or return a new value, but _not_ both at once.
379
382
*
380
- * Will only be called if the existing `currentCacheValue` is not `undefined`.
383
+ * Will only be called if the existing `currentCacheData` is _not_ `undefined` - on first response,
384
+ * the cache entry will just save the response data directly.
381
385
*
382
386
* Useful if you don't want a new request to completely override the current cache value,
383
387
* maybe because you have manually updated it from another source and don't want those
384
388
* updates to get lost.
389
+ *
390
+ *
391
+ * @example
392
+ *
393
+ * ```ts
394
+ * // codeblock-meta title="merge: pagination"
395
+ *
396
+ * import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
397
+ * interface Post {
398
+ * id: number
399
+ * name: string
400
+ * }
401
+ *
402
+ * createApi({
403
+ * baseQuery: fetchBaseQuery({ baseUrl: '/' }),
404
+ * endpoints: (build) => ({
405
+ * listItems: build.query<string[], number>({
406
+ * query: (pageNumber) => `/listItems?page=${pageNumber}`,
407
+ * // Only have one cache entry because the arg always maps to one string
408
+ * serializeQueryArgs: ({ endpointName }) => {
409
+ * return endpointName
410
+ * },
411
+ * // Always merge incoming data to the cache entry
412
+ * merge: (currentCache, newItems) => {
413
+ * currentCache.push(...newItems)
414
+ * },
415
+ * // Refetch when the page arg changes
416
+ * forceRefetch({ currentArg, previousArg }) {
417
+ * return currentArg !== previousArg
418
+ * },
419
+ * }),
420
+ * }),
421
+ *})
422
+ * ```
385
423
*/
386
424
merge ?(
387
425
currentCacheData : ResultType ,
0 commit comments