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