Open History to see the last commit
1import { InfiniteQueryObserver } from '@tanstack/query-core'
2import { createBaseQuery } from './createBaseQuery.svelte.js'
3import type {
4 DefaultError,
5 InfiniteData,
6 InfiniteQueryMode,
7 QueryClient,
8 QueryKey,
9 QueryObserver,
10} from '@tanstack/query-core'
11import type {
12 Accessor,
13 CreateBaseQueryOptions,
14 CreateInfiniteQueryOptions,
15 CreateInfiniteQueryResult,
16 DefinedCreateInfiniteQueryResult,
17} from './types.js'
18import type {
19 DefinedInitialDataInfiniteOptions,
20 UndefinedInitialDataInfiniteOptions,
21} from './infiniteQueryOptions.js'
22β
23/**
24 * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
25 * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
26 *
27 * This overload is selected when `initialData` is known to be defined.
28 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β everything you can pass to
29 * `createInfiniteQuery`, with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
30 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
31 * be used.
32 * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
33 * to page through the query.
34 * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
35 * like `queryClient.infiniteQuery`.
36 */
37export function createInfiniteQuery<
38 TQueryFnData = unknown,
39 TError = DefaultError,
40 TData = InfiniteData<TQueryFnData>,
41 TQueryKey extends QueryKey = QueryKey,
42 TPageParam = unknown,
43>(
44 options: Accessor<
45 DefinedInitialDataInfiniteOptions<
46 TQueryFnData,
47 TError,
48 TData,
49 TQueryKey,
50 TPageParam,
51 undefined
52 >
53 >,
54 queryClient?: Accessor<QueryClient>,
55): DefinedCreateInfiniteQueryResult<TData, TError, TPageParam, undefined>
56β
57export function createInfiniteQuery<
58 TQueryFnData = unknown,
59 TError = DefaultError,
60 TData = InfiniteData<TQueryFnData>,
61 TQueryKey extends QueryKey = QueryKey,
62 TPageParam = unknown,
63>(
64 options: Accessor<
65 DefinedInitialDataInfiniteOptions<
66 TQueryFnData,
67 TError,
68 TData,
69 TQueryKey,
70 TPageParam,
71 InfiniteQueryMode
72 >
73 >,
74 queryClient?: Accessor<QueryClient>,
75): DefinedCreateInfiniteQueryResult<
76 TData,
77 TError,
78 TPageParam,
79 InfiniteQueryMode
80>
81β
82/**
83 * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
84 * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
85 *
86 * This overload is selected when `initialData` is omitted or may be `undefined`.
87 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β everything you can pass to
88 * `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
89 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
90 * be used.
91 * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
92 * to page through the query.
93 * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
94 * like `queryClient.infiniteQuery`.
95 */
96export function createInfiniteQuery<
97 TQueryFnData = unknown,
98 TError = DefaultError,
99 TData = InfiniteData<TQueryFnData>,
100 TQueryKey extends QueryKey = QueryKey,
101 TPageParam = unknown,
102>(
103 options: Accessor<
104 UndefinedInitialDataInfiniteOptions<
105 TQueryFnData,
106 TError,
107 TData,
108 TQueryKey,
109 TPageParam,
110 undefined
111 >
112 >,
113 queryClient?: Accessor<QueryClient>,
114): CreateInfiniteQueryResult<TData, TError, TPageParam, undefined>
115β
116export function createInfiniteQuery<
117 TQueryFnData = unknown,
118 TError = DefaultError,
119 TData = InfiniteData<TQueryFnData>,
120 TQueryKey extends QueryKey = QueryKey,
121 TPageParam = unknown,
122>(
123 options: Accessor<
124 UndefinedInitialDataInfiniteOptions<
125 TQueryFnData,
126 TError,
127 TData,
128 TQueryKey,
129 TPageParam,
130 InfiniteQueryMode
131 >
132 >,
133 queryClient?: Accessor<QueryClient>,
134): CreateInfiniteQueryResult<TData, TError, TPageParam, InfiniteQueryMode>
135β
136/**
137 * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
138 * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
139 * @param options - The {@link CreateInfiniteQueryOptions} to use β everything you can pass to
140 * `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
141 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
142 * be used.
143 * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
144 * to page through the query.
145 * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
146 * like `queryClient.infiniteQuery`.
147 * @example
148 * Fetching the next page from a "Load More" button click:
149 * ```svelte
150 * <script lang="ts">
151 * import { createInfiniteQuery } from '@tanstack/svelte-query'
152 *
153 * const query = createInfiniteQuery(() => ({
154 * queryKey: ['projects'],
155 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
156 * initialPageParam: 0,
157 * getNextPageParam: (lastPage) => lastPage.nextId,
158 * }))
159 * </script>
160 *
161 * {#if query.isPending}
162 * Loading...
163 * {:else if query.isError}
164 * <span>Error: {query.error.message}</span>
165 * {:else}
166 * <ul>
167 * {#each query.data.pages as page}
168 * {#each page.projects as project (project.id)}
169 * <li>{project.name}</li>
170 * {/each}
171 * {/each}
172 * </ul>
173 * <button
174 * onclick={() => query.fetchNextPage()}
175 * disabled={!query.hasNextPage || query.isFetching}
176 * >
177 * {query.isFetchingNextPage
178 * ? 'Loading more...'
179 * : query.hasNextPage
180 * ? 'Load More'
181 * : 'Nothing more to load'}
182 * </button>
183 * {/if}
184 * ```
185 * @example
186 * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
187 * sentinel element after the list:
188 * ```svelte
189 * <script lang="ts">
190 * import { createInfiniteQuery } from '@tanstack/svelte-query'
191 *
192 * const query = createInfiniteQuery(() => ({
193 * queryKey: ['projects'],
194 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
195 * initialPageParam: 0,
196 * getNextPageParam: (lastPage) => lastPage.nextId,
197 * }))
198 *
199 * let sentinel: HTMLDivElement | undefined = $state()
200 *
201 * $effect(() => {
202 * if (sentinel == null || !query.hasNextPage || query.isFetching) return
203 *
204 * const observer = new IntersectionObserver(([entry]) => {
205 * if (entry?.isIntersecting) query.fetchNextPage()
206 * })
207 * observer.observe(sentinel)
208 *
209 * return () => observer.disconnect()
210 * })
211 * </script>
212 *
213 * {#if query.isPending}
214 * Loading...
215 * {:else if query.isError}
216 * <span>Error: {query.error.message}</span>
217 * {:else}
218 * <ul>
219 * {#each query.data.pages as page}
220 * {#each page.projects as project (project.id)}
221 * <li>{project.name}</li>
222 * {/each}
223 * {/each}
224 * </ul>
225 * <div bind:this={sentinel}></div>
226 * {/if}
227 * ```
228 */
229export function createInfiniteQuery<
230 TQueryFnData,
231 TError = DefaultError,
232 TData = InfiniteData<TQueryFnData>,
233 TQueryKey extends QueryKey = QueryKey,
234 TPageParam = unknown,
235>(
236 options: Accessor<
237 CreateInfiniteQueryOptions<
238 TQueryFnData,
239 TError,
240 TData,
241 TQueryKey,
242 TPageParam,
243 undefined
244 >
245 >,
246 queryClient?: Accessor<QueryClient>,
247): CreateInfiniteQueryResult<TData, TError, TPageParam, undefined>
248export function createInfiniteQuery<
249 TQueryFnData,
250 TError = DefaultError,
251 TData = InfiniteData<TQueryFnData>,
252 TQueryKey extends QueryKey = QueryKey,
253 TPageParam = unknown,
254>(
255 options: Accessor<
256 CreateInfiniteQueryOptions<
257 TQueryFnData,
258 TError,
259 TData,
260 TQueryKey,
261 TPageParam,
262 InfiniteQueryMode
263 >
264 >,
265 queryClient?: Accessor<QueryClient>,
266): CreateInfiniteQueryResult<TData, TError, TPageParam, InfiniteQueryMode>
267export function createInfiniteQuery(
268 options: any,
269 queryClient?: Accessor<QueryClient>,
270): any {
271 return createBaseQuery(
272 options as Accessor<
273 CreateBaseQueryOptions<any, any, any, InfiniteData<any, any>, any>
274 >,
275 InfiniteQueryObserver as typeof QueryObserver,
276 queryClient,
277 ) as any
278}