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

infiniteQueryOptions.ts

184 lines5.3 KB

1import type {
2 DefaultError,
3 InfiniteData,
4 InitialDataFunction,
5 NonUndefinedGuard,
6 QueryKey,
7 QueryKeyWithDataTag,
8} from '@tanstack/query-core'
9import type { CreateInfiniteQueryOptions } from './types.js'
10​
11export type UndefinedInitialDataInfiniteOptions<
12 TQueryFnData = unknown,
13 TError = DefaultError,
14 TData = InfiniteData<TQueryFnData>,
15 TQueryKey extends QueryKey = QueryKey,
16 TPageParam = unknown,
17> = CreateInfiniteQueryOptions<
18 TQueryFnData,
19 TError,
20 TData,
21 TQueryKey,
22 TPageParam
23> & {
24 initialData?:
25 | undefined
26 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
27 | InitialDataFunction<
28 NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
29 >
31​
32export type DefinedInitialDataInfiniteOptions<
33 TQueryFnData = unknown,
34 TError = DefaultError,
35 TData = InfiniteData<TQueryFnData>,
36 TQueryKey extends QueryKey = QueryKey,
37 TPageParam = unknown,
38> = CreateInfiniteQueryOptions<
39 TQueryFnData,
40 TError,
41 TData,
42 TQueryKey,
43 TPageParam
44> & {
45 initialData:
46 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
47 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
49​
50/**
51 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `createInfiniteQuery`.
52 * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
53 * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
54 *
55 * This overload is selected when `initialData` is set.
56 *
57 * @see {@link createInfiniteQuery} to run an infinite query with these options.
58 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β€” everything you can pass to
59 * `createInfiniteQuery`, with `initialData` set.
60 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
61 *
62 * @example
63 * `initialData` skips the loading state on first render β€” even if a refetch fails, the list stays
64 * visible alongside the error:
65 * ```svelte
66 * <script lang="ts">
67 * import { infiniteQueryOptions, createInfiniteQuery } from '@tanstack/svelte-query'
68 *
69 * const projectsOptions = infiniteQueryOptions({
70 * queryKey: ['projects'],
71 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
72 * initialPageParam: 0,
73 * getNextPageParam: (lastPage) => lastPage.nextId,
74 * initialData: { pages: [], pageParams: [] },
75 * })
76 *
77 * const query = createInfiniteQuery(() => projectsOptions)
78 * </script>
79 *
80 * {#if query.isError}
81 * <span>Error: {query.error.message}</span>
82 * {/if}
83 * <ul>
84 * {#each query.data.pages as page}
85 * {#each page.projects as project (project.id)}
86 * <li>{project.name}</li>
87 * {/each}
88 * {/each}
89 * </ul>
90 * ```
91 */
92export function infiniteQueryOptions<
93 TQueryFnData,
94 TError = DefaultError,
95 TData = InfiniteData<TQueryFnData>,
96 TQueryKey extends QueryKey = QueryKey,
97 TPageParam = unknown,
98>(
99 options: DefinedInitialDataInfiniteOptions<
100 TQueryFnData,
101 TError,
102 TData,
103 TQueryKey,
104 TPageParam
105 >,
106): DefinedInitialDataInfiniteOptions<
107 TQueryFnData,
108 TError,
109 TData,
110 TQueryKey,
111 TPageParam
112> &
113 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
114​
115/**
116 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `createInfiniteQuery`.
117 * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
118 * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
119 *
120 * @see {@link createInfiniteQuery} to run an infinite query with these options.
121 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to
122 * `createInfiniteQuery`.
123 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
124 *
125 * @example
126 * A parameterized factory, so the same options object can be reused per `postId`:
127 * ```svelte
128 * <script lang="ts">
129 * import { infiniteQueryOptions, createInfiniteQuery } from '@tanstack/svelte-query'
130 *
131 * let { postId }: { postId: string } = $props()
132 *
133 * const commentsOptions = (postId: string) =>
134 * infiniteQueryOptions({
135 * queryKey: ['post', postId, 'comments'],
136 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
137 * initialPageParam: 0,
138 * getNextPageParam: (lastPage) => lastPage.nextId,
139 * })
140 *
141 * const query = createInfiniteQuery(() => commentsOptions(postId))
142 * </script>
143 *
144 * {#if query.isPending}
145 * Loading...
146 * {:else if query.isError}
147 * <span>Error: {query.error.message}</span>
148 * {:else}
149 * <ul>
150 * {#each query.data.pages as page}
151 * {#each page.comments as comment (comment.id)}
152 * <li>{comment.text}</li>
153 * {/each}
154 * {/each}
155 * </ul>
156 * {/if}
157 * ```
158 */
159export function infiniteQueryOptions<
160 TQueryFnData,
161 TError = DefaultError,
162 TData = InfiniteData<TQueryFnData>,
163 TQueryKey extends QueryKey = QueryKey,
164 TPageParam = unknown,
165>(
166 options: UndefinedInitialDataInfiniteOptions<
167 TQueryFnData,
168 TError,
169 TData,
170 TQueryKey,
171 TPageParam
172 >,
173): UndefinedInitialDataInfiniteOptions<
174 TQueryFnData,
175 TError,
176 TData,
177 TQueryKey,
178 TPageParam
179> &
180 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
181​
182export function infiniteQueryOptions(options: unknown) {
183 return options