Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createInfiniteQuery.ts

210 lines6.8 KB

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 DefinedInitialDataInfiniteOptions,
12 UndefinedInitialDataInfiniteOptions,
13} from './infiniteQueryOptions.js'
14import type {
15 Accessor,
16 CreateInfiniteQueryOptions,
17 CreateInfiniteQueryResult,
18 DefinedCreateInfiniteQueryResult,
19} from './types.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` known to be defined, wrapped in an {@link Accessor} so options
28 * can be reactive.
29 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
30 * be used.
31 * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
32 * to page through the query.
33 * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
34 * like `queryClient.infiniteQuery`.
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 omitted, may be `undefined`, or is a function that may
60 * return `undefined`.
61 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to
62 * `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
63 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
64 * be used.
65 * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
66 * to page through the query.
67 * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
68 * like `queryClient.infiniteQuery`.
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 * @param options - The {@link CreateInfiniteQueryOptions} to use β€” everything you can pass to
93 * `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
94 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
95 * be used.
96 * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
97 * to page through the query.
98 * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
99 * like `queryClient.infiniteQuery`.
100 * @example
101 * Fetching the next page from a "Load More" button click:
102 * ```svelte
103 * <script lang="ts">
104 * import { createInfiniteQuery } from '@tanstack/svelte-query'
105 *
106 * const query = createInfiniteQuery(() => ({
107 * queryKey: ['projects'],
108 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
109 * initialPageParam: 0,
110 * getNextPageParam: (lastPage) => lastPage.nextId,
111 * }))
112 * </script>
113 *
114 * {#if query.isPending}
115 * Loading...
116 * {:else if query.isError}
117 * <span>Error: {query.error.message}</span>
118 * {:else}
119 * <ul>
120 * {#each query.data.pages as page}
121 * {#each page.projects as project (project.id)}
122 * <li>{project.name}</li>
123 * {/each}
124 * {/each}
125 * </ul>
126 * <button
127 * onclick={() => query.fetchNextPage()}
128 * disabled={!query.hasNextPage || query.isFetching}
129 * >
130 * {query.isFetchingNextPage
131 * ? 'Loading more...'
132 * : query.hasNextPage
133 * ? 'Load More'
134 * : 'Nothing more to load'}
135 * </button>
136 * {/if}
137 * ```
138 * @example
139 * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
140 * sentinel element after the list:
141 * ```svelte
142 * <script lang="ts">
143 * import { createInfiniteQuery } from '@tanstack/svelte-query'
144 *
145 * const query = createInfiniteQuery(() => ({
146 * queryKey: ['projects'],
147 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
148 * initialPageParam: 0,
149 * getNextPageParam: (lastPage) => lastPage.nextId,
150 * }))
151 *
152 * let sentinel: HTMLDivElement | undefined = $state()
153 *
154 * $effect(() => {
155 * if (sentinel == null || !query.hasNextPage || query.isFetching) return
156 *
157 * const observer = new IntersectionObserver(([entry]) => {
158 * if (entry?.isIntersecting) query.fetchNextPage()
159 * })
160 * observer.observe(sentinel)
161 *
162 * return () => observer.disconnect()
163 * })
164 * </script>
165 *
166 * {#if query.isPending}
167 * Loading...
168 * {:else if query.isError}
169 * <span>Error: {query.error.message}</span>
170 * {:else}
171 * <ul>
172 * {#each query.data.pages as page}
173 * {#each page.projects as project (project.id)}
174 * <li>{project.name}</li>
175 * {/each}
176 * {/each}
177 * </ul>
178 * <div bind:this={sentinel}></div>
179 * {/if}
180 * ```
181 */
182export function createInfiniteQuery<
183 TQueryFnData,
184 TError = DefaultError,
185 TData = InfiniteData<TQueryFnData>,
186 TQueryKey extends QueryKey = QueryKey,
187 TPageParam = unknown,
188>(
189 options: Accessor<
190 CreateInfiniteQueryOptions<
191 TQueryFnData,
192 TError,
193 TData,
194 TQueryKey,
195 TPageParam
196 >
197 >,
198 queryClient?: Accessor<QueryClient>,
199): CreateInfiniteQueryResult<TData, TError>
200​
201export function createInfiniteQuery(
202 options: Accessor<CreateInfiniteQueryOptions>,
203 queryClient?: Accessor<QueryClient>,
204) {
205 return createBaseQuery(
206 options,
207 InfiniteQueryObserver as typeof QueryObserver,
208 queryClient,
209 )