Open History to see the last commit
1import type {
2 DefaultError,
3 InfiniteData,
4 NonUndefinedGuard,
5 QueryKey,
6 QueryKeyWithDataTag,
7} from '@tanstack/query-core'
8import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
9β
10/**
11 * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set β `data`
12 * may be `undefined` while the query is `pending`.
13 *
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> = UseInfiniteQueryOptions<
28 TQueryFnData,
29 TError,
30 TData,
31 TQueryKey,
32 TPageParam
33> & {
34 initialData?: undefined
35}
36β
37/**
38 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set β `data` is
39 * never `undefined`.
40 *
41 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
42 * @template TError - The type of errors your `queryFn` may throw.
43 * @template TData - The type `data` ends up as after `select` runs β defaults to `InfiniteData<TQueryFnData>`,
44 * the shape of all fetched pages plus their page params.
45 * @template TQueryKey - The type of your `queryKey`.
46 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
47 */
48export type DefinedInitialDataInfiniteOptions<
49 TQueryFnData,
50 TError = DefaultError,
51 TData = InfiniteData<TQueryFnData>,
52 TQueryKey extends QueryKey = QueryKey,
53 TPageParam = unknown,
54> = UseInfiniteQueryOptions<
55 TQueryFnData,
56 TError,
57 TData,
58 TQueryKey,
59 TPageParam
60> & {
61 /**
62 * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
63 * created or cached yet). If set to a function, the function will be called **once** during the shared/root
64 * query initialization, and be expected to synchronously return the initial data. Initial data is
65 * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
66 * cache.
67 */
68 initialData:
69 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
70 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
71}
72β
73/**
74 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
75 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
76 * `options.queryKey` is required and is the query key to generate options for.
77 *
78 * @see {@link useInfiniteQuery} to run an infinite query with these options.
79 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β everything you can pass to
80 * `useInfiniteQuery`.
81 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
82 *
83 * @example
84 * ```vue
85 * <script setup lang="ts">
86 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/vue-query'
87 *
88 * const projectsOptions = infiniteQueryOptions({
89 * queryKey: ['projects'],
90 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
91 * initialPageParam: 0,
92 * getNextPageParam: (lastPage) => lastPage.nextId,
93 * })
94 *
95 * const { data, isError, error, fetchNextPage } = useInfiniteQuery(projectsOptions)
96 * </script>
97 * ```
98 */
99export function infiniteQueryOptions<
100 TQueryFnData,
101 TError = DefaultError,
102 TData = InfiniteData<TQueryFnData>,
103 TQueryKey extends QueryKey = QueryKey,
104 TPageParam = unknown,
105>(
106 options: UndefinedInitialDataInfiniteOptions<
107 TQueryFnData,
108 TError,
109 TData,
110 TQueryKey,
111 TPageParam
112 >,
113): UndefinedInitialDataInfiniteOptions<
114 TQueryFnData,
115 TError,
116 TData,
117 TQueryKey,
118 TPageParam
119> &
120 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
121β
122/**
123 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
124 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
125 * `options.queryKey` is required and is the query key to generate options for.
126 *
127 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
128 *
129 * @see {@link useInfiniteQuery} to run an infinite query with these options.
130 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
131 * `useInfiniteQuery`, with `initialData` set.
132 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
133 *
134 * @example
135 * ```vue
136 * <script setup lang="ts">
137 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/vue-query'
138 *
139 * const projectsOptions = infiniteQueryOptions({
140 * queryKey: ['projects'],
141 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
142 * initialPageParam: 0,
143 * getNextPageParam: (lastPage) => lastPage.nextId,
144 * initialData: { pages: [], pageParams: [] },
145 * })
146 *
147 * // `data` is never `undefined`, thanks to `initialData` β even if a refetch fails, so the
148 * // list stays visible alongside the error.
149 * const { data, isError, error } = useInfiniteQuery(projectsOptions)
150 * </script>
151 * ```
152 */
153export function infiniteQueryOptions<
154 TQueryFnData,
155 TError = DefaultError,
156 TData = InfiniteData<TQueryFnData>,
157 TQueryKey extends QueryKey = QueryKey,
158 TPageParam = unknown,
159>(
160 options: DefinedInitialDataInfiniteOptions<
161 TQueryFnData,
162 TError,
163 TData,
164 TQueryKey,
165 TPageParam
166 >,
167): DefinedInitialDataInfiniteOptions<
168 TQueryFnData,
169 TError,
170 TData,
171 TQueryKey,
172 TPageParam
173> &
174 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
175β
176export function infiniteQueryOptions(options: unknown) {
177 return options
178}