Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

infiniteQueryOptions.ts

558 lines18.0 KB

1import type {
2 DefaultError,
3 InfiniteData,
4 InfiniteQueryMode,
5 InitialDataFunction,
6 NonUndefinedGuard,
7 OmitKeyof,
8 QueryKey,
9 QueryKeyWithDataTag,
10 SkipToken,
11} from '@tanstack/query-core'
12import type {
13 UseInfiniteQueryOptions,
14 UseInfiniteQueryOptionsBase,
15} from './types'
16​
17type OptionalInitialData<TQueryFnData, TPageParam> = {
18 /**
19 * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
20 * created or cached yet). If set to a function, the function will be called **once** during the shared/root
21 * query initialization, and be expected to synchronously return the initial data. Initial data is
22 * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
23 * cache.
24 */
25 initialData?:
26 | undefined
27 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
28 | InitialDataFunction<
29 NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
30 >
32​
33type RequiredInitialData<TQueryFnData, TPageParam> = {
34 initialData:
35 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
36 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
37 | undefined
39​
40type WithoutSkipTokenQueryFn<TOptions extends { queryFn?: unknown }> =
41 OmitKeyof<TOptions, 'queryFn'> & {
42 queryFn?: Exclude<TOptions['queryFn'], SkipToken | undefined>
43 }
44​
45/**
46 * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set β€” `data`
47 * may be `undefined` while the query is `pending`.
48 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
49 * @template TError - The type of errors your `queryFn` may throw.
50 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
51 * the shape of all fetched pages plus their page params.
52 * @template TQueryKey - The type of your `queryKey`.
53 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
54 */
55export type UndefinedInitialDataInfiniteOptions<
56 TQueryFnData,
57 TError = DefaultError,
58 TData = InfiniteData<TQueryFnData>,
59 TQueryKey extends QueryKey = QueryKey,
60 TPageParam = unknown,
61 TMode extends InfiniteQueryMode | undefined = InfiniteQueryMode | undefined,
62> = UseInfiniteQueryOptions<
63 TQueryFnData,
64 TError,
65 TData,
66 TQueryKey,
67 TPageParam,
68 TMode
69> &
70 OptionalInitialData<TQueryFnData, TPageParam>
71​
72/** Manual query options with optional initial data. */
73export type ManualUndefinedInitialDataInfiniteOptions<
74 TQueryFnData,
75 TError = DefaultError,
76 TData = InfiniteData<TQueryFnData>,
77 TQueryKey extends QueryKey = QueryKey,
78 TPageParam = unknown,
79> = UseInfiniteQueryOptionsBase<
80 TQueryFnData,
81 TError,
82 TData,
83 TQueryKey,
84 TPageParam,
85 InfiniteQueryMode
86> &
87 OptionalInitialData<TQueryFnData, TPageParam>
88​
89/** Manual query options with a callable query function. */
90export type UnusedSkipTokenManualInfiniteOptions<
91 TQueryFnData,
92 TError = DefaultError,
93 TData = InfiniteData<TQueryFnData>,
94 TQueryKey extends QueryKey = QueryKey,
95 TPageParam = unknown,
96> = WithoutSkipTokenQueryFn<
97 UseInfiniteQueryOptionsBase<
98 TQueryFnData,
99 TError,
100 TData,
101 TQueryKey,
102 TPageParam,
103 InfiniteQueryMode
104 >
106​
107/**
108 * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set and
109 * `queryFn` is not `skipToken` β€” same as {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be
110 * `skipToken`.
111 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
112 * @template TError - The type of errors your `queryFn` may throw.
113 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
114 * the shape of all fetched pages plus their page params.
115 * @template TQueryKey - The type of your `queryKey`.
116 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
117 */
118export type UnusedSkipTokenInfiniteOptions<
119 TQueryFnData,
120 TError = DefaultError,
121 TData = InfiniteData<TQueryFnData>,
122 TQueryKey extends QueryKey = QueryKey,
123 TPageParam = unknown,
124 TMode extends InfiniteQueryMode | undefined = InfiniteQueryMode | undefined,
125> = WithoutSkipTokenQueryFn<
126 UseInfiniteQueryOptions<
127 TQueryFnData,
128 TError,
129 TData,
130 TQueryKey,
131 TPageParam,
132 TMode
133 >
135​
136/**
137 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set β€” `data` is
138 * never `undefined` (unless a `select` changes `TData` to include `undefined`).
139 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
140 * @template TError - The type of errors your `queryFn` may throw.
141 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
142 * the shape of all fetched pages plus their page params.
143 * @template TQueryKey - The type of your `queryKey`.
144 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
145 */
146export type DefinedInitialDataInfiniteOptions<
147 TQueryFnData,
148 TError = DefaultError,
149 TData = InfiniteData<TQueryFnData>,
150 TQueryKey extends QueryKey = QueryKey,
151 TPageParam = unknown,
152 TMode extends InfiniteQueryMode | undefined = InfiniteQueryMode | undefined,
153> = UseInfiniteQueryOptions<
154 TQueryFnData,
155 TError,
156 TData,
157 TQueryKey,
158 TPageParam,
159 TMode
160> &
161 RequiredInitialData<TQueryFnData, TPageParam>
162​
163/** Manual query options with defined initial data. */
164export type ManualDefinedInitialDataInfiniteOptions<
165 TQueryFnData,
166 TError = DefaultError,
167 TData = InfiniteData<TQueryFnData>,
168 TQueryKey extends QueryKey = QueryKey,
169 TPageParam = unknown,
170> = UseInfiniteQueryOptionsBase<
171 TQueryFnData,
172 TError,
173 TData,
174 TQueryKey,
175 TPageParam,
176 InfiniteQueryMode
177> &
178 RequiredInitialData<TQueryFnData, TPageParam>
179​
180/**
181 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
182 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
183 * `options.queryKey` is required and is the query key to generate options for.
184 *
185 * This overload is selected when `initialData` is set.
186 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`, with `initialData` set.
187 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
188 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
189 * automatically as the user scrolls.
190 * @see {@link useInfiniteQuery} to run an infinite query with these options.
191 * @example
192 * ```tsx
193 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
194 *
195 * export const projectsOptions = infiniteQueryOptions({
196 * queryKey: ['projects'],
197 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
198 * initialPageParam: 0,
199 * getNextPageParam: (lastPage) => lastPage.nextId,
200 * initialData: { pages: [], pageParams: [] },
201 * })
202 *
203 * function Projects() {
204 * // `data` is never `undefined`, thanks to `initialData` β€” even if a refetch fails, so the
205 * // list stays visible alongside the error.
206 * const { data, isError, error } = useInfiniteQuery(projectsOptions)
207 *
208 * return (
209 * <div>
210 * {isError ? <span>Error: {error.message}</span> : null}
211 * <ul>
212 * {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
213 * </ul>
214 * </div>
215 * )
216 * }
217 * ```
218 */
219export function infiniteQueryOptions<
220 TQueryFnData,
221 TError = DefaultError,
222 TData = InfiniteData<TQueryFnData>,
223 TQueryKey extends QueryKey = QueryKey,
224 TPageParam = unknown,
225>(
226 options: DefinedInitialDataInfiniteOptions<
227 TQueryFnData,
228 TError,
229 TData,
230 TQueryKey,
231 TPageParam,
232 undefined
233 >,
234): DefinedInitialDataInfiniteOptions<
235 TQueryFnData,
236 TError,
237 TData,
238 TQueryKey,
239 TPageParam,
240 undefined
241> &
242 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
243​
244export function infiniteQueryOptions<
245 TQueryFnData,
246 TError = DefaultError,
247 TData = InfiniteData<TQueryFnData>,
248 TQueryKey extends QueryKey = QueryKey,
249 TPageParam = unknown,
250>(
251 options: ManualDefinedInitialDataInfiniteOptions<
252 TQueryFnData,
253 TError,
254 TData,
255 TQueryKey,
256 TPageParam
257 >,
258): ManualDefinedInitialDataInfiniteOptions<
259 TQueryFnData,
260 TError,
261 TData,
262 TQueryKey,
263 TPageParam
264> &
265 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
266​
267/**
268 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
269 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
270 * `options.queryKey` is required and is the query key to generate options for.
271 * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
272 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
273 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
274 * automatically as the user scrolls.
275 * @see {@link useInfiniteQuery} to run an infinite query with these options.
276 * @example
277 * A parameterized factory, so the same options object can be reused per `postId`:
278 * ```tsx
279 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
280 *
281 * export const commentsOptions = (postId: string) =>
282 * infiniteQueryOptions({
283 * queryKey: ['post', postId, 'comments'],
284 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
285 * initialPageParam: 0,
286 * getNextPageParam: (lastPage) => lastPage.nextId,
287 * })
288 *
289 * function Comments({ postId }: { postId: string }) {
290 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
291 *
292 * if (isPending) return 'Loading...'
293 * if (isError) return <span>Error: {error.message}</span>
294 *
295 * return (
296 * <ul>
297 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
298 * </ul>
299 * )
300 * }
301 * ```
302 */
303export function infiniteQueryOptions<
304 TQueryFnData,
305 TError = DefaultError,
306 TData = InfiniteData<TQueryFnData>,
307 TQueryKey extends QueryKey = QueryKey,
308 TPageParam = unknown,
309>(
310 options: UnusedSkipTokenInfiniteOptions<
311 TQueryFnData,
312 TError,
313 TData,
314 TQueryKey,
315 TPageParam,
316 undefined
317 >,
318): UnusedSkipTokenInfiniteOptions<
319 TQueryFnData,
320 TError,
321 TData,
322 TQueryKey,
323 TPageParam,
324 undefined
325> &
326 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
327​
328export function infiniteQueryOptions<
329 TQueryFnData,
330 TError = DefaultError,
331 TData = InfiniteData<TQueryFnData>,
332 TQueryKey extends QueryKey = QueryKey,
333 TPageParam = unknown,
334>(
335 options: UnusedSkipTokenManualInfiniteOptions<
336 TQueryFnData,
337 TError,
338 TData,
339 TQueryKey,
340 TPageParam
341 >,
342): UnusedSkipTokenManualInfiniteOptions<
343 TQueryFnData,
344 TError,
345 TData,
346 TQueryKey,
347 TPageParam
348> &
349 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
350​
351/**
352 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
353 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
354 * `options.queryKey` is required and is the query key to generate options for.
355 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
356 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
357 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages (from a button click or
358 * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
359 * @see {@link useInfiniteQuery} to run an infinite query with these options.
360 * @example
361 * A parameterized factory, so the same options object can be reused per `postId`:
362 * ```tsx
363 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
364 *
365 * export const commentsOptions = (postId: string) =>
366 * infiniteQueryOptions({
367 * queryKey: ['post', postId, 'comments'],
368 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
369 * initialPageParam: 0,
370 * getNextPageParam: (lastPage) => lastPage.nextId,
371 * })
372 *
373 * function Comments({ postId }: { postId: string }) {
374 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
375 *
376 * if (isPending) return 'Loading...'
377 * if (isError) return <span>Error: {error.message}</span>
378 *
379 * return (
380 * <ul>
381 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
382 * </ul>
383 * )
384 * }
385 * ```
386 */
387export function infiniteQueryOptions<
388 TQueryFnData,
389 TError = DefaultError,
390 TData = InfiniteData<TQueryFnData>,
391 TQueryKey extends QueryKey = QueryKey,
392 TPageParam = unknown,
393>(
394 options: UndefinedInitialDataInfiniteOptions<
395 TQueryFnData,
396 TError,
397 TData,
398 TQueryKey,
399 TPageParam,
400 undefined
401 >,
402): UndefinedInitialDataInfiniteOptions<
403 TQueryFnData,
404 TError,
405 TData,
406 TQueryKey,
407 TPageParam,
408 undefined
409> &
410 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
411​
412export function infiniteQueryOptions<
413 TQueryFnData,
414 TError = DefaultError,
415 TData = InfiniteData<TQueryFnData>,
416 TQueryKey extends QueryKey = QueryKey,
417 TPageParam = unknown,
418>(
419 options: ManualUndefinedInitialDataInfiniteOptions<
420 TQueryFnData,
421 TError,
422 TData,
423 TQueryKey,
424 TPageParam
425 >,
426): ManualUndefinedInitialDataInfiniteOptions<
427 TQueryFnData,
428 TError,
429 TData,
430 TQueryKey,
431 TPageParam
432> &
433 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
434​
435/**
436 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
437 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
438 * `options.queryKey` is required and is the query key to generate options for.
439 *
440 * This overload is selected when `initialData` is set.
441 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`, with `initialData` set.
442 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
443 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
444 * automatically as the user scrolls.
445 * @see {@link useInfiniteQuery} to run an infinite query with these options.
446 * @example
447 * ```tsx
448 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
449 *
450 * export const projectsOptions = infiniteQueryOptions({
451 * queryKey: ['projects'],
452 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
453 * initialPageParam: 0,
454 * getNextPageParam: (lastPage) => lastPage.nextId,
455 * initialData: { pages: [], pageParams: [] },
456 * })
457 *
458 * function Projects() {
459 * // `data` is never `undefined`, thanks to `initialData` β€” even if a refetch fails, so the
460 * // list stays visible alongside the error.
461 * const { data, isError, error } = useInfiniteQuery(projectsOptions)
462 *
463 * return (
464 * <div>
465 * {isError ? <span>Error: {error.message}</span> : null}
466 * <ul>
467 * {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
468 * </ul>
469 * </div>
470 * )
471 * }
472 * ```
473 */
474export function infiniteQueryOptions<
475 TQueryFnData,
476 TError = DefaultError,
477 TData = InfiniteData<TQueryFnData>,
478 TQueryKey extends QueryKey = QueryKey,
479 TPageParam = unknown,
480>(
481 options: DefinedInitialDataInfiniteOptions<
482 TQueryFnData,
483 TError,
484 TData,
485 TQueryKey,
486 TPageParam
487 >,
488): DefinedInitialDataInfiniteOptions<
489 TQueryFnData,
490 TError,
491 TData,
492 TQueryKey,
493 TPageParam
494> &
495 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
496​
497/**
498 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
499 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
500 * `options.queryKey` is required and is the query key to generate options for.
501 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
502 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
503 * @remarks See {@link useInfiniteQuery} for examples that fetch further pages (from a button click or
504 * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
505 * @see {@link useInfiniteQuery} to run an infinite query with these options.
506 * @example
507 * A parameterized factory, so the same options object can be reused per `postId`:
508 * ```tsx
509 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
510 *
511 * export const commentsOptions = (postId: string) =>
512 * infiniteQueryOptions({
513 * queryKey: ['post', postId, 'comments'],
514 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
515 * initialPageParam: 0,
516 * getNextPageParam: (lastPage) => lastPage.nextId,
517 * })
518 *
519 * function Comments({ postId }: { postId: string }) {
520 * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
521 *
522 * if (isPending) return 'Loading...'
523 * if (isError) return <span>Error: {error.message}</span>
524 *
525 * return (
526 * <ul>
527 * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
528 * </ul>
529 * )
530 * }
531 * ```
532 */
533export function infiniteQueryOptions<
534 TQueryFnData,
535 TError = DefaultError,
536 TData = InfiniteData<TQueryFnData>,
537 TQueryKey extends QueryKey = QueryKey,
538 TPageParam = unknown,
539>(
540 options: UndefinedInitialDataInfiniteOptions<
541 TQueryFnData,
542 TError,
543 TData,
544 TQueryKey,
545 TPageParam
546 >,
547): UndefinedInitialDataInfiniteOptions<
548 TQueryFnData,
549 TError,
550 TData,
551 TQueryKey,
552 TPageParam
553> &
554 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
555​
556export function infiniteQueryOptions(options: unknown) {
557 return options