@tanstack/query-core 5.0.0-alpha.63 → 5.0.0-alpha.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/infiniteQueryBehavior.cjs +6 -13
- package/build/lib/infiniteQueryBehavior.cjs.map +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +6 -13
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.legacy.cjs +7 -13
- package/build/lib/infiniteQueryBehavior.legacy.cjs.map +1 -1
- package/build/lib/infiniteQueryBehavior.legacy.js +7 -13
- package/build/lib/infiniteQueryBehavior.legacy.js.map +1 -1
- package/build/lib/queryClient.cjs +1 -1
- package/build/lib/queryClient.cjs.map +1 -1
- package/build/lib/queryClient.d.ts.map +1 -1
- package/build/lib/queryClient.js +1 -1
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.legacy.cjs +1 -1
- package/build/lib/queryClient.legacy.cjs.map +1 -1
- package/build/lib/queryClient.legacy.js +1 -1
- package/build/lib/queryClient.legacy.js.map +1 -1
- package/build/lib/types.d.ts +8 -2
- package/build/lib/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/infiniteQueryBehavior.ts +12 -17
- package/src/queryClient.ts +3 -1
- package/src/tests/queryClient.test.tsx +40 -0
- package/src/types.ts +17 -9
|
@@ -7,11 +7,9 @@ import type {
|
|
|
7
7
|
QueryKey,
|
|
8
8
|
} from './types'
|
|
9
9
|
|
|
10
|
-
export function infiniteQueryBehavior<
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
TData,
|
|
14
|
-
>(): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
|
|
10
|
+
export function infiniteQueryBehavior<TQueryFnData, TError, TData>(
|
|
11
|
+
pages?: number,
|
|
12
|
+
): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
|
|
15
13
|
return {
|
|
16
14
|
onFetch: (context) => {
|
|
17
15
|
context.fetchFn = async () => {
|
|
@@ -84,13 +82,8 @@ export function infiniteQueryBehavior<
|
|
|
84
82
|
|
|
85
83
|
let result: InfiniteData<unknown>
|
|
86
84
|
|
|
87
|
-
// Fetch first page?
|
|
88
|
-
if (!oldPages.length) {
|
|
89
|
-
result = await fetchPage(empty, options.defaultPageParam)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
85
|
// fetch next / previous page?
|
|
93
|
-
|
|
86
|
+
if (direction && oldPages.length) {
|
|
94
87
|
const previous = direction === 'backward'
|
|
95
88
|
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
|
|
96
89
|
const oldData = {
|
|
@@ -100,15 +93,17 @@ export function infiniteQueryBehavior<
|
|
|
100
93
|
const param = pageParamFn(options, oldData)
|
|
101
94
|
|
|
102
95
|
result = await fetchPage(oldData, param, previous)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Refetch pages
|
|
106
|
-
else {
|
|
96
|
+
} else {
|
|
107
97
|
// Fetch first page
|
|
108
|
-
result = await fetchPage(
|
|
98
|
+
result = await fetchPage(
|
|
99
|
+
empty,
|
|
100
|
+
oldPageParams[0] ?? options.defaultPageParam,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
const remainingPages = pages ?? oldPages.length
|
|
109
104
|
|
|
110
105
|
// Fetch remaining pages
|
|
111
|
-
for (let i = 1; i <
|
|
106
|
+
for (let i = 1; i < remainingPages; i++) {
|
|
112
107
|
const param = getNextPageParam(options, result)
|
|
113
108
|
result = await fetchPage(result, param)
|
|
114
109
|
}
|
package/src/queryClient.ts
CHANGED
|
@@ -325,7 +325,9 @@ export class QueryClient {
|
|
|
325
325
|
TPageParam
|
|
326
326
|
>,
|
|
327
327
|
): Promise<InfiniteData<TData>> {
|
|
328
|
-
options.behavior = infiniteQueryBehavior<TQueryFnData, TError, TData>(
|
|
328
|
+
options.behavior = infiniteQueryBehavior<TQueryFnData, TError, TData>(
|
|
329
|
+
options.pages,
|
|
330
|
+
)
|
|
329
331
|
return this.fetchQuery(options)
|
|
330
332
|
}
|
|
331
333
|
|
|
@@ -649,6 +649,46 @@ describe('queryClient', () => {
|
|
|
649
649
|
pageParams: [10],
|
|
650
650
|
})
|
|
651
651
|
})
|
|
652
|
+
|
|
653
|
+
test('should prefetch multiple pages', async () => {
|
|
654
|
+
const key = queryKey()
|
|
655
|
+
|
|
656
|
+
await queryClient.prefetchInfiniteQuery({
|
|
657
|
+
queryKey: key,
|
|
658
|
+
queryFn: ({ pageParam }) => String(pageParam),
|
|
659
|
+
getNextPageParam: (_lastPage, _pages, lastPageParam) =>
|
|
660
|
+
lastPageParam + 5,
|
|
661
|
+
defaultPageParam: 10,
|
|
662
|
+
pages: 3,
|
|
663
|
+
})
|
|
664
|
+
|
|
665
|
+
const result = queryClient.getQueryData(key)
|
|
666
|
+
|
|
667
|
+
expect(result).toEqual({
|
|
668
|
+
pages: ['10', '15', '20'],
|
|
669
|
+
pageParams: [10, 15, 20],
|
|
670
|
+
})
|
|
671
|
+
})
|
|
672
|
+
|
|
673
|
+
test('should stop prefetching if getNextPageParam returns undefined', async () => {
|
|
674
|
+
const key = queryKey()
|
|
675
|
+
|
|
676
|
+
await queryClient.prefetchInfiniteQuery({
|
|
677
|
+
queryKey: key,
|
|
678
|
+
queryFn: ({ pageParam }) => String(pageParam),
|
|
679
|
+
getNextPageParam: (_lastPage, _pages, lastPageParam) =>
|
|
680
|
+
lastPageParam >= 20 ? undefined : lastPageParam + 5,
|
|
681
|
+
defaultPageParam: 10,
|
|
682
|
+
pages: 5,
|
|
683
|
+
})
|
|
684
|
+
|
|
685
|
+
const result = queryClient.getQueryData(key)
|
|
686
|
+
|
|
687
|
+
expect(result).toEqual({
|
|
688
|
+
pages: ['10', '15', '20'],
|
|
689
|
+
pageParams: [10, 15, 20],
|
|
690
|
+
})
|
|
691
|
+
})
|
|
652
692
|
})
|
|
653
693
|
|
|
654
694
|
describe('prefetchQuery', () => {
|
package/src/types.ts
CHANGED
|
@@ -358,20 +358,28 @@ export interface FetchQueryOptions<
|
|
|
358
358
|
staleTime?: number
|
|
359
359
|
}
|
|
360
360
|
|
|
361
|
-
|
|
361
|
+
type FetchInfiniteQueryPages<TQueryFnData = unknown, TPageParam = unknown> =
|
|
362
|
+
| { pages?: never; getNextPageParam?: never }
|
|
363
|
+
| {
|
|
364
|
+
pages: number
|
|
365
|
+
getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData>
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export type FetchInfiniteQueryOptions<
|
|
362
369
|
TQueryFnData = unknown,
|
|
363
370
|
TError = DefaultError,
|
|
364
371
|
TData = TQueryFnData,
|
|
365
372
|
TQueryKey extends QueryKey = QueryKey,
|
|
366
373
|
TPageParam = unknown,
|
|
367
|
-
>
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
374
|
+
> = FetchQueryOptions<
|
|
375
|
+
TQueryFnData,
|
|
376
|
+
TError,
|
|
377
|
+
InfiniteData<TData>,
|
|
378
|
+
TQueryKey,
|
|
379
|
+
TPageParam
|
|
380
|
+
> &
|
|
381
|
+
DefaultPageParam<TPageParam> &
|
|
382
|
+
FetchInfiniteQueryPages<TQueryFnData, TPageParam>
|
|
375
383
|
|
|
376
384
|
export interface ResultOptions {
|
|
377
385
|
throwOnError?: boolean
|