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 { CreateInfiniteQueryOptions } from './types.js'
10β
11/**
12 * The options accepted by the `infiniteQueryOptions` overload selected when
13 * `initialData` is omitted or may be `undefined` β `data` may be `undefined`
14 * while the query is `pending`.
15 */
16export type UndefinedInitialDataInfiniteOptions<
17 TQueryFnData = unknown,
18 TError = DefaultError,
19 TData = InfiniteData<TQueryFnData>,
20 TQueryKey extends QueryKey = QueryKey,
21 TPageParam = unknown,
22> = CreateInfiniteQueryOptions<
23 TQueryFnData,
24 TError,
25 TData,
26 TQueryKey,
27 TPageParam
28> & {
29 initialData?:
30 | undefined
31 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
32 | InitialDataFunction<
33 NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
34 >
35}
36β
37/**
38 * The options accepted by the `infiniteQueryOptions` overload selected when
39 * `initialData` is known to be defined β `data` is never `undefined` (unless a
40 * `select` changes `TData` to include `undefined`).
41 */
42export type DefinedInitialDataInfiniteOptions<
43 TQueryFnData = unknown,
44 TError = DefaultError,
45 TData = InfiniteData<TQueryFnData>,
46 TQueryKey extends QueryKey = QueryKey,
47 TPageParam = unknown,
48> = CreateInfiniteQueryOptions<
49 TQueryFnData,
50 TError,
51 TData,
52 TQueryKey,
53 TPageParam
54> & {
55 initialData:
56 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
57 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
58}
59β
60/**
61 * You can generally pass everything to `infiniteQueryOptions` that you can also
62 * pass to `createInfiniteQuery`. These options can be shared across
63 * `createInfiniteQuery` calls and imperative APIs such as
64 * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query
65 * key to generate options for.
66 *
67 * This overload is selected when `initialData` is known to be defined.
68 *
69 * @example
70 * `initialData` skips the loading state on first render β even if a refetch fails, the list stays
71 * visible alongside the error:
72 * ```svelte
73 * <script lang="ts">
74 * import { infiniteQueryOptions, createInfiniteQuery } from '@tanstack/svelte-query'
75 *
76 * const projectsOptions = infiniteQueryOptions({
77 * queryKey: ['projects'],
78 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
79 * initialPageParam: 0,
80 * getNextPageParam: (lastPage) => lastPage.nextId,
81 * initialData: { pages: [], pageParams: [] },
82 * })
83 *
84 * const query = createInfiniteQuery(() => projectsOptions)
85 * </script>
86 *
87 * {#if query.isError}
88 * <span>Error: {query.error.message}</span>
89 * {/if}
90 * <ul>
91 * {#each query.data.pages as page}
92 * {#each page.projects as project (project.id)}
93 * <li>{project.name}</li>
94 * {/each}
95 * {/each}
96 * </ul>
97 * ```
98 *
99 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β
100 * everything you can pass to `createInfiniteQuery`, with `initialData` set.
101 * @returns The same options object, typed so that `queryKey` carries the
102 * inferred data type.
103 * @see {@link createInfiniteQuery} to run an infinite query with these options.
104 */
105export function infiniteQueryOptions<
106 TQueryFnData,
107 TError = DefaultError,
108 TData = InfiniteData<TQueryFnData>,
109 TQueryKey extends QueryKey = QueryKey,
110 TPageParam = unknown,
111>(
112 options: DefinedInitialDataInfiniteOptions<
113 TQueryFnData,
114 TError,
115 TData,
116 TQueryKey,
117 TPageParam
118 >,
119): DefinedInitialDataInfiniteOptions<
120 TQueryFnData,
121 TError,
122 TData,
123 TQueryKey,
124 TPageParam
125> &
126 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
127β
128/**
129 * You can generally pass everything to `infiniteQueryOptions` that you can also
130 * pass to `createInfiniteQuery`. These options can be shared across
131 * `createInfiniteQuery` calls and imperative APIs such as
132 * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query
133 * key to generate options for.
134 *
135 * @example
136 * A parameterized factory, so the same options object can be reused per `postId`:
137 * ```svelte
138 * <script lang="ts">
139 * import { infiniteQueryOptions, createInfiniteQuery } from '@tanstack/svelte-query'
140 *
141 * let { postId }: { postId: string } = $props()
142 *
143 * const commentsOptions = (postId: string) =>
144 * infiniteQueryOptions({
145 * queryKey: ['post', postId, 'comments'],
146 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
147 * initialPageParam: 0,
148 * getNextPageParam: (lastPage) => lastPage.nextId,
149 * })
150 *
151 * const query = createInfiniteQuery(() => commentsOptions(postId))
152 * </script>
153 *
154 * {#if query.isPending}
155 * Loading...
156 * {:else if query.isError}
157 * <span>Error: {query.error.message}</span>
158 * {:else}
159 * <ul>
160 * {#each query.data.pages as page}
161 * {#each page.comments as comment (comment.id)}
162 * <li>{comment.text}</li>
163 * {/each}
164 * {/each}
165 * </ul>
166 * {/if}
167 * ```
168 *
169 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β
170 * everything you can pass to `createInfiniteQuery`.
171 * @returns The same options object, typed so that `queryKey` carries the
172 * inferred data type.
173 * @see {@link createInfiniteQuery} to run an infinite query with these options.
174 */
175export function infiniteQueryOptions<
176 TQueryFnData,
177 TError = DefaultError,
178 TData = InfiniteData<TQueryFnData>,
179 TQueryKey extends QueryKey = QueryKey,
180 TPageParam = unknown,
181>(
182 options: UndefinedInitialDataInfiniteOptions<
183 TQueryFnData,
184 TError,
185 TData,
186 TQueryKey,
187 TPageParam
188 >,
189): UndefinedInitialDataInfiniteOptions<
190 TQueryFnData,
191 TError,
192 TData,
193 TQueryKey,
194 TPageParam
195> &
196 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
197β
198export function infiniteQueryOptions(options: unknown) {
199 return options
200}