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