Skip to content

Commit 729a102

Browse files
committed
Code review cleanup
1 parent 469736b commit 729a102

File tree

9 files changed

+50
-63
lines changed

9 files changed

+50
-63
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/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

packages/toolkit/src/query/core/apiState.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ export type InfiniteQueryConfigOptions<DataType, PageParam> = {
4343
*/
4444
initialPageParam: PageParam
4545
/**
46-
* If specified, only keep this many pages in cache at once.
47-
* If additional pages are fetched, older pages in the other
48-
* direction will be dropped from the cache.
46+
* This function is required to automatically get the next cursor for infinite queries.
47+
* The result will also be used to determine the value of `hasNextPage`.
4948
*/
50-
maxPages?: number
49+
getNextPageParam: PageParamFunction<DataType, PageParam>
5150
/**
5251
* This function can be set to automatically get the previous cursor for infinite queries.
5352
* The result will also be used to determine the value of `hasPreviousPage`.
5453
*/
5554
getPreviousPageParam?: PageParamFunction<DataType, PageParam>
5655
/**
57-
* This function is required to automatically get the next cursor for infinite queries.
58-
* The result will also be used to determine the value of `hasNextPage`.
56+
* If specified, only keep this many pages in cache at once.
57+
* If additional pages are fetched, older pages in the other
58+
* direction will be dropped from the cache.
5959
*/
60-
getNextPageParam: PageParamFunction<DataType, PageParam>
60+
maxPages?: number
6161
}
6262

6363
export interface InfiniteData<DataType, PageParam> {

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ type StartQueryActionCreator<
9696
options?: StartQueryActionCreatorOptions,
9797
) => ThunkAction<QueryActionCreatorResult<D>, any, any, UnknownAction>
9898

99-
// placeholder type which
100-
// may attempt to derive the list of args to query in pagination
10199
export type StartInfiniteQueryActionCreator<
102100
D extends InfiniteQueryDefinition<any, any, any, any, any>,
103101
> = (

packages/toolkit/src/query/core/buildMiddleware/cacheLifecycle.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ export type CacheLifecycleQueryExtraOptions<
142142
): Promise<void> | void
143143
}
144144

145-
// copying QueryDefinition to get past initial build
146145
export type CacheLifecycleInfiniteQueryExtraOptions<
147146
ResultType,
148147
QueryArg,

packages/toolkit/src/query/core/buildMiddleware/queryLifecycle.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ export type QueryLifecycleQueryExtraOptions<
121121
): Promise<void> | void
122122
}
123123

124-
// temporarily cloned QueryOptions again to just get the definition to build for now
125124
export type QueryLifecycleInfiniteQueryExtraOptions<
126125
ResultType,
127126
QueryArg,

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ export type BaseEndpointDefinition<
219219
export enum DefinitionType {
220220
query = 'query',
221221
mutation = 'mutation',
222-
// hijacking query temporarily to get the definition to build
223222
infinitequery = 'infinitequery',
224223
}
225224

@@ -545,7 +544,6 @@ export type QueryDefinition<
545544
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType> &
546545
QueryExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>
547546

548-
// cloning Query Endpoint Definition with an extra option to begin with
549547
export interface InfiniteQueryTypes<
550548
QueryArg,
551549
PageParam,
@@ -613,6 +611,7 @@ export interface InfiniteQueryExtraOptions<
613611
* ensure the infinite query can properly fetch the next page of data.
614612
* `initialPageParam` may be specified when using the
615613
* endpoint, to override the default value.
614+
* `maxPages` and `getPreviousPageParam` are both optional.
616615
*
617616
* @example
618617
*
@@ -625,30 +624,30 @@ export interface InfiniteQueryExtraOptions<
625624
* name: string
626625
* }
627626
*
628-
const pokemonApi = createApi({
629-
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
630-
endpoints: (build) => ({
631-
getInfinitePokemonWithMax: build.infiniteQuery<Pokemon[], string, number>({
632-
infiniteQueryOptions: {
633-
initialPageParam: 0,
634-
maxPages: 3,
635-
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
636-
lastPageParam + 1,
637-
getPreviousPageParam: (
638-
firstPage,
639-
allPages,
640-
firstPageParam,
641-
allPageParams,
642-
) => {
643-
return firstPageParam > 0 ? firstPageParam - 1 : undefined
644-
},
645-
},
646-
query({pageParam}) {
647-
return `https://example.com/listItems?page=${pageParam}`
648-
},
649-
}),
650-
}),
651-
})
627+
* const pokemonApi = createApi({
628+
* baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
629+
* endpoints: (build) => ({
630+
* getInfinitePokemonWithMax: build.infiniteQuery<Pokemon[], string, number>({
631+
* infiniteQueryOptions: {
632+
* initialPageParam: 0,
633+
* maxPages: 3,
634+
* getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
635+
* lastPageParam + 1,
636+
* getPreviousPageParam: (
637+
* firstPage,
638+
* allPages,
639+
* firstPageParam,
640+
* allPageParams,
641+
* ) => {
642+
* return firstPageParam > 0 ? firstPageParam - 1 : undefined
643+
* },
644+
* },
645+
* query({pageParam}) {
646+
* return `https://example.com/listItems?page=${pageParam}`
647+
* },
648+
* }),
649+
* }),
650+
* })
652651
653652
* ```
654653
*/
@@ -736,7 +735,7 @@ export type InfiniteQueryDefinition<
736735
ResultType,
737736
ReducerPath extends string = string,
738737
> =
739-
// Intentionally use `PageParam` as the `QueryArg` type
738+
// Infinite query endpoints receive `{queryArg, pageParam}`
740739
BaseEndpointDefinition<
741740
InfiniteQueryCombinedArg<QueryArg, PageParam>,
742741
BaseQuery,

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,13 +1913,10 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
19131913

19141914
return useMemo(() => {
19151915
const fetchNextPage = () => {
1916-
// TODO the hasNextPage bailout breaks things
1917-
//if (!hasNextPage) return
19181916
return trigger(arg, 'forward')
19191917
}
19201918

19211919
const fetchPreviousPage = () => {
1922-
//if (!hasPreviousPage) return
19231920
return trigger(arg, 'backward')
19241921
}
19251922

packages/toolkit/src/query/tests/infiniteQueries.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,6 @@ describe('Infinite queries', () => {
642642
}),
643643
)
644644

645-
console.log('Awaiting promises 1 and 2')
646-
647645
const entry1 = await promise1
648646
const entry2 = await promise2
649647

0 commit comments

Comments
 (0)