Skip to content

Commit 5ff666f

Browse files
committed
Document merge option
1 parent 9faae2a commit 5ff666f

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,14 @@ _(optional, only for query endpoints)_
504504

505505
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.serializeQueryArgs)
506506

507+
### `merge`
508+
509+
_(optional, only for query endpoints)_ w
510+
511+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.merge)
512+
513+
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.merge)
514+
507515
### `onQueryStarted`
508516

509517
_(optional)_

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ export interface QueryExtraOptions<
340340
*
341341
* createApi({
342342
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
343-
* tagTypes: ['Posts'],
344343
* endpoints: (build) => ({
345344
* // Example: an endpoint with an API client passed in as an argument,
346345
* // but only the item ID should be used as the cache key
@@ -370,18 +369,57 @@ export interface QueryExtraOptions<
370369
serializeQueryArgs?: SerializeQueryArgs<QueryArg>
371370

372371
/**
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.
374373
* If supplied, no automatic structural sharing will be applied - it's up to
375374
* you to update the cache appropriately.
376375
*
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+
*
377380
* 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.
379382
*
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.
381385
*
382386
* Useful if you don't want a new request to completely override the current cache value,
383387
* maybe because you have manually updated it from another source and don't want those
384388
* 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+
* ```
385423
*/
386424
merge?(
387425
currentCacheData: ResultType,

0 commit comments

Comments
 (0)