Skip to content

Commit 9faae2a

Browse files
committed
Document serializeQueryArgs option
1 parent af63a4f commit 9faae2a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,20 @@ See also [Invalidating cache data](../usage/automated-refetching.mdx#invalidatin
490490

491491
_(optional, only for query endpoints)_
492492

493-
Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.
493+
Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.a
494494

495495
[summary](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
496496

497497
[examples](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
498498

499+
### `serializeQueryArgs`
500+
501+
_(optional, only for query endpoints)_
502+
503+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.serializeQueryArgs)
504+
505+
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.serializeQueryArgs)
506+
499507
### `onQueryStarted`
500508

501509
_(optional)_

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,56 @@ export interface QueryExtraOptions<
318318
*/
319319
invalidatesTags?: never
320320

321-
serializeQueryArgs?: SerializeQueryArgs<any>
321+
/**
322+
* Can be provided to return a custom cache key string based on the provided arguments.
323+
*
324+
* This is primarily intended for cases where a non-serializable value is passed as part of the query arg object and should be excluded from the cache key. It may also be used for cases where an endpoint should only have a single cache entry, such as an infinite loading / pagination implementation.
325+
*
326+
* @example
327+
*
328+
* ```ts
329+
* // codeblock-meta title="serializeQueryArgs : exclude value"
330+
*
331+
* import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
332+
* interface Post {
333+
* id: number
334+
* name: string
335+
* }
336+
*
337+
* interface MyApiClient {
338+
* fetchPost: (id: string) => Promise<Post>
339+
* }
340+
*
341+
* createApi({
342+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
343+
* tagTypes: ['Posts'],
344+
* endpoints: (build) => ({
345+
* // Example: an endpoint with an API client passed in as an argument,
346+
* // but only the item ID should be used as the cache key
347+
* getPost: build.query<Post, { id: string; client: MyApiClient }>({
348+
* queryFn: async ({ id, client }) => {
349+
* const post = await client.fetchPost(id)
350+
* return { data: post }
351+
* },
352+
* // highlight-start
353+
* serializeQueryArgs: ({ queryArgs, endpointDefinition, endpointName }) => {
354+
* const { id } = queryArgs
355+
* // You can use `defaultSerializeQueryArgs` to do the work:
356+
* return defaultSerializeQueryArgs({
357+
* endpointName,
358+
* queryArgs: { id },
359+
* endpointDefinition
360+
* })
361+
* // Or alternately, create a string yourself:
362+
* // return `getPost(${id})`
363+
* },
364+
* // highlight-end
365+
* }),
366+
* }),
367+
*})
368+
* ```
369+
*/
370+
serializeQueryArgs?: SerializeQueryArgs<QueryArg>
322371

323372
/**
324373
* Can be provided to merge the current cache value into the new cache value.

0 commit comments

Comments
 (0)