Skip to content

Commit 1e5789f

Browse files
authored
Merge pull request #4393 from riqts/infQuery-API
[API Concept] - Infinite Query API
2 parents e848a55 + 4ee0f7f commit 1e5789f

21 files changed

+2566
-108
lines changed

packages/toolkit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@size-limit/webpack": "^11.0.1",
5959
"@testing-library/dom": "^10.4.0",
6060
"@testing-library/react": "^16.0.1",
61+
"@testing-library/react-render-stream": "^1.0.3",
6162
"@testing-library/user-event": "^14.5.2",
6263
"@types/babel__core": "^7.20.5",
6364
"@types/babel__helper-module-imports": "^7.18.3",

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

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { SerializedError } from '@reduxjs/toolkit'
22
import type { BaseQueryError } from '../baseQueryTypes'
33
import type {
4-
QueryDefinition,
5-
MutationDefinition,
6-
EndpointDefinitions,
74
BaseEndpointDefinition,
8-
ResultTypeFrom,
5+
EndpointDefinitions,
6+
InfiniteQueryDefinition,
7+
MutationDefinition,
8+
PageParamFrom,
99
QueryArgFrom,
10+
QueryDefinition,
11+
ResultTypeFrom,
1012
} from '../endpointDefinitions'
1113
import type { Id, WithRequiredProp } from '../tsHelpers'
1214

@@ -28,6 +30,35 @@ export type RefetchConfigOptions = {
2830
refetchOnFocus: boolean
2931
}
3032

33+
export type GetNextPageParamFunction<TPageParam, TQueryFnData> = (
34+
lastPage: TQueryFnData,
35+
allPages: Array<TQueryFnData>,
36+
lastPageParam: TPageParam,
37+
allPageParams: Array<TPageParam>,
38+
) => TPageParam | undefined | null
39+
40+
export type GetPreviousPageParamFunction<TPageParam, TQueryFnData> = (
41+
firstPage: TQueryFnData,
42+
allPages: Array<TQueryFnData>,
43+
firstPageParam: TPageParam,
44+
allPageParams: Array<TPageParam>,
45+
) => TPageParam | undefined | null
46+
47+
export type InfiniteQueryConfigOptions<TQueryFnData, TPageParam> = {
48+
initialPageParam: TPageParam
49+
/**
50+
* This function can be set to automatically get the previous cursor for infinite queries.
51+
* The result will also be used to determine the value of `hasPreviousPage`.
52+
*/
53+
getPreviousPageParam?: GetPreviousPageParamFunction<TPageParam, TQueryFnData>
54+
getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData>
55+
}
56+
57+
export interface InfiniteData<TData, TPageParam> {
58+
pages: Array<TData>
59+
pageParams: Array<TPageParam>
60+
}
61+
3162
/**
3263
* Strings describing the query state at any given time.
3364
*/
@@ -133,7 +164,10 @@ export type MutationKeys<Definitions extends EndpointDefinitions> = {
133164
: never
134165
}[keyof Definitions]
135166

136-
type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
167+
type BaseQuerySubState<
168+
D extends BaseEndpointDefinition<any, any, any>,
169+
DataType = ResultTypeFrom<D>,
170+
> = {
137171
/**
138172
* The argument originally passed into the hook or `initiate` action call
139173
*/
@@ -145,7 +179,7 @@ type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
145179
/**
146180
* The received data from the query
147181
*/
148-
data?: ResultTypeFrom<D>
182+
data?: DataType
149183
/**
150184
* The received error if applicable
151185
*/
@@ -166,21 +200,31 @@ type BaseQuerySubState<D extends BaseEndpointDefinition<any, any, any>> = {
166200
* Time that the latest query was fulfilled
167201
*/
168202
fulfilledTimeStamp?: number
203+
/**
204+
* Infinite Query Specific substate properties
205+
*/
206+
hasNextPage?: boolean
207+
hasPreviousPage?: boolean
208+
direction?: 'forward' | 'backward'
209+
param?: QueryArgFrom<D>
169210
}
170211

171-
export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
212+
export type QuerySubState<
213+
D extends BaseEndpointDefinition<any, any, any>,
214+
DataType = ResultTypeFrom<D>,
215+
> = Id<
172216
| ({
173217
status: QueryStatus.fulfilled
174218
} & WithRequiredProp<
175-
BaseQuerySubState<D>,
219+
BaseQuerySubState<D, DataType>,
176220
'data' | 'fulfilledTimeStamp'
177221
> & { error: undefined })
178222
| ({
179223
status: QueryStatus.pending
180-
} & BaseQuerySubState<D>)
224+
} & BaseQuerySubState<D, DataType>)
181225
| ({
182226
status: QueryStatus.rejected
183-
} & WithRequiredProp<BaseQuerySubState<D>, 'error'>)
227+
} & WithRequiredProp<BaseQuerySubState<D, DataType>, 'error'>)
184228
| {
185229
status: QueryStatus.uninitialized
186230
originalArgs?: undefined
@@ -193,6 +237,21 @@ export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
193237
}
194238
>
195239

240+
export type InfiniteQuerySubState<
241+
D extends BaseEndpointDefinition<any, any, any>,
242+
> =
243+
D extends InfiniteQueryDefinition<any, any, any, any, any>
244+
? QuerySubState<D, InfiniteData<ResultTypeFrom<D>, PageParamFrom<D>>> & {
245+
// TODO: These shouldn't be optional
246+
hasNextPage?: boolean
247+
hasPreviousPage?: boolean
248+
isFetchingNextPage?: boolean
249+
isFetchingPreviousPage?: boolean
250+
param?: PageParamFrom<D>
251+
direction?: 'forward' | 'backward'
252+
}
253+
: never
254+
196255
type BaseMutationSubState<D extends BaseEndpointDefinition<any, any, any>> = {
197256
requestId: string
198257
data?: ResultTypeFrom<D>
@@ -249,7 +308,10 @@ export type InvalidationState<TagTypes extends string> = {
249308
}
250309

251310
export type QueryState<D extends EndpointDefinitions> = {
252-
[queryCacheKey: string]: QuerySubState<D[string]> | undefined
311+
[queryCacheKey: string]:
312+
| QuerySubState<D[string]>
313+
| InfiniteQuerySubState<D[string]>
314+
| undefined
253315
}
254316

255317
export type SubscriptionState = {

0 commit comments

Comments
 (0)