Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
dc3b32e
Added rough draft of initial endpoint definition for infinite query
riqts Mar 1, 2024
05a3457
Added placeholder select and initiate functions where the merge and q…
riqts Mar 1, 2024
0f70de3
Theoretical slice added to buildSlice to potentially handle changing …
riqts Mar 1, 2024
cba2d47
Added new endpointDefinition to createApi + module
riqts Mar 1, 2024
4c32ec3
Added infiniteQueryThunks to core module file
riqts Mar 6, 2024
d3008d4
Initial implementation of executeEndpoint/Thunk with infiniteQuery
riqts Mar 6, 2024
d942448
Extended infiniteQueryInitiate, added fetchNextPage and fetchPrevious…
riqts Mar 6, 2024
638244e
Added types for InfiniteQueryConfig
riqts Mar 6, 2024
66e5f65
Shelf
riqts Mar 20, 2024
4ca5764
Built out initial react hook definitions for InfiniteQuery
riqts Apr 23, 2024
dd13f5b
Added InfiniteQuerySubState with param and direction for slice operat…
riqts May 4, 2024
3d75c35
Set up Test Hook to actually test if next page gets data
riqts May 4, 2024
0592827
Introduced correct typing and infiniteQuery hooks into buildHooks.ts
riqts May 4, 2024
f79c7c7
Initiate handling and types for infiniteQuery
riqts May 4, 2024
e000b6d
Added InfiniteQuery Selectors types for the hook
riqts May 4, 2024
435ad47
Added necessary slice actions for inf query substate
riqts May 4, 2024
68c1fb3
Forced into the thunk working InfQuery handling for api proof of concept
riqts May 4, 2024
5fa3afa
Altered EndPointDefinition for type inference
riqts May 4, 2024
46587cb
Index has inf query type
riqts May 4, 2024
41020d5
Cleanup imports from first pass
riqts May 4, 2024
4559b70
add inf query definition option for module.ts
riqts May 4, 2024
c461f4e
Revert "Shelf"
riqts May 4, 2024
0284e6b
Force selector type for infinite query test
riqts May 5, 2024
d4cf9ce
Pass through infinite query options
markerikson Sep 29, 2024
f533b38
Fix remaining TS issues
markerikson Sep 29, 2024
972becd
Hack around TS issues
markerikson Sep 29, 2024
66966e3
Try to fix exported types
markerikson Sep 29, 2024
a159f27
Pass through correct data types for infinite queries
markerikson Oct 22, 2024
462af2c
Add an initial test for infinite query thunks
markerikson Oct 22, 2024
a80d541
Add `serializeQueryArgs` type support
markerikson Oct 23, 2024
e5b0c56
Restructure infinite fetching logic to work with query or queryFn
markerikson Oct 24, 2024
b60fb44
Export InfiniteData for type portability
markerikson Oct 24, 2024
995bfce
Separate QueryArg and PageParam types for infinite queries
markerikson Oct 24, 2024
18aca2b
Require initialPageParam value
markerikson Oct 24, 2024
2c4a5e2
Add type tests for infinite query endpoints
markerikson Oct 27, 2024
12b9c02
Fix type imports
markerikson Oct 27, 2024
5174428
Fix infinite query hook data types
markerikson Oct 27, 2024
5bf0e46
Fix useInfiniteQuery hook types and trigger behavior
markerikson Oct 27, 2024
cb5c70a
Fix exports
aryaemami59 Oct 27, 2024
24427f8
Fix duplicate types
aryaemami59 Oct 27, 2024
95f153d
Fix TS issue caused by duplicate types in emitted type definitions
aryaemami59 Oct 27, 2024
6e0cb94
Pass through `initialPageParam` on thunk dispatch
markerikson Oct 27, 2024
cf74977
Change 'backwards' to 'backward'
markerikson Oct 27, 2024
88c823b
Test getPreviousParam behavior
markerikson Oct 27, 2024
e9e8ecf
Separate out useInfiniteQuery tests
markerikson Oct 27, 2024
3cb0613
Only pass initialPageParams option through query hook
markerikson Oct 27, 2024
a87a0f3
Fix bad rebase of exports
markerikson Nov 29, 2024
de067a4
Add RTL/react-render-stream
markerikson Nov 29, 2024
e634241
Rewrite infinite query test to use render-stream
markerikson Nov 29, 2024
a89883b
Add basic maxPages support
markerikson Nov 29, 2024
aa7588a
Add runtime checks for maxPages values
markerikson Nov 29, 2024
36a22d6
Text maxPages behavior
markerikson Nov 29, 2024
ee68cb2
Fix refetching behavior for infinite queries
markerikson Nov 29, 2024
b0d0aba
Add RTKQ infinite query example app (#4744)
markerikson Dec 1, 2024
b8e7131
Implement infinite query status flags (#4771)
markerikson Dec 15, 2024
dd975c5
Fix infinite query fetching when `refetchOnMountOrArgChange` is true …
markerikson Dec 30, 2024
d64d51a
Add infinite query examples (#4775)
remus-selea Dec 31, 2024
59e807a
Fix updateQueryData for infinite queries (#4796)
markerikson Dec 31, 2024
e3d337b
Declare types for autogenerated infinite query hooks (#4797)
markerikson Dec 31, 2024
e409c86
Expose `refresh` from infinite query hooks, and add RN FlatList examp…
markerikson Dec 31, 2024
35af2bf
Byte-shave and deduplicate infinite query implementation (#4804)
markerikson Jan 5, 2025
6fe387c
Fix infinite query lifecycle callback data types (#4818)
markerikson Jan 16, 2025
2a0cd70
Allow infinite query invalidation and promise checks (#4821)
markerikson Jan 18, 2025
ca39333
Add docs for infinite queries (#4820)
markerikson Jan 20, 2025
840a1e7
Remove dead slice code and add transformResponse test (#4823)
markerikson Jan 20, 2025
91f2e1b
Nest `{queryArg, pageParam}` for infinite queries (#4826)
markerikson Jan 23, 2025
2aca4f2
Update infinite query example with page param changes (#4827)
markerikson Jan 23, 2025
167e4cb
Rework infinite query forced checks (#4856)
markerikson Feb 23, 2025
8503c22
Final infinite query PR cleanup (#4857)
markerikson Feb 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
pull_request:
branches:
- master
- 'feature/infinite-query-integration'
permissions:
pull-requests: write
jobs:
Expand Down
97 changes: 94 additions & 3 deletions docs/rtk-query/api/createApi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import type { Pokemon } from './types'
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemonByName: builder.query<Pokemon, string>({
endpoints: (build) => ({
getPokemonByName: build.query<Pokemon, string>({
query: (name) => `pokemon/${name}`,
}),
}),
Expand Down Expand Up @@ -148,6 +148,10 @@ See [Endpoint Definition Parameters](#endpoint-definition-parameters) for detail

#### Query endpoint definition

Query endpoints (defined with `build.query()`) are used to cache data fetched from the server.

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.

```ts title="Query endpoint definition" no-transpile
export type QueryDefinition<
QueryArg,
Expand Down Expand Up @@ -220,8 +224,80 @@ export type QueryDefinition<
}
```

#### Infinite Query endpoint definition

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.

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.

```ts title="Infinite Query endpoint definition" no-transpile
export type PageParamFunction<DataType, PageParam> = (
firstPage: DataType,
allPages: Array<DataType>,
firstPageParam: PageParam,
allPageParams: Array<PageParam>,
) => PageParam | undefined | null

type InfiniteQueryCombinedArg<QueryArg, PageParam> = {
queryArg: QueryArg
pageParam: PageParam
}

export type InfiniteQueryDefinition<
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
TagTypes extends string,
ResultType,
ReducerPath extends string = string,
> =
// Infinite queries have all the same options as query endpoints,
// but store the `{pages, pageParams}` structure, and receive an object
// with both `{queryArg, pageParam}` as the arg for `query` and `queryFn`.
QueryDefinition<
InfiniteQueryCombinedArg<QueryArg, PageParam>,
BaseQuery,
TagTypes,
InfiniteData<ResultType>
> & {
/**
* Required options to configure the infinite query behavior.
* `initialPageParam` and `getNextPageParam` are required, to
* ensure the infinite query can properly fetch the next page of data.
* `initialPageparam` may be specified when using the
* endpoint, to override the default value.
*/
infiniteQueryOptions: {
/**
* The initial page parameter to use for the first page fetch.
*/
initialPageParam: PageParam
/**
* This function is required to automatically get the next cursor for infinite queries.
* The result will also be used to determine the value of `hasNextPage`.
*/
getNextPageParam: PageParamFunction<DataType, PageParam>
/**
* This function can be set to automatically get the previous cursor for infinite queries.
* The result will also be used to determine the value of `hasPreviousPage`.
*/
getPreviousPageParam?: PageParamFunction<DataType, PageParam>
/**
* If specified, only keep this many pages in cache at once.
* If additional pages are fetched, older pages in the other
* direction will be dropped from the cache.
*/
maxPages?: number
}
}
```

#### Mutation endpoint definition

Mutation endpoints (defined with `build.mutation()`) are used to send updates to the server, and force invalidation and refetching of query endpoints.

As with queries, you must specify either the `query` option or the `queryFn` async method.

```ts title="Mutation endpoint definition" no-transpile
export type MutationDefinition<
QueryArg,
Expand Down Expand Up @@ -446,7 +522,7 @@ export interface BaseQueryApi {
}
```

#### queryFn function arguments
#### `queryFn` function arguments

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

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

### `infiniteQueryOptions`

_(only for `infiniteQuery` endpoints)_

[summary](docblock://query/endpointDefinitions.ts?token=InfiniteQueryExtraOptions.infiniteQueryOptions)

The `infiniteQueryOptions` field includes:

- `initialPageParam`: the default page param value used for the first request, if this was not specified at the usage site
- `getNextPageParam`: a required callback you must provide to calculate the next page param, given the existing cached pages and page params
- `getPreviousPageParam`: an optional callback that will be used to calculate the previous page param, if you try to fetch backwards.
- `maxPages`: an optional limit to how many fetched pages will be kept in the cache entry at a time

[examples](docblock://query/endpointDefinitions.ts?token=InfiniteQueryExtraOptions.infiniteQueryOptions)

### `transformResponse`

_(optional, not applicable with `queryFn`)_
Expand Down
8 changes: 4 additions & 4 deletions docs/rtk-query/api/created-api/code-splitting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

export const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
endpoints: (builder) => ({
getUserByUserId: builder.query({
endpoints: (build) => ({
getUserByUserId: build.query({
query() {
return ''
},
}),
patchUserByUserId: builder.mutation({
patchUserByUserId: build.mutation({
query() {
return ''
},
}),
getUsers: builder.query({
getUsers: build.query({
query() {
return ''
},
Expand Down
Loading
Loading