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