Skip to content

Commit 09e1466

Browse files
markeriksonriqtsaryaemami59remus-selea
authored
RTKQ Infinite Query integration (#4738)
Co-authored-by: Mark Erikson <[email protected]> Co-authored-by: Jackson <[email protected]> Co-authored-by: Arya Emami <[email protected]> Co-authored-by: Remus Selea <[email protected]>
1 parent 7897c4c commit 09e1466

File tree

88 files changed

+18590
-1030
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+18590
-1030
lines changed

.github/workflows/size.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ on:
33
pull_request:
44
branches:
55
- master
6+
- 'feature/infinite-query-integration'
67
permissions:
78
pull-requests: write
89
jobs:

docs/rtk-query/api/createApi.mdx

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import type { Pokemon } from './types'
3939
export const pokemonApi = createApi({
4040
reducerPath: 'pokemonApi',
4141
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
42-
endpoints: (builder) => ({
43-
getPokemonByName: builder.query<Pokemon, string>({
42+
endpoints: (build) => ({
43+
getPokemonByName: build.query<Pokemon, string>({
4444
query: (name) => `pokemon/${name}`,
4545
}),
4646
}),
@@ -148,6 +148,10 @@ See [Endpoint Definition Parameters](#endpoint-definition-parameters) for detail
148148
149149
#### Query endpoint definition
150150
151+
Query endpoints (defined with `build.query()`) are used to cache data fetched from the server.
152+
153+
You must specify either a `query` field (which will use the API's `baseQuery` to make a request), or a `queryFn` function with your own async logic. All other fields are optional.
154+
151155
```ts title="Query endpoint definition" no-transpile
152156
export type QueryDefinition<
153157
QueryArg,
@@ -220,8 +224,80 @@ export type QueryDefinition<
220224
}
221225
```
222226
227+
#### Infinite Query endpoint definition
228+
229+
Infinite query endpoints (defined with `build.infiniteQuery()`) are used to cache multi-page data sets from the server. They have all the same callbacks and options as standard query endpoints, but also require an additional [`infiniteQueryOptions`](#infinitequeryoptions) field to specify how to calculate the unique parameters to fetch each page.
230+
231+
For infinite query endpoints, there is a separation between the "query arg" used for the cache key, and the "page param" used to fetch a specific page. For example, a Pokemon API endpoint might have a string query arg like `"fire"` , but use a page number as the param to determine which page to fetch out of the results. The `query` and `queryFn` methods will receive a combined `{queryArg, pageParam}` object as the argument, rather than just the `queryArg` by itself.
232+
233+
```ts title="Infinite Query endpoint definition" no-transpile
234+
export type PageParamFunction<DataType, PageParam> = (
235+
firstPage: DataType,
236+
allPages: Array<DataType>,
237+
firstPageParam: PageParam,
238+
allPageParams: Array<PageParam>,
239+
) => PageParam | undefined | null
240+
241+
type InfiniteQueryCombinedArg<QueryArg, PageParam> = {
242+
queryArg: QueryArg
243+
pageParam: PageParam
244+
}
245+
246+
export type InfiniteQueryDefinition<
247+
QueryArg,
248+
PageParam,
249+
BaseQuery extends BaseQueryFn,
250+
TagTypes extends string,
251+
ResultType,
252+
ReducerPath extends string = string,
253+
> =
254+
// Infinite queries have all the same options as query endpoints,
255+
// but store the `{pages, pageParams}` structure, and receive an object
256+
// with both `{queryArg, pageParam}` as the arg for `query` and `queryFn`.
257+
QueryDefinition<
258+
InfiniteQueryCombinedArg<QueryArg, PageParam>,
259+
BaseQuery,
260+
TagTypes,
261+
InfiniteData<ResultType>
262+
> & {
263+
/**
264+
* Required options to configure the infinite query behavior.
265+
* `initialPageParam` and `getNextPageParam` are required, to
266+
* ensure the infinite query can properly fetch the next page of data.
267+
* `initialPageparam` may be specified when using the
268+
* endpoint, to override the default value.
269+
*/
270+
infiniteQueryOptions: {
271+
/**
272+
* The initial page parameter to use for the first page fetch.
273+
*/
274+
initialPageParam: PageParam
275+
/**
276+
* This function is required to automatically get the next cursor for infinite queries.
277+
* The result will also be used to determine the value of `hasNextPage`.
278+
*/
279+
getNextPageParam: PageParamFunction<DataType, PageParam>
280+
/**
281+
* This function can be set to automatically get the previous cursor for infinite queries.
282+
* The result will also be used to determine the value of `hasPreviousPage`.
283+
*/
284+
getPreviousPageParam?: PageParamFunction<DataType, PageParam>
285+
/**
286+
* If specified, only keep this many pages in cache at once.
287+
* If additional pages are fetched, older pages in the other
288+
* direction will be dropped from the cache.
289+
*/
290+
maxPages?: number
291+
}
292+
}
293+
```
294+
223295
#### Mutation endpoint definition
224296
297+
Mutation endpoints (defined with `build.mutation()`) are used to send updates to the server, and force invalidation and refetching of query endpoints.
298+
299+
As with queries, you must specify either the `query` option or the `queryFn` async method.
300+
225301
```ts title="Mutation endpoint definition" no-transpile
226302
export type MutationDefinition<
227303
QueryArg,
@@ -446,7 +522,7 @@ export interface BaseQueryApi {
446522
}
447523
```
448524

449-
#### queryFn function arguments
525+
#### `queryFn` function arguments
450526

451527
- `args` - The argument provided when the query itself is called
452528
- `api` - The `BaseQueryApi` object, containing `signal`, `dispatch` and `getState` properties
@@ -458,6 +534,21 @@ export interface BaseQueryApi {
458534

459535
[examples](docblock://query/endpointDefinitions.ts?token=EndpointDefinitionWithQueryFn.queryFn)
460536

537+
### `infiniteQueryOptions`
538+
539+
_(only for `infiniteQuery` endpoints)_
540+
541+
[summary](docblock://query/endpointDefinitions.ts?token=InfiniteQueryExtraOptions.infiniteQueryOptions)
542+
543+
The `infiniteQueryOptions` field includes:
544+
545+
- `initialPageParam`: the default page param value used for the first request, if this was not specified at the usage site
546+
- `getNextPageParam`: a required callback you must provide to calculate the next page param, given the existing cached pages and page params
547+
- `getPreviousPageParam`: an optional callback that will be used to calculate the previous page param, if you try to fetch backwards.
548+
- `maxPages`: an optional limit to how many fetched pages will be kept in the cache entry at a time
549+
550+
[examples](docblock://query/endpointDefinitions.ts?token=InfiniteQueryExtraOptions.infiniteQueryOptions)
551+
461552
### `transformResponse`
462553

463554
_(optional, not applicable with `queryFn`)_

docs/rtk-query/api/created-api/code-splitting.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
7676

7777
export const api = createApi({
7878
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
79-
endpoints: (builder) => ({
80-
getUserByUserId: builder.query({
79+
endpoints: (build) => ({
80+
getUserByUserId: build.query({
8181
query() {
8282
return ''
8383
},
8484
}),
85-
patchUserByUserId: builder.mutation({
85+
patchUserByUserId: build.mutation({
8686
query() {
8787
return ''
8888
},
8989
}),
90-
getUsers: builder.query({
90+
getUsers: build.query({
9191
query() {
9292
return ''
9393
},

0 commit comments

Comments
 (0)