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 | undefined
133}
134β
135/**
136 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
137 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
138 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
139 * for.
140 *
141 * This overload is selected when `initialData` is set.
142 *
143 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
144 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
145 * `injectInfiniteQuery`, with `initialData` set.
146 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
147 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or
148 * automatically as the user scrolls.
149 *
150 * @example
151 * ```angular-ts
152 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
153 *
154 * export const projectsOptions = infiniteQueryOptions({
155 * queryKey: ['projects'],
156 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
157 * initialPageParam: 0,
158 * getNextPageParam: (lastPage) => lastPage.nextId,
159 * initialData: { pages: [], pageParams: [] },
160 * })
161 *
162 * @Component({
163 * selector: 'projects',
164 * template: `
165 * <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β even if a
166 * refetch fails, so the list stays visible alongside the error. -->
167 * <ul>
168 * @for (page of projectsQuery.data().pages; track $index) {
169 * @for (project of page.projects; track project.id) {
170 * <li>{{ project.name }}</li>
171 * }
172 * }
173 * </ul>
174 * `,
175 * })
176 * export class Projects {
177 * readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)
178 * }
179 * ```
180 */
181export function infiniteQueryOptions<
182 TQueryFnData,
183 TError = DefaultError,
184 TData = InfiniteData<TQueryFnData>,
185 TQueryKey extends QueryKey = QueryKey,
186 TPageParam = unknown,
187>(
188 options: DefinedInitialDataInfiniteOptions<
189 TQueryFnData,
190 TError,
191 TData,
192 TQueryKey,
193 TPageParam
194 >,
195): DefinedInitialDataInfiniteOptions<
196 TQueryFnData,
197 TError,
198 TData,
199 TQueryKey,
200 TPageParam
201> &
202 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
203β
204/**
205 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
206 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
207 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
208 * for.
209 *
210 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
211 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or
212 * automatically as the user scrolls.
213 *
214 * @example
215 * A parameterized factory, so the same options object can be reused per `postId`:
216 * ```angular-ts
217 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
218 *
219 * export const commentsOptions = (postId: string) =>
220 * infiniteQueryOptions({
221 * queryKey: ['post', postId, 'comments'],
222 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
223 * initialPageParam: 0,
224 * getNextPageParam: (lastPage) => lastPage.nextId,
225 * })
226 *
227 * @Component({
228 * selector: 'comments',
229 * template: `
230 * @if (commentsQuery.isPending()) {
231 * Loading...
232 * } @else if (commentsQuery.isError()) {
233 * <span>Error: {{ commentsQuery.error()?.message }}</span>
234 * } @else {
235 * <ul>
236 * @for (page of commentsQuery.data().pages; track $index) {
237 * @for (comment of page.comments; track comment.id) {
238 * <li>{{ comment.text }}</li>
239 * }
240 * }
241 * </ul>
242 * }
243 * `,
244 * })
245 * export class Comments {
246 * readonly postId = signal('1')
247 * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
248 * }
249 * ```
250 *
251 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
252 * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use β everything you can pass to
253 * `injectInfiniteQuery`.
254 */
255export function infiniteQueryOptions<
256 TQueryFnData,
257 TError = DefaultError,
258 TData = InfiniteData<TQueryFnData>,
259 TQueryKey extends QueryKey = QueryKey,
260 TPageParam = unknown,
261>(
262 options: UnusedSkipTokenInfiniteOptions<
263 TQueryFnData,
264 TError,
265 TData,
266 TQueryKey,
267 TPageParam
268 >,
269): UnusedSkipTokenInfiniteOptions<
270 TQueryFnData,
271 TError,
272 TData,
273 TQueryKey,
274 TPageParam
275> &
276 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
277β
278/**
279 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to
280 * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as
281 * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
282 * for.
283 *
284 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
285 * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages (from a button click or
286 * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
287 *
288 * @example
289 * A parameterized factory, so the same options object can be reused per `postId`:
290 * ```angular-ts
291 * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query-experimental'
292 *
293 * export const commentsOptions = (postId: string) =>
294 * infiniteQueryOptions({
295 * queryKey: ['post', postId, 'comments'],
296 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
297 * initialPageParam: 0,
298 * getNextPageParam: (lastPage) => lastPage.nextId,
299 * })
300 *
301 * @Component({
302 * selector: 'comments',
303 * template: `
304 * @if (commentsQuery.isPending()) {
305 * Loading...
306 * } @else if (commentsQuery.isError()) {
307 * <span>Error: {{ commentsQuery.error()?.message }}</span>
308 * } @else {
309 * <ul>
310 * @for (page of commentsQuery.data().pages; track $index) {
311 * @for (comment of page.comments; track comment.id) {
312 * <li>{{ comment.text }}</li>
313 * }
314 * }
315 * </ul>
316 * }
317 * `,
318 * })
319 * export class Comments {
320 * readonly postId = signal('1')
321 * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))
322 * }
323 * ```
324 *
325 * @see {@link injectInfiniteQuery} to run an infinite query with these options.
326 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β everything you can pass to
327 * `injectInfiniteQuery`.
328 */
329export function infiniteQueryOptions<
330 TQueryFnData,
331 TError = DefaultError,
332 TData = InfiniteData<TQueryFnData>,
333 TQueryKey extends QueryKey = QueryKey,
334 TPageParam = unknown,
335>(
336 options: UndefinedInitialDataInfiniteOptions<
337 TQueryFnData,
338 TError,
339 TData,
340 TQueryKey,
341 TPageParam
342 >,
343): UndefinedInitialDataInfiniteOptions<
344 TQueryFnData,
345 TError,
346 TData,
347 TQueryKey,
348 TPageParam
349> &
350 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
351β
352export function infiniteQueryOptions(options: unknown) {
353 return options
354}