Skip to content

Commit d920c84

Browse files
committed
fix: we also need defaultPageParam for fetchInfiniteQuery now
1 parent bb16db1 commit d920c84

File tree

11 files changed

+51
-39
lines changed

11 files changed

+51
-39
lines changed

packages/query-core/src/tests/infiniteQueryBehavior.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ describe('InfiniteQueryBehavior', () => {
7676
await waitFor(() =>
7777
expect(observerResult).toMatchObject({
7878
isFetching: false,
79-
data: { pages: [1], pageParams: [undefined] },
79+
data: { pages: [1], pageParams: [1] },
8080
}),
8181
)
8282

8383
expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
8484
queryKey: key,
85-
pageParam: undefined,
85+
pageParam: 1,
8686
meta: undefined,
8787
signal: abortSignal,
8888
})
@@ -101,7 +101,7 @@ describe('InfiniteQueryBehavior', () => {
101101

102102
expect(observerResult).toMatchObject({
103103
isFetching: false,
104-
data: { pages: [1, 2], pageParams: [undefined, 2] },
104+
data: { pages: [1, 2], pageParams: [1, 2] },
105105
})
106106

107107
queryFnSpy.mockClear()
@@ -119,7 +119,7 @@ describe('InfiniteQueryBehavior', () => {
119119
// Only first two pages should be in the data
120120
expect(observerResult).toMatchObject({
121121
isFetching: false,
122-
data: { pages: [0, 1], pageParams: [0, undefined] },
122+
data: { pages: [0, 1], pageParams: [0, 1] },
123123
})
124124

125125
queryFnSpy.mockClear()
@@ -224,7 +224,7 @@ describe('InfiniteQueryBehavior', () => {
224224

225225
expect(queryFnSpy).toHaveBeenNthCalledWith(1, {
226226
queryKey: key,
227-
pageParam: undefined,
227+
pageParam: 1,
228228
meta: undefined,
229229
signal: abortSignal,
230230
})
@@ -261,7 +261,7 @@ describe('InfiniteQueryBehavior', () => {
261261
await waitFor(() =>
262262
expect(observerResult).toMatchObject({
263263
isFetching: false,
264-
data: { pages: [1], pageParams: [undefined] },
264+
data: { pages: [1], pageParams: [1] },
265265
}),
266266
)
267267

@@ -272,7 +272,7 @@ describe('InfiniteQueryBehavior', () => {
272272

273273
expect(observerResult).toMatchObject({
274274
isFetching: false,
275-
data: { pages: [1, 2], pageParams: [undefined, 2] },
275+
data: { pages: [1, 2], pageParams: [1, 2] },
276276
})
277277

278278
expect(queryFnSpy).toHaveBeenCalledTimes(1)
@@ -305,7 +305,7 @@ describe('InfiniteQueryBehavior', () => {
305305
isFetching: false,
306306
isError: true,
307307
error: new CancelledError(),
308-
data: { pages: [1, 2], pageParams: [undefined, 2] },
308+
data: { pages: [1, 2], pageParams: [1, 2] },
309309
})
310310

311311
// Pages should not have been fetched

packages/query-core/src/tests/infiniteQueryObserver.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('InfiniteQueryObserver', () => {
3333
await sleep(1)
3434
unsubscribe()
3535
expect(observerResult).toMatchObject({
36-
data: { pages: ['1'], pageParams: [undefined] },
36+
data: { pages: ['1'], pageParams: [1] },
3737
})
3838
})
3939

@@ -62,7 +62,7 @@ describe('InfiniteQueryObserver', () => {
6262
await sleep(1)
6363
unsubscribe()
6464
expect(observerResult).toMatchObject({
65-
data: { pages: ['1'], pageParams: [undefined] },
65+
data: { pages: ['1'], pageParams: [1] },
6666
})
6767
expect(queryFn).toBeCalledWith(expect.objectContaining({ meta }))
6868
})

packages/query-core/src/tests/queryClient.test.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ describe('queryClient', () => {
562562

563563
const data = {
564564
pages: ['data'],
565-
pageParams: [undefined],
565+
pageParams: [0],
566566
} as const
567567

568568
const fetchFn: QueryFunction<StrictData, StrictQueryKey> = () =>
@@ -574,21 +574,22 @@ describe('queryClient', () => {
574574
any,
575575
StrictData,
576576
StrictQueryKey
577-
>({ queryKey: key, queryFn: fetchFn }),
577+
>({ queryKey: key, queryFn: fetchFn, defaultPageParam: 0 }),
578578
).resolves.toEqual(data)
579579
})
580580

581581
test('should return infinite query data', async () => {
582582
const key = queryKey()
583583
const result = await queryClient.fetchInfiniteQuery({
584584
queryKey: key,
585-
queryFn: ({ pageParam = 10 }) => Number(pageParam),
585+
defaultPageParam: 10,
586+
queryFn: ({ pageParam }) => Number(pageParam),
586587
})
587588
const result2 = queryClient.getQueryData(key)
588589

589590
const expected = {
590591
pages: [10],
591-
pageParams: [undefined],
592+
pageParams: [10],
592593
}
593594

594595
expect(result).toEqual(expected)
@@ -610,13 +611,13 @@ describe('queryClient', () => {
610611
any,
611612
StrictData,
612613
StrictQueryKey
613-
>({ queryKey: key, queryFn: fetchFn })
614+
>({ queryKey: key, queryFn: fetchFn, defaultPageParam: 0 })
614615

615616
const result = queryClient.getQueryData(key)
616617

617618
expect(result).toEqual({
618619
pages: ['data'],
619-
pageParams: [undefined],
620+
pageParams: [0],
620621
})
621622
})
622623

@@ -625,14 +626,15 @@ describe('queryClient', () => {
625626

626627
await queryClient.prefetchInfiniteQuery({
627628
queryKey: key,
628-
queryFn: ({ pageParam = 10 }) => Number(pageParam),
629+
queryFn: ({ pageParam }) => Number(pageParam),
630+
defaultPageParam: 10,
629631
})
630632

631633
const result = queryClient.getQueryData(key)
632634

633635
expect(result).toEqual({
634636
pages: [10],
635-
pageParams: [undefined],
637+
pageParams: [10],
636638
})
637639
})
638640
})

packages/query-core/src/types.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export interface QueryOptions<
114114
maxPages?: number
115115
}
116116

117-
export interface InfiniteQueryOptions<TQueryFnData = unknown> {
117+
export interface DefaultPageParam {
118+
defaultPageParam: unknown
119+
}
120+
121+
export interface InfiniteQueryOptions<TQueryFnData = unknown>
122+
extends DefaultPageParam {
118123
/**
119124
* This function can be set to automatically get the previous cursor for infinite queries.
120125
* The result will also be used to determine the value of `hasPreviousPage`.
@@ -125,8 +130,6 @@ export interface InfiniteQueryOptions<TQueryFnData = unknown> {
125130
* The result will also be used to determine the value of `hasNextPage`.
126131
*/
127132
getNextPageParam: GetNextPageParamFunction<TQueryFnData>
128-
129-
defaultPageParam: unknown
130133
}
131134

132135
export type ThrowErrors<
@@ -334,11 +337,12 @@ export interface FetchInfiniteQueryOptions<
334337
TData = TQueryFnData,
335338
TQueryKey extends QueryKey = QueryKey,
336339
> extends FetchQueryOptions<
337-
TQueryFnData,
338-
TError,
339-
InfiniteData<TData>,
340-
TQueryKey
341-
> {}
340+
TQueryFnData,
341+
TError,
342+
InfiniteData<TData>,
343+
TQueryKey
344+
>,
345+
DefaultPageParam {}
342346

343347
export interface ResultOptions {
344348
throwOnError?: boolean

packages/react-query/src/__tests__/ssr.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ describe('Server Side Rendering', () => {
146146
)
147147
}
148148

149-
await queryClient.prefetchInfiniteQuery({ queryKey: key, queryFn })
149+
await queryClient.prefetchInfiniteQuery({
150+
queryKey: key,
151+
queryFn,
152+
defaultPageParam: 0,
153+
})
150154

151155
const markup = renderToString(
152156
<QueryClientProvider client={queryClient}>

packages/react-query/src/__tests__/suspense.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("useQuery's in Suspense mode", () => {
102102

103103
expect(states.length).toBe(1)
104104
expect(states[0]).toMatchObject({
105-
data: { pages: [1], pageParams: [undefined] },
105+
data: { pages: [1], pageParams: [1] },
106106
status: 'success',
107107
})
108108

@@ -111,7 +111,7 @@ describe("useQuery's in Suspense mode", () => {
111111

112112
expect(states.length).toBe(2)
113113
expect(states[1]).toMatchObject({
114-
data: { pages: [2], pageParams: [undefined] },
114+
data: { pages: [2], pageParams: [1] },
115115
status: 'success',
116116
})
117117
})

packages/react-query/src/__tests__/useInfiniteQuery.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('useInfiniteQuery', () => {
9797
})
9898

9999
expect(states[1]).toEqual({
100-
data: { pages: [0], pageParams: [undefined] },
100+
data: { pages: [0], pageParams: [0] },
101101
dataUpdatedAt: expect.any(Number),
102102
error: null,
103103
errorUpdatedAt: 0,
@@ -1111,7 +1111,7 @@ describe('useInfiniteQuery', () => {
11111111
const state = useInfiniteQuery({
11121112
queryKey: key,
11131113
queryFn: ({ pageParam }): number => pageParam,
1114-
initialData: { pages: [10], pageParams: [undefined] },
1114+
initialData: { pages: [10], pageParams: [10] },
11151115
getNextPageParam: (lastPage) => (lastPage === 10 ? 11 : undefined),
11161116
defaultPageParam: 10,
11171117
})
@@ -1151,7 +1151,7 @@ describe('useInfiniteQuery', () => {
11511151
queryKey: key,
11521152
queryFn: ({ pageParam }): number => pageParam,
11531153
defaultPageParam: 10,
1154-
initialData: { pages: [10], pageParams: [undefined] },
1154+
initialData: { pages: [10], pageParams: [10] },
11551155
getNextPageParam: () => undefined,
11561156
})
11571157

packages/solid-query/src/__tests__/createInfiniteQuery.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('useInfiniteQuery', () => {
112112
})
113113

114114
expect(states[1]).toEqual({
115-
data: { pages: [0], pageParams: [undefined] },
115+
data: { pages: [0], pageParams: [0] },
116116
dataUpdatedAt: expect.any(Number),
117117
error: null,
118118
errorUpdatedAt: 0,
@@ -1185,7 +1185,7 @@ describe('useInfiniteQuery', () => {
11851185
queryKey: key,
11861186
queryFn: ({ pageParam }): number => pageParam,
11871187
defaultPageParam: 10,
1188-
initialData: { pages: [10], pageParams: [undefined] },
1188+
initialData: { pages: [10], pageParams: [10] },
11891189
getNextPageParam: (lastPage) => (lastPage === 10 ? 11 : undefined),
11901190
}))
11911191

@@ -1229,7 +1229,7 @@ describe('useInfiniteQuery', () => {
12291229
queryKey: key,
12301230
queryFn: ({ pageParam }): number => pageParam,
12311231
defaultPageParam: 10,
1232-
initialData: { pages: [10], pageParams: [undefined] },
1232+
initialData: { pages: [10], pageParams: [10] },
12331233
getNextPageParam: () => undefined,
12341234
}))
12351235

packages/solid-query/src/__tests__/suspense.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("useQuery's in Suspense mode", () => {
120120
// occurs on read.
121121
expect(states.length).toBe(2)
122122
expect(states[1]).toMatchObject({
123-
data: { pages: [1], pageParams: [undefined] },
123+
data: { pages: [1], pageParams: [1] },
124124
status: 'success',
125125
})
126126

@@ -130,7 +130,7 @@ describe("useQuery's in Suspense mode", () => {
130130
// TODO(lukemurray): in react this is 2 and in solid it is 4
131131
expect(states.length).toBe(4)
132132
expect(states[3]).toMatchObject({
133-
data: { pages: [2], pageParams: [undefined] },
133+
data: { pages: [2], pageParams: [1] },
134134
status: 'success',
135135
})
136136
})

packages/vue-query/src/__tests__/queryClient.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ describe('QueryCache', () => {
263263

264264
queryClient.fetchInfiniteQuery({
265265
queryKey: queryKeyRef,
266+
defaultPageParam: 0,
266267
})
267268

268269
expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
@@ -278,6 +279,7 @@ describe('QueryCache', () => {
278279
queryClient.prefetchInfiniteQuery({
279280
queryKey: queryKeyRef,
280281
queryFn: fn,
282+
defaultPageParam: 0,
281283
})
282284

283285
expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({

0 commit comments

Comments
 (0)