Skip to content

Commit 71003b8

Browse files
committed
Document forceRefetch option
1 parent d567ef3 commit 71003b8

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ If you specify `track: false` when manually dispatching queries, RTK Query will
380380

381381
_(required if no `queryFn` provided)_
382382

383+
```ts title="query signature" no-transpile
384+
export type query = <QueryArg>(
385+
arg: QueryArg
386+
) => string | Record<string, unknown>
387+
388+
// with `fetchBaseQuery`
389+
export type query = <QueryArg>(arg: QueryArg) => string | FetchArgs
390+
```
391+
383392
[summary](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQuery.query)
384393
385394
[examples](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQuery.query)
@@ -506,12 +515,29 @@ _(optional, only for query endpoints)_
506515

507516
### `merge`
508517

509-
_(optional, only for query endpoints)_ w
518+
_(optional, only for query endpoints)_
510519

511520
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.merge)
512521

513522
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.merge)
514523

524+
### `forceRefetch`
525+
526+
_(optional, only for query endpoints)_
527+
528+
```ts title="forceRefetch signature" no-transpile
529+
type forceRefetch = (params: {
530+
currentArg: QueryArg | undefined
531+
previousArg: QueryArg | undefined
532+
state: RootState<any, any, string>
533+
endpointState?: QuerySubState<any>
534+
}) => boolean
535+
```
536+
537+
[summary](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.forceRefetch)
538+
539+
[examples](docblock://query/endpointDefinitions.ts?token=QueryExtraOptions.forceRefetch)
540+
515541
### `onQueryStarted`
516542
517543
_(optional)_

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,23 @@ interface EndpointDefinitionWithQuery<
4444
*
4545
* const api = createApi({
4646
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
47+
* tagTypes: ['Post'],
4748
* endpoints: (build) => ({
4849
* getPosts: build.query<PostsResponse, void>({
4950
* // highlight-start
5051
* query: () => 'posts',
5152
* // highlight-end
52-
* })
53+
* }),
54+
* addPost: build.mutation<Post, Partial<Post>>({
55+
* // highlight-start
56+
* query: (body) => ({
57+
* url: `posts`,
58+
* method: 'POST',
59+
* body,
60+
* }),
61+
* // highlight-end
62+
* invalidatesTags: [{ type: 'Post', id: 'LIST' }],
63+
* }),
5364
* })
5465
* })
5566
* ```
@@ -433,20 +444,37 @@ export interface QueryExtraOptions<
433444
* with `serializeQueryArgs` returning a fixed cache key and a `merge` callback
434445
* set to add incoming data to the cache entry each time.
435446
*
436-
* Example:
447+
* @example
437448
*
438449
* ```ts
439-
* forceRefetch({currentArg, previousArg}) {
440-
* // Assume these are page numbers
441-
* return currentArg !== previousArg
442-
* },
443-
* serializeQueryArgs({endpointName}) {
444-
* return endpointName
445-
* },
446-
* merge(currentCacheData, responseData) {
447-
* currentCacheData.push(...responseData)
450+
* // codeblock-meta title="forceRefresh: pagination"
451+
*
452+
* import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
453+
* interface Post {
454+
* id: number
455+
* name: string
448456
* }
449457
*
458+
* createApi({
459+
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
460+
* endpoints: (build) => ({
461+
* listItems: build.query<string[], number>({
462+
* query: (pageNumber) => `/listItems?page=${pageNumber}`,
463+
* // Only have one cache entry because the arg always maps to one string
464+
* serializeQueryArgs: ({ endpointName }) => {
465+
* return endpointName
466+
* },
467+
* // Always merge incoming data to the cache entry
468+
* merge: (currentCache, newItems) => {
469+
* currentCache.push(...newItems)
470+
* },
471+
* // Refetch when the page arg changes
472+
* forceRefetch({ currentArg, previousArg }) {
473+
* return currentArg !== previousArg
474+
* },
475+
* }),
476+
* }),
477+
*})
450478
* ```
451479
*/
452480
forceRefetch?(params: {

0 commit comments

Comments
 (0)