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