Open History to see the last commit
1import type {
2 DefaultError,
3 InfiniteData,
4 InitialDataFunction,
5 NonUndefinedGuard,
6 OmitKeyof,
7 QueryKey,
8 QueryKeyWithDataTag,
9 SkipToken,
10} from '@tanstack/query-core'
11import type { CreateInfiniteQueryOptions } from './types'
12β
13/**
14 * The options accepted by the `infiniteQueryOptions` overload selected when no
15 * `initialData` is set β `data` may be `undefined` while the query is
16 * `pending`.
17 *
18 * @template TQueryFnData - The type of a single page, as your `queryFn`
19 * resolves it.
20 * @template TError - The type of errors your `queryFn` may throw.
21 * @template TData - The type `data` ends up as after `select` runs β defaults
22 * to `InfiniteData<TQueryFnData>`, the shape of all fetched pages plus their
23 * page params.
24 * @template TQueryKey - The type of your `queryKey`.
25 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch
26 * a given page.
27 */
28export type UndefinedInitialDataInfiniteOptions<
29 TQueryFnData,
30 TError = DefaultError,
31 TData = InfiniteData<TQueryFnData>,
32 TQueryKey extends QueryKey = QueryKey,
33 TPageParam = unknown,
34> = CreateInfiniteQueryOptions<
35 TQueryFnData,
36 TError,
37 TData,
38 TQueryKey,
39 TPageParam
40> & {
41 /**
42 * If set, this value will be used as the initial data for the query cache (as
43 * long as the query hasn't been created or cached yet). If set to a function,
44 * the function will be called **once** during the shared/root query
45 * initialization, and be expected to synchronously return the initial data.
46 * Initial data is considered stale by default unless a `staleTime` has been
47 * set. `initialData` **is persisted** to the cache.
48 */
49 initialData?:
50 | undefined
51 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
52 | InitialDataFunction<
53 NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
54 >
55}
56β
57/**
58 * The options accepted by the `infiniteQueryOptions` overload selected when no
59 * `initialData` is set and `queryFn` is not `skipToken` β same as
60 * {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be
61 * `skipToken`.
62 *
63 * @template TQueryFnData - The type of a single page, as your `queryFn`
64 * resolves it.
65 * @template TError - The type of errors your `queryFn` may throw.
66 * @template TData - The type `data` ends up as after `select` runs β defaults
67 * to `InfiniteData<TQueryFnData>`, the shape of all fetched pages plus their
68 * page params.
69 * @template TQueryKey - The type of your `queryKey`.
70 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch
71 * a given page.
72 */
73export type UnusedSkipTokenInfiniteOptions<
74 TQueryFnData,
75 TError = DefaultError,
76 TData = InfiniteData<TQueryFnData>,
77 TQueryKey extends QueryKey = QueryKey,
78 TPageParam = unknown,
79> = OmitKeyof<
80 CreateInfiniteQueryOptions<
81 TQueryFnData,
82 TError,
83 TData,
84 TQueryKey,
85 TPageParam
86 >,
87 'queryFn'
88> & {
89 /**
90 * `skipToken` is not allowed as a value here β this overload is selected when
91 * no `initialData` is set. If you don't intend to run the query yet, set
92 * `enabled: false` β omitting `queryFn` alone still triggers a fetch that
93 * fails with "Missing queryFn" unless `enabled` is `false` or a default query
94 * function has been defined. A default query function only supplies
95 * `queryFn`; it doesn't defer the fetch on its own.
96 */
97 queryFn?: Exclude<
98 CreateInfiniteQueryOptions<
99 TQueryFnData,
100 TError,
101 TData,
102 TQueryKey,
103 TPageParam
104 >['queryFn'],
105 SkipToken | undefined
106 >
107}
108β
109/**
110 * The options accepted by the `infiniteQueryOptions` overload selected when
111 * `initialData` is set β `data` is never `undefined` (unless a `select` changes
112 * `TData` to include `undefined`).
113 *
114 * @template TQueryFnData - The type of a single page, as your `queryFn`
115 * resolves it.
116 * @template TError - The type of errors your `queryFn` may throw.
117 * @template TData - The type `data` ends up as after `select` runs β defaults
118 * to `InfiniteData<TQueryFnData>`, the shape of all fetched pages plus their
119 * page params.
120 * @template TQueryKey - The type of your `queryKey`.
121 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch
122 * a given page.
123 */
124export type DefinedInitialDataInfiniteOptions<
125 TQueryFnData,
126 TError = DefaultError,
127 TData = InfiniteData<TQueryFnData>,
128 TQueryKey extends QueryKey = QueryKey,
129 TPageParam = unknown,
130> = CreateInfiniteQueryOptions<
131 TQueryFnData,
132 TError,
133 TData,
134 TQueryKey,
135 TPageParam
136> & {
137 /**
138 * If set, this value will be used as the initial data for the query cache (as
139 * long as the query hasn't been created or cached yet). If set to a function,
140 * the function will be called **once** during the shared/root query
141 * initialization, and be expected to synchronously return the initial data.
142 * Initial data is considered stale by default unless a `staleTime` has been
143 * set. `initialData` **is persisted** to the cache.
144 */
145 initialData:
146 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
147 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
148 | undefined
149}
150β
151/**
152 * You can generally pass everything to `infiniteQueryOptions` that you can also
153 * pass to `injectInfiniteQuery`. These options can be shared across functions
154 * and imperative APIs such as `queryClient.fetchInfiniteQuery`.
155 * `options.queryKey` is required and is the query key to generate options for.
156 *
157 * This overload is selected when `initialData` is set.
158 *
159 * @remarks
160 * See {@link injectInfiniteQuery} for examples that fetch further pages, from
161 * a button click or automatically as the user scrolls.
162 * @example
163 * ```angular-ts
164 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
165 *
166 * export const projectsOptions = infiniteQueryOptions({
167 * queryKey: ['projects'],
168 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
169 * initialPageParam: 0,
170 * getNextPageParam: (lastPage) => lastPage.nextId,
171 * initialData: { pages: [], pageParams: [] },
172 * })
173 *
174 * @Component({
175 * selector: 'projects',
176 * template: `
177 * <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β even if a
178 * refetch fails, so the list stays visible alongside the error. -->
179 * <ul>
180 * @for (page of projectsQuery.data().pages; track $index) {
181 * @for (project of page.projects; track project.id) {
182 * <li>{{ project.name }}</li>
183 * }
184 * }
185 * </ul>
186 * `,
187 * })
188 * export class Projects {
189 * readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)
190 * }
191 * ```
192 *
193 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β
194 * everything you can pass to `injectInfiniteQuery`, with `initialData` set.
195 * @returns The same options object, typed so that `queryKey` carries the
196 * inferred data type.
197 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
198 */
199export function infiniteQueryOptions<
200 TQueryFnData,
201 TError = DefaultError,
202 TData = InfiniteData<TQueryFnData>,
203 TQueryKey extends QueryKey = QueryKey,
204 TPageParam = unknown,
205>(
206 options: DefinedInitialDataInfiniteOptions<
207 TQueryFnData,
208 TError,
209 TData,
210 TQueryKey,
211 TPageParam
212 >,
213): DefinedInitialDataInfiniteOptions<
214 TQueryFnData,
215 TError,
216 TData,
217 TQueryKey,
218 TPageParam
219> &
220 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
221β
222/**
223 * You can generally pass everything to `infiniteQueryOptions` that you can also
224 * pass to `injectInfiniteQuery`. These options can be shared across functions
225 * and imperative APIs such as `queryClient.fetchInfiniteQuery`.
226 * `options.queryKey` is required and is the query key to generate options for.
227 *
228 * @remarks
229 * See {@link injectInfiniteQuery} for examples that fetch further pages, from
230 * a button click or automatically as the user scrolls.
231 * @example
232 * A parameterized factory, so the same options object can be reused per `postId`:
233 * ```angular-ts
234 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
235 *
236 * export const commentsOptions = (postId: string) =>
237 * infiniteQueryOptions({
238 * queryKey: ['post', postId, 'comments'],
239 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
240 * initialPageParam: 0,
241 * getNextPageParam: (lastPage) => lastPage.nextId,
242 * })
243 *
244 * @Component({
245 * selector: 'comments',
246 * template: `
247 * @if (commentsQuery.isPending()) {
248 * Loading...
249 * } @else if (commentsQuery.isError()) {
250 * <span>Error: {{ commentsQuery.error()?.message }}</span>
251 * } @else {
252 * <ul>
253 * @for (page of commentsQuery.data().pages; track $index) {
254 * @for (comment of page.comments; track comment.id) {
255 * <li>{{ comment.text }}</li>
256 * }
257 * }
258 * </ul>
259 * }
260 * `,
261 * })
262 * export class Comments {
263 * readonly postId = signal('1')
264 * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
265 * }
266 * ```
267 *
268 * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β
269 * everything you can pass to `injectInfiniteQuery`.
270 * @returns The same options object, typed so that `queryKey` carries the
271 * inferred data type.
272 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
273 */
274export function infiniteQueryOptions<
275 TQueryFnData,
276 TError = DefaultError,
277 TData = InfiniteData<TQueryFnData>,
278 TQueryKey extends QueryKey = QueryKey,
279 TPageParam = unknown,
280>(
281 options: UnusedSkipTokenInfiniteOptions<
282 TQueryFnData,
283 TError,
284 TData,
285 TQueryKey,
286 TPageParam
287 >,
288): UnusedSkipTokenInfiniteOptions<
289 TQueryFnData,
290 TError,
291 TData,
292 TQueryKey,
293 TPageParam
294> &
295 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
296β
297/**
298 * You can generally pass everything to `infiniteQueryOptions` that you can also
299 * pass to `injectInfiniteQuery`. These options can be shared across functions
300 * and imperative APIs such as `queryClient.fetchInfiniteQuery`.
301 * `options.queryKey` is required and is the query key to generate options for.
302 *
303 * @remarks
304 * See {@link injectInfiniteQuery} for examples that fetch further pages (from
305 * a button click or automatically as the user scrolls) and that use
306 * `skipToken` to disable the query until `postId` is set.
307 * @example
308 * A parameterized factory, so the same options object can be reused per `postId`:
309 * ```angular-ts
310 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
311 *
312 * export const commentsOptions = (postId: string) =>
313 * infiniteQueryOptions({
314 * queryKey: ['post', postId, 'comments'],
315 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
316 * initialPageParam: 0,
317 * getNextPageParam: (lastPage) => lastPage.nextId,
318 * })
319 *
320 * @Component({
321 * selector: 'comments',
322 * template: `
323 * @if (commentsQuery.isPending()) {
324 * Loading...
325 * } @else if (commentsQuery.isError()) {
326 * <span>Error: {{ commentsQuery.error()?.message }}</span>
327 * } @else {
328 * <ul>
329 * @for (page of commentsQuery.data().pages; track $index) {
330 * @for (comment of page.comments; track comment.id) {
331 * <li>{{ comment.text }}</li>
332 * }
333 * }
334 * </ul>
335 * }
336 * `,
337 * })
338 * export class Comments {
339 * readonly postId = signal('1')
340 * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
341 * }
342 * ```
343 *
344 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β
345 * everything you can pass to `injectInfiniteQuery`.
346 * @returns The same options object, typed so that `queryKey` carries the
347 * inferred data type.
348 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
349 */
350export function infiniteQueryOptions<
351 TQueryFnData,
352 TError = DefaultError,
353 TData = InfiniteData<TQueryFnData>,
354 TQueryKey extends QueryKey = QueryKey,
355 TPageParam = unknown,
356>(
357 options: UndefinedInitialDataInfiniteOptions<
358 TQueryFnData,
359 TError,
360 TData,
361 TQueryKey,
362 TPageParam
363 >,
364): UndefinedInitialDataInfiniteOptions<
365 TQueryFnData,
366 TError,
367 TData,
368 TQueryKey,
369 TPageParam
370> &
371 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
372β
373export function infiniteQueryOptions(options: unknown) {
374 return options
375}