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