Open History to see the last commit
1import type {
2 DefaultError,
3 InfiniteData,
4 InitialDataFunction,
5 NonUndefinedGuard,
6 QueryKey,
7 QueryKeyWithDataTag,
8} from '@tanstack/query-core'
9import type { CreateInfiniteQueryOptions } from './types.js'
10β
11/**
12 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is omitted or may
13 * be `undefined` β `data` may be `undefined` while the query is `pending`.
14 */
15export type UndefinedInitialDataInfiniteOptions<
16 TQueryFnData = unknown,
17 TError = DefaultError,
18 TData = InfiniteData<TQueryFnData>,
19 TQueryKey extends QueryKey = QueryKey,
20 TPageParam = unknown,
21> = CreateInfiniteQueryOptions<
22 TQueryFnData,
23 TError,
24 TData,
25 TQueryKey,
26 TPageParam
27> & {
28 initialData?:
29 | undefined
30 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
31 | InitialDataFunction<
32 NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
33 >
34}
35β
36/**
37 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be
38 * defined β `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
39 */
40export type DefinedInitialDataInfiniteOptions<
41 TQueryFnData = unknown,
42 TError = DefaultError,
43 TData = InfiniteData<TQueryFnData>,
44 TQueryKey extends QueryKey = QueryKey,
45 TPageParam = unknown,
46> = CreateInfiniteQueryOptions<
47 TQueryFnData,
48 TError,
49 TData,
50 TQueryKey,
51 TPageParam
52> & {
53 initialData:
54 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
55 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
56}
57β
58/**
59 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `createInfiniteQuery`.
60 * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
61 * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
62 *
63 * This overload is selected when `initialData` is known to be defined.
64 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
65 * `createInfiniteQuery`, with `initialData` set.
66 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
67 * @see {@link createInfiniteQuery} to run an infinite query with these options.
68 * @example
69 * `initialData` skips the loading state on first render β even if a refetch fails, the list stays
70 * visible alongside the error:
71 * ```svelte
72 * <script lang="ts">
73 * import { infiniteQueryOptions, createInfiniteQuery } from '@tanstack/svelte-query'
74 *
75 * const projectsOptions = infiniteQueryOptions({
76 * queryKey: ['projects'],
77 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
78 * initialPageParam: 0,
79 * getNextPageParam: (lastPage) => lastPage.nextId,
80 * initialData: { pages: [], pageParams: [] },
81 * })
82 *
83 * const query = createInfiniteQuery(() => projectsOptions)
84 * </script>
85 *
86 * {#if query.isError}
87 * <span>Error: {query.error.message}</span>
88 * {/if}
89 * <ul>
90 * {#each query.data.pages as page}
91 * {#each page.projects as project (project.id)}
92 * <li>{project.name}</li>
93 * {/each}
94 * {/each}
95 * </ul>
96 * ```
97 */
98export function infiniteQueryOptions<
99 TQueryFnData,
100 TError = DefaultError,
101 TData = InfiniteData<TQueryFnData>,
102 TQueryKey extends QueryKey = QueryKey,
103 TPageParam = unknown,
104>(
105 options: DefinedInitialDataInfiniteOptions<
106 TQueryFnData,
107 TError,
108 TData,
109 TQueryKey,
110 TPageParam
111 >,
112): DefinedInitialDataInfiniteOptions<
113 TQueryFnData,
114 TError,
115 TData,
116 TQueryKey,
117 TPageParam
118> &
119 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
120β
121/**
122 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `createInfiniteQuery`.
123 * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
124 * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
125 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β everything you can pass to
126 * `createInfiniteQuery`.
127 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
128 * @see {@link createInfiniteQuery} to run an infinite query with these options.
129 * @example
130 * A parameterized factory, so the same options object can be reused per `postId`:
131 * ```svelte
132 * <script lang="ts">
133 * import { infiniteQueryOptions, createInfiniteQuery } from '@tanstack/svelte-query'
134 *
135 * let { postId }: { postId: string } = $props()
136 *
137 * const commentsOptions = (postId: string) =>
138 * infiniteQueryOptions({
139 * queryKey: ['post', postId, 'comments'],
140 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
141 * initialPageParam: 0,
142 * getNextPageParam: (lastPage) => lastPage.nextId,
143 * })
144 *
145 * const query = createInfiniteQuery(() => commentsOptions(postId))
146 * </script>
147 *
148 * {#if query.isPending}
149 * Loading...
150 * {:else if query.isError}
151 * <span>Error: {query.error.message}</span>
152 * {:else}
153 * <ul>
154 * {#each query.data.pages as page}
155 * {#each page.comments as comment (comment.id)}
156 * <li>{comment.text}</li>
157 * {/each}
158 * {/each}
159 * </ul>
160 * {/if}
161 * ```
162 */
163export function infiniteQueryOptions<
164 TQueryFnData,
165 TError = DefaultError,
166 TData = InfiniteData<TQueryFnData>,
167 TQueryKey extends QueryKey = QueryKey,
168 TPageParam = unknown,
169>(
170 options: UndefinedInitialDataInfiniteOptions<
171 TQueryFnData,
172 TError,
173 TData,
174 TQueryKey,
175 TPageParam
176 >,
177): UndefinedInitialDataInfiniteOptions<
178 TQueryFnData,
179 TError,
180 TData,
181 TQueryKey,
182 TPageParam
183> &
184 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
185β
186export function infiniteQueryOptions(options: unknown) {
187 return options
188}