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