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