Skip to content

Commit 8503c22

Browse files
authored
Final infinite query PR cleanup (#4857)
1 parent 167e4cb commit 8503c22

File tree

35 files changed

+509
-213
lines changed

35 files changed

+509
-213
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,28 +273,28 @@ export type InfiniteQueryDefinition<
273273
*/
274274
initialPageParam: PageParam
275275
/**
276-
* If specified, only keep this many pages in cache at once.
277-
* If additional pages are fetched, older pages in the other
278-
* direction will be dropped from the cache.
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`.
279278
*/
280-
maxPages?: number
279+
getNextPageParam: PageParamFunction<DataType, PageParam>
281280
/**
282281
* This function can be set to automatically get the previous cursor for infinite queries.
283282
* The result will also be used to determine the value of `hasPreviousPage`.
284283
*/
285284
getPreviousPageParam?: PageParamFunction<DataType, PageParam>
286285
/**
287-
* This function is required to automatically get the next cursor for infinite queries.
288-
* The result will also be used to determine the value of `hasNextPage`.
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.
289289
*/
290-
getNextPageParam: PageParamFunction<DataType, PageParam>
290+
maxPages?: number
291291
}
292292
}
293293
```
294294
295295
#### Mutation endpoint definition
296296
297-
Mutation endpoints (defined with build.mutation()`) are used to send updates to the server, and force invalidation and refetching of query endpoints.
297+
Mutation endpoints (defined with `build.mutation()`) are used to send updates to the server, and force invalidation and refetching of query endpoints.
298298
299299
As with queries, you must specify either the `query` option or the `queryFn` async method.
300300
@@ -543,12 +543,9 @@ _(only for `infiniteQuery` endpoints)_
543543
The `infiniteQueryOptions` field includes:
544544

545545
- `initialPageParam`: the default page param value used for the first request, if this was not specified at the usage site
546-
- `maxPages`: an optional limit to how many fetched pages will be kept in the cache entry at a time
547546
- `getNextPageParam`: a required callback you must provide to calculate the next page param, given the existing cached pages and page params
548547
- `getPreviousPageParam`: an optional callback that will be used to calculate the previous page param, if you try to fetch backwards.
549-
550-
Both `initialPageParam` and `getNextPageParam` are required, to
551-
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.
548+
- `maxPages`: an optional limit to how many fetched pages will be kept in the cache entry at a time
552549

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

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
},

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ hide_title: true
1111

1212
## Hooks Overview
1313

14-
The core RTK Query `createApi` method is UI-agnostic, in the same way that the Redux core library and Redux Toolkit are UI-agnostic. They are all plain JS logic that can be used anywhere.
14+
The core RTK Query `createApi` method is UI-agnostic, in the same way that the Redux core library and Redux Toolkit are UI-agnostic. They are all plain JS logic that can be used anywhere. So, if you import `createApi` from `'@reduxjs/toolkit/query'`, it does not have any specific UI integrations included.
1515

16-
However, RTK Query also provides the ability to auto-generate React hooks for each of your endpoints. Since this specifically depends on React itself, RTK Query provides an alternate entry point that exposes a customized version of `createApi` that includes that functionality:
16+
However, RTK Query also provides the ability to auto-generate React hooks for each of your endpoints. Since this specifically depends on React itself, RTK Query provides an additional entry point that exposes a customized version of `createApi` that includes that functionality:
1717

1818
```ts no-transpile
1919
import { createApi } from '@reduxjs/toolkit/query/react'
2020
```
2121

22-
If you have used the React-specific version of `createApi`, the generated `api` slice structure will also contain a set of React hooks. The primary endpoint hooks are available as `api.endpoints[endpointName].useQuery` or `api.endpoints[endpointName].useMutation`, matching how you defined that endpoint.
22+
If you have used the React-specific version of `createApi`, the generated `api` slice structure will also contain a set of React hooks. The primary endpoint hooks are available as `api.endpoints[endpointName].useQuery`, `api.endpoints[endpointName].useMutation`, and `api.endpoints[endpointName].useInfiniteQuery`, matching how you defined that endpoint.
2323

2424
### Generated Hook Names
2525

@@ -37,15 +37,15 @@ const { data } = api.useGetPostsQuery()
3737
const [updatePost, { data }] = api.useUpdatePostMutation()
3838
```
3939

40-
The general format is `use(Endpointname)(Query|Mutation)` - `use` is prefixed, the first letter of your endpoint name is capitalized, then `Query` or `Mutation` is appended depending on the type.
40+
The general format is `use(Endpointname)(Query|Mutation|InfiniteQuery)` - `use` is prefixed, the first letter of your endpoint name is capitalized, then `Query` or `Mutation` or `InfiniteQuery` is appended depending on the type.
4141

4242
### Available Hooks
4343

4444
RTK Query provides additional hooks for more advanced use-cases, although not all are generated directly on the `api` object as well.
4545

4646
Most of the hooks are generated on a per-endpoint basis.
4747

48-
The full list of hooks generated in the React-specific version of `createApi` is as follows:
48+
The full list of hooks generated in the React-specific version of `createApi` is:
4949

5050
- Endpoint-specific, generated the `api` object with a unique name and on the endpoint object with a generic name:
5151
- [`useQuery`](#usequery) (all standard queries)
@@ -54,10 +54,10 @@ The full list of hooks generated in the React-specific version of `createApi` is
5454
- [`useLazyQuery`](#uselazyquery) (all standard queries)
5555
- Endpoint-specific, only generated on the endpoint object with a generic name:
5656
- [`useQueryState`](#usequerystate)
57-
- [`useQuerySubscription](#usequerysubscription)
58-
- [`useLazyQuerySubscription](#uselazyquerysubscription)
57+
- [`useQuerySubscription`](#usequerysubscription)
58+
- [`useLazyQuerySubscription`](#uselazyquerysubscription)
5959
- [`useInfiniteQueryState`](#useinfinitequerystate)
60-
- [`useInfiniteQuerySubscription](#useinfinitequerysubscription)
60+
- [`useInfiniteQuerySubscription`](#useinfinitequerysubscription)
6161
- Endpoint-agnostic, generated on the `api` object:
6262
- [`usePrefetch`](#useprefetch)
6363

docs/rtk-query/api/fetchBaseQuery.mdx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
2626
export const pokemonApi = createApi({
2727
// Set the baseUrl for every endpoint below
2828
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
29-
endpoints: (builder) => ({
30-
getPokemonByName: builder.query({
29+
endpoints: (build) => ({
30+
getPokemonByName: build.query({
3131
// Will make a request like https://pokeapi.co/api/v2/pokemon/bulbasaur
3232
query: (name: string) => `pokemon/${name}`,
3333
}),
34-
updatePokemon: builder.mutation({
34+
updatePokemon: build.mutation({
3535
query: ({ name, patch }) => ({
3636
url: `pokemon/${name}`,
3737
// When performing a mutation, you typically use a method of
@@ -260,8 +260,8 @@ By default, `fetchBaseQuery` assumes that every request you make will be `json`,
260260

261261
```ts no-transpile
262262
// omitted
263-
endpoints: (builder) => ({
264-
updateUser: builder.query({
263+
endpoints: (build) => ({
264+
updateUser: build.query({
265265
query: (user: Record<string, string>) => ({
266266
url: `users`,
267267
method: 'PUT',
@@ -274,8 +274,8 @@ By default, `fetchBaseQuery` assumes that every request you make will be `json`,
274274

275275
```ts no-transpile
276276
// omitted
277-
endpoints: (builder) => ({
278-
updateUser: builder.query({
277+
endpoints: (build) => ({
278+
updateUser: build.query({
279279
query: (user: Record<string, string>) => ({
280280
url: `users`,
281281
method: 'PUT',
@@ -296,8 +296,8 @@ By default, `fetchBaseQuery` assumes that every request you make will be `json`,
296296

297297
```ts no-transpile
298298
// omitted
299-
endpoints: (builder) => ({
300-
updateUser: builder.query({
299+
endpoints: (build) => ({
300+
updateUser: build.query({
301301
query: (user: Record<string, string>) => ({
302302
url: `users`,
303303
// Assuming no `paramsSerializer` is specified, the user object is automatically converted
@@ -328,8 +328,8 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
328328
329329
export const customApi = createApi({
330330
baseQuery: fetchBaseQuery({ baseUrl: '/api/' }),
331-
endpoints: (builder) => ({
332-
getUsers: builder.query({
331+
endpoints: (build) => ({
332+
getUsers: build.query({
333333
query: () => ({
334334
url: `users`,
335335
// This is the same as passing 'text'
@@ -365,8 +365,8 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
365365
export const customApi = createApi({
366366
// Set the baseUrl for every endpoint below
367367
baseQuery: fetchBaseQuery({ baseUrl: '/api/' }),
368-
endpoints: (builder) => ({
369-
getUsers: builder.query({
368+
endpoints: (build) => ({
369+
getUsers: build.query({
370370
query: () => ({
371371
url: `users`,
372372
// Example: we have a backend API always returns a 200,
@@ -389,8 +389,8 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
389389
export const api = createApi({
390390
// Set a default timeout of 10 seconds
391391
baseQuery: fetchBaseQuery({ baseUrl: '/api/', timeout: 10000 }),
392-
endpoints: (builder) => ({
393-
getUsers: builder.query({
392+
endpoints: (build) => ({
393+
getUsers: build.query({
394394
query: () => ({
395395
url: `users`,
396396
// Example: we know the users endpoint is _really fast_ because it's always cached.

docs/rtk-query/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ import type { Pokemon } from './types'
121121
export const pokemonApi = createApi({
122122
reducerPath: 'pokemonApi',
123123
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
124-
endpoints: (builder) => ({
125-
getPokemonByName: builder.query<Pokemon, string>({
124+
endpoints: (build) => ({
125+
getPokemonByName: build.query<Pokemon, string>({
126126
query: (name) => `pokemon/${name}`,
127127
}),
128128
}),

docs/rtk-query/usage-with-typescript.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ import type { Pokemon } from './types'
5151
export const pokemonApi = createApi({
5252
reducerPath: 'pokemonApi',
5353
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
54-
endpoints: (builder) => ({
55-
getPokemonByName: builder.query<Pokemon, string>({
54+
endpoints: (build) => ({
55+
getPokemonByName: build.query<Pokemon, string>({
5656
query: (name) => `pokemon/${name}`,
5757
}),
5858
}),
@@ -78,8 +78,8 @@ import type { Pokemon } from './types'
7878
export const pokemonApi = createApi({
7979
reducerPath: 'pokemonApi',
8080
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
81-
endpoints: (builder) => ({
82-
getPokemonByName: builder.query<Pokemon, string>({
81+
endpoints: (build) => ({
82+
getPokemonByName: build.query<Pokemon, string>({
8383
query: (name) => `pokemon/${name}`,
8484
}),
8585
}),
@@ -178,8 +178,8 @@ const simpleBaseQuery: BaseQueryFn<
178178

179179
const api = createApi({
180180
baseQuery: simpleBaseQuery,
181-
endpoints: (builder) => ({
182-
getSupport: builder.query({
181+
endpoints: (build) => ({
182+
getSupport: build.query({
183183
query: () => 'support me',
184184
extraOptions: {
185185
shout: true,

docs/rtk-query/usage/cache-behavior.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export const api = createApi({
100100
// global configuration for the api
101101
keepUnusedDataFor: 30,
102102
// highlight-end
103-
endpoints: (builder) => ({
104-
getPosts: builder.query<Post[], number>({
103+
endpoints: (build) => ({
104+
getPosts: build.query<Post[], number>({
105105
query: () => `posts`,
106106
// highlight-start
107107
// configuration for an individual endpoint, overriding the api setting
@@ -189,8 +189,8 @@ export const api = createApi({
189189
// global configuration for the api
190190
refetchOnMountOrArgChange: 30,
191191
// highlight-end
192-
endpoints: (builder) => ({
193-
getPosts: builder.query<Post[], number>({
192+
endpoints: (build) => ({
193+
getPosts: build.query<Post[], number>({
194194
query: () => `posts`,
195195
}),
196196
}),
@@ -241,8 +241,8 @@ export const api = createApi({
241241
// global configuration for the api
242242
refetchOnFocus: true,
243243
// highlight-end
244-
endpoints: (builder) => ({
245-
getPosts: builder.query<Post[], number>({
244+
endpoints: (build) => ({
245+
getPosts: build.query<Post[], number>({
246246
query: () => `posts`,
247247
}),
248248
}),
@@ -266,8 +266,8 @@ export const api = createApi({
266266
// global configuration for the api
267267
refetchOnFocus: true,
268268
// highlight-end
269-
endpoints: (builder) => ({
270-
getPosts: builder.query<Post[], number>({
269+
endpoints: (build) => ({
270+
getPosts: build.query<Post[], number>({
271271
query: () => `posts`,
272272
}),
273273
}),
@@ -320,8 +320,8 @@ export const api = createApi({
320320
// global configuration for the api
321321
refetchOnReconnect: true,
322322
// highlight-end
323-
endpoints: (builder) => ({
324-
getPosts: builder.query<Post[], number>({
323+
endpoints: (build) => ({
324+
getPosts: build.query<Post[], number>({
325325
query: () => `posts`,
326326
}),
327327
}),
@@ -345,8 +345,8 @@ export const api = createApi({
345345
// global configuration for the api
346346
refetchOnReconnect: true,
347347
// highlight-end
348-
endpoints: (builder) => ({
349-
getPosts: builder.query<Post[], number>({
348+
endpoints: (build) => ({
349+
getPosts: build.query<Post[], number>({
350350
query: () => `posts`,
351351
}),
352352
}),

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ export const api = createApi({
446446
baseUrl: 'https://graphqlzero.almansi.me/api',
447447
}),
448448
// highlight-end
449-
endpoints: (builder) => ({
450-
getPosts: builder.query({
449+
endpoints: (build) => ({
450+
getPosts: build.query({
451451
query: () => ({
452452
body: gql`
453453
query {
@@ -462,7 +462,7 @@ export const api = createApi({
462462
}),
463463
transformResponse: (response) => response.posts.data,
464464
}),
465-
getPost: builder.query({
465+
getPost: build.query({
466466
query: (id) => ({
467467
body: gql`
468468
query {
@@ -823,8 +823,8 @@ const dynamicBaseQuery: BaseQueryFn<
823823
824824
export const api = createApi({
825825
baseQuery: dynamicBaseQuery,
826-
endpoints: (builder) => ({
827-
getPosts: builder.query<Post[], void>({
826+
endpoints: (build) => ({
827+
getPosts: build.query<Post[], void>({
828828
query: () => 'posts',
829829
}),
830830
}),
@@ -862,8 +862,8 @@ export const api = createApi({
862862
baseQuery: graphqlBaseQuery({
863863
baseUrl: '/graphql',
864864
}),
865-
endpoints: (builder) => ({
866-
getPosts: builder.query<Post[], void>({
865+
endpoints: (build) => ({
866+
getPosts: build.query<Post[], void>({
867867
query: () => ({
868868
body: gql`
869869
query {
@@ -959,8 +959,8 @@ import { supabase } from './supabaseApi'
959959
export const supabaseApi = createApi({
960960
reducerPath: 'supabaseApi',
961961
baseQuery: fakeBaseQuery(),
962-
endpoints: (builder) => ({
963-
getBlogs: builder.query({
962+
endpoints: (build) => ({
963+
getBlogs: build.query({
964964
queryFn: async () => {
965965
// Supabase conveniently already has `data` and `error` fields
966966
const { data, error } = await supabase.from('blogs').select()

0 commit comments

Comments
 (0)