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

infiniteQueryOptions.ts

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