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