Open History to see the last commit
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 CreateInfiniteQueryOptions,
14 CreateInfiniteQueryOptionsBase,
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 >
31}
32β
33type RequiredInitialData<TQueryFnData, TPageParam> = {
34 initialData:
35 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
36 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
37 | undefined
38}
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> = CreateInfiniteQueryOptions<
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> = CreateInfiniteQueryOptionsBase<
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 CreateInfiniteQueryOptionsBase<
98 TQueryFnData,
99 TError,
100 TData,
101 TQueryKey,
102 TPageParam,
103 InfiniteQueryMode
104 >
105>
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 CreateInfiniteQueryOptions<
127 TQueryFnData,
128 TError,
129 TData,
130 TQueryKey,
131 TPageParam,
132 TMode
133 >
134>
135β
136/** Manual query options with defined initial data. */
137export type ManualDefinedInitialDataInfiniteOptions<
138 TQueryFnData,
139 TError = DefaultError,
140 TData = InfiniteData<TQueryFnData>,
141 TQueryKey extends QueryKey = QueryKey,
142 TPageParam = unknown,
143> = CreateInfiniteQueryOptionsBase<
144 TQueryFnData,
145 TError,
146 TData,
147 TQueryKey,
148 TPageParam,
149 InfiniteQueryMode
150> &
151 RequiredInitialData<TQueryFnData, TPageParam>
152β
153/**
154 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set β `data` is
155 * never `undefined` (unless a `select` changes `TData` to include `undefined`).
156 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
157 * @template TError - The type of errors your `queryFn` may throw.
158 * @template TData - The type `data` ends up as after `select` runs β defaults to `InfiniteData<TQueryFnData>`,
159 * the shape of all fetched pages plus their page params.
160 * @template TQueryKey - The type of your `queryKey`.
161 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
162 */
163export type DefinedInitialDataInfiniteOptions<
164 TQueryFnData,
165 TError = DefaultError,
166 TData = InfiniteData<TQueryFnData>,
167 TQueryKey extends QueryKey = QueryKey,
168 TPageParam = unknown,
169 TMode extends InfiniteQueryMode | undefined = InfiniteQueryMode | undefined,
170> = CreateInfiniteQueryOptions<
171 TQueryFnData,
172 TError,
173 TData,
174 TQueryKey,
175 TPageParam,
176 TMode
177> &
178 RequiredInitialData<TQueryFnData, TPageParam>
179β
180/**
181 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
182 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
183 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
184 * for.
185 *
186 * This overload is selected when `initialData` is set.
187 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
188 * `injectInfiniteQuery`, with `initialData` set.
189 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
190 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or
191 * automatically as the user scrolls.
192 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
193 * @example
194 * ```angular-ts
195 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
196 *
197 * export const projectsOptions = infiniteQueryOptions({
198 * queryKey: ['projects'],
199 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
200 * initialPageParam: 0,
201 * getNextPageParam: (lastPage) => lastPage.nextId,
202 * initialData: { pages: [], pageParams: [] },
203 * })
204 *
205 * @Component({
206 * selector: 'projects',
207 * template: `
208 * <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β even if a
209 * refetch fails, so the list stays visible alongside the error. -->
210 * <ul>
211 * @for (page of projectsQuery.data().pages; track $index) {
212 * @for (project of page.projects; track project.id) {
213 * <li>{{ project.name }}</li>
214 * }
215 * }
216 * </ul>
217 * `,
218 * })
219 * export class Projects {
220 * readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)
221 * }
222 * ```
223 */
224export function infiniteQueryOptions<
225 TQueryFnData,
226 TError = DefaultError,
227 TData = InfiniteData<TQueryFnData>,
228 TQueryKey extends QueryKey = QueryKey,
229 TPageParam = unknown,
230>(
231 options: DefinedInitialDataInfiniteOptions<
232 TQueryFnData,
233 TError,
234 TData,
235 TQueryKey,
236 TPageParam,
237 undefined
238 >,
239): DefinedInitialDataInfiniteOptions<
240 TQueryFnData,
241 TError,
242 TData,
243 TQueryKey,
244 TPageParam,
245 undefined
246> &
247 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
248export function infiniteQueryOptions<
249 TQueryFnData,
250 TError = DefaultError,
251 TData = InfiniteData<TQueryFnData>,
252 TQueryKey extends QueryKey = QueryKey,
253 TPageParam = unknown,
254>(
255 options: ManualDefinedInitialDataInfiniteOptions<
256 TQueryFnData,
257 TError,
258 TData,
259 TQueryKey,
260 TPageParam
261 >,
262): ManualDefinedInitialDataInfiniteOptions<
263 TQueryFnData,
264 TError,
265 TData,
266 TQueryKey,
267 TPageParam
268> &
269 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
270/**
271 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
272 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
273 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
274 * for.
275 * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β everything you can pass to
276 * `injectInfiniteQuery`.
277 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
278 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or
279 * automatically as the user scrolls.
280 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
281 * @example
282 * A parameterized factory, so the same options object can be reused per `postId`:
283 * ```angular-ts
284 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
285 *
286 * export const commentsOptions = (postId: string) =>
287 * infiniteQueryOptions({
288 * queryKey: ['post', postId, 'comments'],
289 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
290 * initialPageParam: 0,
291 * getNextPageParam: (lastPage) => lastPage.nextId,
292 * })
293 *
294 * @Component({
295 * selector: 'comments',
296 * template: `
297 * @if (commentsQuery.isPending()) {
298 * Loading...
299 * } @else if (commentsQuery.isError()) {
300 * <span>Error: {{ commentsQuery.error()?.message }}</span>
301 * } @else {
302 * <ul>
303 * @for (page of commentsQuery.data().pages; track $index) {
304 * @for (comment of page.comments; track comment.id) {
305 * <li>{{ comment.text }}</li>
306 * }
307 * }
308 * </ul>
309 * }
310 * `,
311 * })
312 * export class Comments {
313 * readonly postId = signal('1')
314 * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
315 * }
316 * ```
317 */
318export function infiniteQueryOptions<
319 TQueryFnData,
320 TError = DefaultError,
321 TData = InfiniteData<TQueryFnData>,
322 TQueryKey extends QueryKey = QueryKey,
323 TPageParam = unknown,
324>(
325 options: UnusedSkipTokenInfiniteOptions<
326 TQueryFnData,
327 TError,
328 TData,
329 TQueryKey,
330 TPageParam,
331 undefined
332 >,
333): UnusedSkipTokenInfiniteOptions<
334 TQueryFnData,
335 TError,
336 TData,
337 TQueryKey,
338 TPageParam,
339 undefined
340> &
341 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
342export function infiniteQueryOptions<
343 TQueryFnData,
344 TError = DefaultError,
345 TData = InfiniteData<TQueryFnData>,
346 TQueryKey extends QueryKey = QueryKey,
347 TPageParam = unknown,
348>(
349 options: UnusedSkipTokenManualInfiniteOptions<
350 TQueryFnData,
351 TError,
352 TData,
353 TQueryKey,
354 TPageParam
355 >,
356): UnusedSkipTokenManualInfiniteOptions<
357 TQueryFnData,
358 TError,
359 TData,
360 TQueryKey,
361 TPageParam
362> &
363 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
364/**
365 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
366 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
367 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
368 * for.
369 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β everything you can pass to
370 * `injectInfiniteQuery`.
371 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
372 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages (from a button click or
373 * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
374 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
375 * @example
376 * A parameterized factory, so the same options object can be reused per `postId`:
377 * ```angular-ts
378 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
379 *
380 * export const commentsOptions = (postId: string) =>
381 * infiniteQueryOptions({
382 * queryKey: ['post', postId, 'comments'],
383 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
384 * initialPageParam: 0,
385 * getNextPageParam: (lastPage) => lastPage.nextId,
386 * })
387 *
388 * @Component({
389 * selector: 'comments',
390 * template: `
391 * @if (commentsQuery.isPending()) {
392 * Loading...
393 * } @else if (commentsQuery.isError()) {
394 * <span>Error: {{ commentsQuery.error()?.message }}</span>
395 * } @else {
396 * <ul>
397 * @for (page of commentsQuery.data().pages; track $index) {
398 * @for (comment of page.comments; track comment.id) {
399 * <li>{{ comment.text }}</li>
400 * }
401 * }
402 * </ul>
403 * }
404 * `,
405 * })
406 * export class Comments {
407 * readonly postId = signal('1')
408 * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
409 * }
410 * ```
411 */
412export function infiniteQueryOptions<
413 TQueryFnData,
414 TError = DefaultError,
415 TData = InfiniteData<TQueryFnData>,
416 TQueryKey extends QueryKey = QueryKey,
417 TPageParam = unknown,
418>(
419 options: UndefinedInitialDataInfiniteOptions<
420 TQueryFnData,
421 TError,
422 TData,
423 TQueryKey,
424 TPageParam,
425 undefined
426 >,
427): UndefinedInitialDataInfiniteOptions<
428 TQueryFnData,
429 TError,
430 TData,
431 TQueryKey,
432 TPageParam,
433 undefined
434> &
435 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
436export function infiniteQueryOptions<
437 TQueryFnData,
438 TError = DefaultError,
439 TData = InfiniteData<TQueryFnData>,
440 TQueryKey extends QueryKey = QueryKey,
441 TPageParam = unknown,
442>(
443 options: ManualUndefinedInitialDataInfiniteOptions<
444 TQueryFnData,
445 TError,
446 TData,
447 TQueryKey,
448 TPageParam
449 >,
450): ManualUndefinedInitialDataInfiniteOptions<
451 TQueryFnData,
452 TError,
453 TData,
454 TQueryKey,
455 TPageParam
456> &
457 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
458/**
459 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
460 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
461 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
462 * for.
463 *
464 * This overload is selected when `initialData` is set.
465 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
466 * `injectInfiniteQuery`, with `initialData` set.
467 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
468 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or
469 * automatically as the user scrolls.
470 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
471 * @example
472 * ```angular-ts
473 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
474 *
475 * export const projectsOptions = infiniteQueryOptions({
476 * queryKey: ['projects'],
477 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
478 * initialPageParam: 0,
479 * getNextPageParam: (lastPage) => lastPage.nextId,
480 * initialData: { pages: [], pageParams: [] },
481 * })
482 *
483 * @Component({
484 * selector: 'projects',
485 * template: `
486 * <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β even if a
487 * refetch fails, so the list stays visible alongside the error. -->
488 * <ul>
489 * @for (page of projectsQuery.data().pages; track $index) {
490 * @for (project of page.projects; track project.id) {
491 * <li>{{ project.name }}</li>
492 * }
493 * }
494 * </ul>
495 * `,
496 * })
497 * export class Projects {
498 * readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)
499 * }
500 * ```
501 */
502export function infiniteQueryOptions<
503 TQueryFnData,
504 TError = DefaultError,
505 TData = InfiniteData<TQueryFnData>,
506 TQueryKey extends QueryKey = QueryKey,
507 TPageParam = unknown,
508>(
509 options: DefinedInitialDataInfiniteOptions<
510 TQueryFnData,
511 TError,
512 TData,
513 TQueryKey,
514 TPageParam
515 >,
516): DefinedInitialDataInfiniteOptions<
517 TQueryFnData,
518 TError,
519 TData,
520 TQueryKey,
521 TPageParam
522> &
523 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
524export function infiniteQueryOptions<
525 TQueryFnData,
526 TError = DefaultError,
527 TData = InfiniteData<TQueryFnData>,
528 TQueryKey extends QueryKey = QueryKey,
529 TPageParam = unknown,
530>(
531 options: UndefinedInitialDataInfiniteOptions<
532 TQueryFnData,
533 TError,
534 TData,
535 TQueryKey,
536 TPageParam
537 >,
538): UndefinedInitialDataInfiniteOptions<
539 TQueryFnData,
540 TError,
541 TData,
542 TQueryKey,
543 TPageParam
544> &
545 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
546export function infiniteQueryOptions(options: unknown) {
547 return options
548}