Skip to content

Commit 4efc718

Browse files
committed
Pass through initialPageParam on thunk dispatch
1 parent 822d0da commit 4efc718

File tree

5 files changed

+72
-41
lines changed

5 files changed

+72
-41
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
InfiniteQueryArgFrom,
1616
InfiniteQueryDefinition,
1717
MutationDefinition,
18+
PageParamFrom,
1819
QueryArgFrom,
1920
QueryDefinition,
2021
ResultTypeFrom,
@@ -66,16 +67,22 @@ export type StartQueryActionCreatorOptions = {
6667
[forceQueryFnSymbol]?: () => QueryReturnValue
6768
}
6869

69-
export type StartInfiniteQueryActionCreatorOptions = {
70+
export type StartInfiniteQueryActionCreatorOptions<
71+
D extends InfiniteQueryDefinition<any, any, any, any, any>,
72+
> = {
7073
subscribe?: boolean
7174
forceRefetch?: boolean | number
7275
subscriptionOptions?: SubscriptionOptions
73-
infiniteQueryOptions?: InfiniteQueryConfigOptions<unknown, unknown>
7476
direction?: 'forward' | 'backwards'
7577
[forceQueryFnSymbol]?: () => QueryReturnValue
7678
param?: unknown
7779
previous?: boolean
78-
}
80+
} & Partial<
81+
Pick<
82+
Partial<InfiniteQueryConfigOptions<ResultTypeFrom<D>, PageParamFrom<D>>>,
83+
'initialPageParam'
84+
>
85+
>
7986

8087
type StartQueryActionCreator<
8188
D extends QueryDefinition<any, any, any, any, any>,
@@ -90,7 +97,7 @@ type StartInfiniteQueryActionCreator<
9097
D extends InfiniteQueryDefinition<any, any, any, any, any>,
9198
> = (
9299
arg: InfiniteQueryArgFrom<D>,
93-
options?: StartInfiniteQueryActionCreatorOptions,
100+
options?: StartInfiniteQueryActionCreatorOptions<D>,
94101
) => (
95102
dispatch: ThunkDispatch<any, any, UnknownAction>,
96103
getState: () => any,
@@ -254,7 +261,7 @@ export function buildInitiate({
254261
}: {
255262
serializeQueryArgs: InternalSerializeQueryArgs
256263
queryThunk: QueryThunk
257-
infiniteQueryThunk: InfiniteQueryThunk
264+
infiniteQueryThunk: InfiniteQueryThunk<any>
258265
mutationThunk: MutationThunk
259266
api: Api<any, EndpointDefinitions, any, any>
260267
context: ApiContext<EndpointDefinitions>
@@ -488,7 +495,7 @@ You must add the middleware for RTK-Query to function correctly!`,
488495
subscribe = true,
489496
forceRefetch,
490497
subscriptionOptions,
491-
infiniteQueryOptions,
498+
initialPageParam,
492499
[forceQueryFnSymbol]: forceQueryFn,
493500
direction,
494501
param = arg,
@@ -514,6 +521,7 @@ You must add the middleware for RTK-Query to function correctly!`,
514521
param,
515522
previous,
516523
direction,
524+
initialPageParam,
517525
})
518526
const selector = (
519527
api.endpoints[endpointName] as ApiEndpointInfiniteQuery<any, any>
@@ -550,7 +558,6 @@ You must add the middleware for RTK-Query to function correctly!`,
550558
arg,
551559
requestId,
552560
subscriptionOptions,
553-
infiniteQueryOptions,
554561
queryCacheKey,
555562
abort,
556563
async unwrap() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface BuildMiddlewareInput<
4949
context: ApiContext<Definitions>
5050
queryThunk: QueryThunk
5151
mutationThunk: MutationThunk
52-
infiniteQueryThunk: InfiniteQueryThunk
52+
infiniteQueryThunk: InfiniteQueryThunk<any>
5353
api: Api<any, Definitions, ReducerPath, TagTypes>
5454
assertTagType: AssertTagTypes
5555
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export function buildSlice({
160160
}: {
161161
reducerPath: string
162162
queryThunk: QueryThunk
163-
infiniteQueryThunk: InfiniteQueryThunk
163+
infiniteQueryThunk: InfiniteQueryThunk<any>
164164
mutationThunk: MutationThunk
165165
serializeQueryArgs: InternalSerializeQueryArgs
166166
context: ApiContext<EndpointDefinitions>

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ export type QueryThunkArg = QuerySubstateIdentifier &
118118
endpointName: string
119119
}
120120

121-
export type InfiniteQueryThunkArg = QuerySubstateIdentifier &
122-
StartInfiniteQueryActionCreatorOptions & {
121+
export type InfiniteQueryThunkArg<
122+
D extends InfiniteQueryDefinition<any, any, any, any, any>,
123+
> = QuerySubstateIdentifier &
124+
StartInfiniteQueryActionCreatorOptions<D> & {
123125
type: `query`
124126
originalArgs: unknown
125127
endpointName: string
@@ -158,11 +160,9 @@ export type QueryThunk = AsyncThunk<
158160
QueryThunkArg,
159161
ThunkApiMetaConfig
160162
>
161-
export type InfiniteQueryThunk = AsyncThunk<
162-
ThunkResult,
163-
InfiniteQueryThunkArg,
164-
ThunkApiMetaConfig
165-
>
163+
export type InfiniteQueryThunk<
164+
D extends InfiniteQueryDefinition<any, any, any, any, any>,
165+
> = AsyncThunk<ThunkResult, InfiniteQueryThunkArg<D>, ThunkApiMetaConfig>
166166
export type MutationThunk = AsyncThunk<
167167
ThunkResult,
168168
MutationThunkArg,
@@ -382,7 +382,7 @@ export function buildThunks<
382382
// The generic async payload function for all of our thunks
383383
const executeEndpoint: AsyncThunkPayloadCreator<
384384
ThunkResult,
385-
QueryThunkArg | MutationThunkArg | InfiniteQueryThunkArg,
385+
QueryThunkArg | MutationThunkArg | InfiniteQueryThunkArg<any>,
386386
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
387387
> = async (
388388
arg,
@@ -554,21 +554,19 @@ export function buildThunks<
554554
existingData,
555555
)
556556

557-
console.log('Next page param: ', {
558-
previous,
559-
param,
560-
existingData,
561-
})
562-
563557
result = await fetchPage(existingData, param, previous)
564558
} else {
565559
// Otherwise, fetch the first page and then any remaining pages
566560

561+
const {
562+
initialPageParam = endpointDefinition.infiniteQueryOptions
563+
.initialPageParam,
564+
} = arg as InfiniteQueryThunkArg<any>
565+
567566
// Fetch first page
568567
result = await fetchPage(
569568
existingData,
570-
existingData.pageParams[0] ??
571-
endpointDefinition.infiniteQueryOptions.initialPageParam, // arg.originalArgs,
569+
existingData.pageParams[0] ?? initialPageParam,
572570
)
573571

574572
//original
@@ -752,7 +750,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
752750

753751
const infiniteQueryThunk = createAsyncThunk<
754752
ThunkResult,
755-
InfiniteQueryThunkArg,
753+
InfiniteQueryThunkArg<any>,
756754
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
757755
>(`${reducerPath}/executeQuery`, executeEndpoint, {
758756
getPendingMeta(queryThunkArgs) {

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

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,68 @@ describe('Infinite queries', () => {
7878
})
7979

8080
test('Basic infinite query behavior', async () => {
81-
const res = storeRef.store.dispatch(
81+
const res1 = storeRef.store.dispatch(
8282
// Should be `arg: string`
8383
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}),
8484
)
8585

86-
const firstResult = await res
87-
expect(firstResult.status).toBe(QueryStatus.fulfilled)
88-
console.log('Value: ', util.inspect(firstResult, { depth: Infinity }))
86+
const entry1InitialLoad = await res1
87+
expect(entry1InitialLoad.status).toBe(QueryStatus.fulfilled)
88+
// console.log('Value: ', util.inspect(entry1InitialLoad, { depth: Infinity }))
8989

90-
if (firstResult.status === QueryStatus.fulfilled) {
91-
expect(firstResult.data.pages).toEqual([
90+
if (entry1InitialLoad.status === QueryStatus.fulfilled) {
91+
expect(entry1InitialLoad.data.pages).toEqual([
9292
// one page, one entry
9393
[{ id: '0', name: 'Pokemon 0' }],
9494
])
9595
}
9696

97-
const secondRes = storeRef.store.dispatch(
97+
const entry1SecondPage = await storeRef.store.dispatch(
9898
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {
9999
direction: 'forward',
100100
}),
101101
)
102102

103-
const secondResult = await secondRes
104-
expect(secondResult.status).toBe(QueryStatus.fulfilled)
105-
console.log('Value: ', util.inspect(secondResult, { depth: Infinity }))
106-
if (secondResult.status === QueryStatus.fulfilled) {
107-
expect(secondResult.data.pages).toEqual([
103+
expect(entry1SecondPage.status).toBe(QueryStatus.fulfilled)
104+
// console.log('Value: ', util.inspect(entry1SecondPage, { depth: Infinity }))
105+
if (entry1SecondPage.status === QueryStatus.fulfilled) {
106+
expect(entry1SecondPage.data.pages).toEqual([
108107
// two pages, one entry each
109108
[{ id: '0', name: 'Pokemon 0' }],
110109
[{ id: '1', name: 'Pokemon 1' }],
111110
])
112111
}
113112

114-
console.log(
115-
'API state: ',
116-
util.inspect(storeRef.store.getState().api, { depth: Infinity }),
113+
// console.log(
114+
// 'API state: ',
115+
// util.inspect(storeRef.store.getState().api, { depth: Infinity }),
116+
// )
117+
118+
const entry2InitialLoad = await storeRef.store.dispatch(
119+
pokemonApi.endpoints.getInfinitePokemon.initiate('water', {
120+
initialPageParam: 3,
121+
}),
122+
)
123+
124+
if (entry2InitialLoad.status === QueryStatus.fulfilled) {
125+
expect(entry2InitialLoad.data.pages).toEqual([
126+
// one page, one entry
127+
[{ id: '3', name: 'Pokemon 3' }],
128+
])
129+
}
130+
131+
const entry2NextPage = await storeRef.store.dispatch(
132+
pokemonApi.endpoints.getInfinitePokemon.initiate('water', {
133+
direction: 'forward',
134+
}),
117135
)
136+
137+
if (entry2NextPage.status === QueryStatus.fulfilled) {
138+
expect(entry2NextPage.data.pages).toEqual([
139+
// two pages, one entry each
140+
[{ id: '3', name: 'Pokemon 3' }],
141+
[{ id: '4', name: 'Pokemon 4' }],
142+
])
143+
}
118144
})
119145
})

0 commit comments

Comments
 (0)