Skip to content

Commit 25e7779

Browse files
committed
Document combined infinite query arg
1 parent 4df2ca8 commit 25e7779

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export type QueryDefinition<
228228
229229
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.
230230
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. This means the page param is what will be passed to your `query` or `queryFn` methods.
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.
232232
233233
```ts title="Infinite Query endpoint definition" no-transpile
234234
export type PageParamFunction<DataType, PageParam> = (
@@ -238,6 +238,11 @@ export type PageParamFunction<DataType, PageParam> = (
238238
allPageParams: Array<PageParam>,
239239
) => PageParam | undefined | null
240240

241+
type InfiniteQueryCombinedArg<QueryArg, PageParam> = {
242+
queryArg: QueryArg
243+
pageParam: PageParam
244+
}
245+
241246
export type InfiniteQueryDefinition<
242247
QueryArg,
243248
PageParam,
@@ -247,9 +252,14 @@ export type InfiniteQueryDefinition<
247252
ReducerPath extends string = string,
248253
> =
249254
// Infinite queries have all the same options as query endpoints,
250-
// but store the `{data, pages}` structure, and use the
251-
// `PageParam` type as the `QueryArg` for fetching.
252-
QueryDefinition<PageParam, BaseQuery, TagTypes, InfiniteData<ResultType>> & {
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+
> & {
253263
/**
254264
* Required options to configure the infinite query behavior.
255265
* `initialPageParam` and `getNextPageParam` are required, to

docs/rtk-query/usage/infinite-queries.mdx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ With standard query endpoints:
3131
Infinite queries work similarly, but have a couple additional layers:
3232

3333
- You still specify a "query arg", which is still used to generate the unique cache key for this specific cache entry
34-
- However, there is a separation between the "query arg" used for the cache key, and the "page param" used to fetch a specific page. This means the page param is what will be passed to your `query` or `queryFn` methods.
34+
- However, there is a separation between the "query arg" used for the cache key, and the "page param" used to fetch a specific page. Since both are useful for determining what to fetch, your `query` and `queryFn` methods will receive a combined object with `{queryArg, pageParam}` as the first argument, instead of just the `queryArg` by itself.
3535
- The `data` field in the cache entry stores a `{pages: DataType[], pageParams: PageParam[]}` structure that contains _all_ of the fetched page results and their corresponding page params used to fetch them.
3636

3737
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. For a query like `useGetPokemonInfiniteQuery('fire')`, the resulting cache data might look like this:
@@ -100,7 +100,7 @@ If there is no possible page to fetch in that direction, the callback should ret
100100
101101
### Infinite Query Definition Example
102102
103-
A complete example of this might look like:
103+
A complete example of this for a fictional Pokemon API service might look like:
104104
105105
```ts no-transpile title="Infinite Query definition example"
106106
type Pokemon = {
@@ -109,11 +109,12 @@ type Pokemon = {
109109
}
110110

111111
const pokemonApi = createApi({
112-
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
112+
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com/pokemon' }),
113113
endpoints: (build) => ({
114+
// 3 TS generics: page contents, query arg, page param
114115
getInfinitePokemonWithMax: build.infiniteQuery<Pokemon[], string, number>({
115116
infiniteQueryOptions: {
116-
initialPageParam: 0,
117+
initialPageParam: 1,
117118
maxPages: 3,
118119
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
119120
lastPageParam + 1,
@@ -126,8 +127,9 @@ const pokemonApi = createApi({
126127
return firstPageParam > 0 ? firstPageParam - 1 : undefined
127128
},
128129
},
129-
query(pageParam) {
130-
return `https://example.com/listItems?page=${pageParam}`
130+
// The `query` function receives `{queryArg, pageParam}` as its argument
131+
query({ queryArg, pageParam }) {
132+
return `/type/${queryArg}?page=${pageParam}`
131133
},
132134
}),
133135
}),
@@ -192,16 +194,16 @@ type Pokemon = {
192194
}
193195

194196
const pokemonApi = createApi({
195-
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
197+
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com/pokemon' }),
196198
endpoints: (build) => ({
197199
getPokemon: build.infiniteQuery<Pokemon[], string, number>({
198200
infiniteQueryOptions: {
199201
initialPageParam: 0,
200202
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
201203
lastPageParam + 1,
202204
},
203-
query(pageParam) {
204-
return `https://example.com/listItems?page=${pageParam}`
205+
query({ queryArg, pageParam }) {
206+
return `/type/${queryArg}?page=${pageParam}`
205207
},
206208
}),
207209
}),

0 commit comments

Comments
 (0)