@@ -39,8 +39,8 @@ import type { Pokemon } from './types'
3939export 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
152156export type QueryDefinition <
153157 QueryArg ,
@@ -220,8 +224,70 @@ 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. This means the page param is what will be passed to your ` query ` or ` queryFn ` methods.
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+ export type InfiniteQueryDefinition <
242+ QueryArg ,
243+ PageParam ,
244+ BaseQuery extends BaseQueryFn ,
245+ TagTypes extends string ,
246+ ResultType ,
247+ ReducerPath extends string = string ,
248+ > =
249+ // 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 >> & {
253+ /**
254+ * Required options to configure the infinite query behavior.
255+ * `initialPageParam` and `getNextPageParam` are required, to
256+ * ensure the infinite query can properly fetch the next page of data.
257+ * `initialPageparam` may be specified when using the
258+ * endpoint, to override the default value.
259+ */
260+ infiniteQueryOptions: {
261+ /**
262+ * The initial page parameter to use for the first page fetch.
263+ */
264+ initialPageParam: PageParam
265+ /**
266+ * If specified, only keep this many pages in cache at once.
267+ * If additional pages are fetched, older pages in the other
268+ * direction will be dropped from the cache.
269+ */
270+ maxPages? : number
271+ /**
272+ * This function can be set to automatically get the previous cursor for infinite queries.
273+ * The result will also be used to determine the value of `hasPreviousPage`.
274+ */
275+ getPreviousPageParam? : PageParamFunction <DataType , PageParam >
276+ /**
277+ * This function is required to automatically get the next cursor for infinite queries.
278+ * The result will also be used to determine the value of `hasNextPage`.
279+ */
280+ getNextPageParam: PageParamFunction <DataType , PageParam >
281+ }
282+ }
283+ ` ` `
284+
223285#### Mutation endpoint definition
224286
287+ Mutation endpoints (defined with build.mutation() ` ) are used to send updates to the server , and force invalidation and refetching of query endpoints .
288+
289+ As with queries , you must specify either the ` query ` option or the ` queryFn ` async method .
290+
225291` ` ` ts title="Mutation endpoint definition" no-transpile
226292export type MutationDefinition<
227293 QueryArg,
@@ -446,7 +512,7 @@ export interface BaseQueryApi {
446512}
447513` ` `
448514
449- #### queryFn function arguments
515+ #### ` queryFn ` function arguments
450516
451517- ` args ` - The argument provided when the query itself is called
452518- ` api ` - The ` BaseQueryApi ` object , containing ` signal ` , ` dispatch ` and ` getState ` properties
@@ -458,6 +524,24 @@ export interface BaseQueryApi {
458524
459525[examples ](docblock : // query/endpointDefinitions.ts?token=EndpointDefinitionWithQueryFn.queryFn)
460526
527+ ### ` infiniteQueryOptions `
528+
529+ _ (only for ` infiniteQuery ` endpoints )_
530+
531+ [summary ](docblock : // query/endpointDefinitions.ts?token=InfiniteQueryExtraOptions.infiniteQueryOptions)
532+
533+ The ` infiniteQueryOptions ` field includes :
534+
535+ - ` initialPageParam ` : the default page param value used for the first request , if this was not specified at the usage site
536+ - ` maxPages ` : an optional limit to how many fetched pages will be kept in the cache entry at a time
537+ - ` getNextPageParam ` : a required callback you must provide to calculate the next page param , given the existing cached pages and page params
538+ - ` getPreviousPageParam ` : an optional callback that will be used to calculate the previous page param , if you try to fetch backwards.
539+
540+ Both ` initialPageParam ` and ` getNextPageParam ` are required , to
541+ ensure the infinite query can properly fetch the next page of data.Also, ` initialPageParam ` may be specified when using the endpoint , to override the default value. ` maxPages ` and ` getPreviousPageParam ` are both optional.
542+
543+ [examples ](docblock : // query/endpointDefinitions.ts?token=InfiniteQueryExtraOptions.infiniteQueryOptions)
544+
461545### ` transformResponse `
462546
463547_ (optional , not applicable with ` queryFn ` )_
0 commit comments