Open History to see the last commit
1import type {
2 DefaultError,
3 InfiniteData,
4 InfiniteQueryMode,
5 NonUndefinedGuard,
6 QueryKey,
7 QueryKeyWithDataTag,
8} from '@tanstack/query-core'
9import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
10β
11/**
12 * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set β `data`
13 * may be `undefined` while the query is `pending`.
14 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
15 * @template TError - The type of errors your `queryFn` may throw.
16 * @template TData - The type `data` ends up as after `select` runs β defaults to `InfiniteData<TQueryFnData>`,
17 * the shape of all fetched pages plus their page params.
18 * @template TQueryKey - The type of your `queryKey`.
19 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
20 */
21export type UndefinedInitialDataInfiniteOptions<
22 TQueryFnData,
23 TError = DefaultError,
24 TData = InfiniteData<TQueryFnData>,
25 TQueryKey extends QueryKey = QueryKey,
26 TPageParam = unknown,
27 TMode extends InfiniteQueryMode | undefined = undefined,
28> = UseInfiniteQueryOptions<
29 TQueryFnData,
30 TError,
31 TData,
32 TQueryKey,
33 TPageParam,
34 TMode
35> & {
36 initialData?: undefined
37}
38β
39/**
40 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set β `data` is
41 * never `undefined` (unless a `select` changes `TData` to include `undefined`).
42 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
43 * @template TError - The type of errors your `queryFn` may throw.
44 * @template TData - The type `data` ends up as after `select` runs β defaults to `InfiniteData<TQueryFnData>`,
45 * the shape of all fetched pages plus their page params.
46 * @template TQueryKey - The type of your `queryKey`.
47 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
48 */
49export type DefinedInitialDataInfiniteOptions<
50 TQueryFnData,
51 TError = DefaultError,
52 TData = InfiniteData<TQueryFnData>,
53 TQueryKey extends QueryKey = QueryKey,
54 TPageParam = unknown,
55 TMode extends InfiniteQueryMode | undefined = undefined,
56> = UseInfiniteQueryOptions<
57 TQueryFnData,
58 TError,
59 TData,
60 TQueryKey,
61 TPageParam,
62 TMode
63> & {
64 /**
65 * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
66 * created or cached yet). If set to a function, the function will be called **once** during the shared/root
67 * query initialization, and be expected to synchronously return the initial data. Initial data is
68 * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
69 * cache.
70 */
71 initialData:
72 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
73 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
74}
75β
76/**
77 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
78 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
79 * `options.queryKey` is required and is the query key to generate options for.
80 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β everything you can pass to
81 * `useInfiniteQuery`.
82 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
83 * @see {@link useInfiniteQuery} to run an infinite query with these options.
84 * @example
85 * ```vue
86 * <script setup lang="ts">
87 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/vue-query'
88 *
89 * const projectsOptions = infiniteQueryOptions({
90 * queryKey: ['projects'],
91 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
92 * initialPageParam: 0,
93 * getNextPageParam: (lastPage) => lastPage.nextId,
94 * })
95 *
96 * const { data, isError, error, fetchNextPage } = useInfiniteQuery(projectsOptions)
97 * </script>
98 * ```
99 */
100export function infiniteQueryOptions<
101 TQueryFnData,
102 TError = DefaultError,
103 TData = InfiniteData<TQueryFnData>,
104 TQueryKey extends QueryKey = QueryKey,
105 TPageParam = unknown,
106>(
107 options: UndefinedInitialDataInfiniteOptions<
108 TQueryFnData,
109 TError,
110 TData,
111 TQueryKey,
112 TPageParam,
113 undefined
114 >,
115): UndefinedInitialDataInfiniteOptions<
116 TQueryFnData,
117 TError,
118 TData,
119 TQueryKey,
120 TPageParam,
121 undefined
122> &
123 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
124export function infiniteQueryOptions<
125 TQueryFnData,
126 TError = DefaultError,
127 TData = InfiniteData<TQueryFnData>,
128 TQueryKey extends QueryKey = QueryKey,
129 TPageParam = unknown,
130>(
131 options: UndefinedInitialDataInfiniteOptions<
132 TQueryFnData,
133 TError,
134 TData,
135 TQueryKey,
136 TPageParam,
137 InfiniteQueryMode
138 >,
139): UndefinedInitialDataInfiniteOptions<
140 TQueryFnData,
141 TError,
142 TData,
143 TQueryKey,
144 TPageParam,
145 InfiniteQueryMode
146> &
147 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
148β
149export function infiniteQueryOptions<
150 TQueryFnData,
151 TError = DefaultError,
152 TData = InfiniteData<TQueryFnData>,
153 TQueryKey extends QueryKey = QueryKey,
154 TPageParam = unknown,
155>(
156 options: DefinedInitialDataInfiniteOptions<
157 TQueryFnData,
158 TError,
159 TData,
160 TQueryKey,
161 TPageParam,
162 undefined
163 >,
164): DefinedInitialDataInfiniteOptions<
165 TQueryFnData,
166 TError,
167 TData,
168 TQueryKey,
169 TPageParam,
170 undefined
171> &
172 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
173β
174export function infiniteQueryOptions<
175 TQueryFnData,
176 TError = DefaultError,
177 TData = InfiniteData<TQueryFnData>,
178 TQueryKey extends QueryKey = QueryKey,
179 TPageParam = unknown,
180>(
181 options: DefinedInitialDataInfiniteOptions<
182 TQueryFnData,
183 TError,
184 TData,
185 TQueryKey,
186 TPageParam,
187 InfiniteQueryMode
188 >,
189): DefinedInitialDataInfiniteOptions<
190 TQueryFnData,
191 TError,
192 TData,
193 TQueryKey,
194 TPageParam,
195 InfiniteQueryMode
196> &
197 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
198β
199export function infiniteQueryOptions<
200 TQueryFnData,
201 TError = DefaultError,
202 TData = InfiniteData<TQueryFnData>,
203 TQueryKey extends QueryKey = QueryKey,
204 TPageParam = unknown,
205>(
206 options: UndefinedInitialDataInfiniteOptions<
207 TQueryFnData,
208 TError,
209 TData,
210 TQueryKey,
211 TPageParam
212 >,
213): UndefinedInitialDataInfiniteOptions<
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 pass to `useInfiniteQuery`.
224 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
225 * `options.queryKey` is required and is the query key to generate options for.
226 *
227 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
228 * a `select` changes `TData` to include `undefined`).
229 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
230 * `useInfiniteQuery`, with `initialData` set.
231 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
232 * @see {@link useInfiniteQuery} to run an infinite query with these options.
233 * @example
234 * ```vue
235 * <script setup lang="ts">
236 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/vue-query'
237 *
238 * const projectsOptions = infiniteQueryOptions({
239 * queryKey: ['projects'],
240 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
241 * initialPageParam: 0,
242 * getNextPageParam: (lastPage) => lastPage.nextId,
243 * initialData: { pages: [], pageParams: [] },
244 * })
245 *
246 * // `data` is never `undefined`, thanks to `initialData` β even if a refetch fails, so the
247 * // list stays visible alongside the error.
248 * const { data, isError, error } = useInfiniteQuery(projectsOptions)
249 * </script>
250 * ```
251 */
252export function infiniteQueryOptions<
253 TQueryFnData,
254 TError = DefaultError,
255 TData = InfiniteData<TQueryFnData>,
256 TQueryKey extends QueryKey = QueryKey,
257 TPageParam = unknown,
258>(
259 options: DefinedInitialDataInfiniteOptions<
260 TQueryFnData,
261 TError,
262 TData,
263 TQueryKey,
264 TPageParam
265 >,
266): DefinedInitialDataInfiniteOptions<
267 TQueryFnData,
268 TError,
269 TData,
270 TQueryKey,
271 TPageParam
272> &
273 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
274β
275export function infiniteQueryOptions(options: unknown) {
276 return options
277}