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