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

infiniteQueryOptions.ts

274 lines8.5 KB

1import type {
2 DefaultError,
3 InfiniteData,
4 InfiniteQueryMode,
5 NonUndefinedGuard,
6 QueryKey,
7 QueryKeyWithDataTag,
8} from '@tanstack/query-core'
9import type { InfiniteQueryOptions } from './types'
10import type { Accessor } from 'solid-js'
11​
12/**
13 * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set β€” `data`
14 * may be `undefined` while the query is `pending`. `infiniteQueryOptions` itself accepts and returns a plain
15 * object (its parameter type is `ReturnType<UndefinedInitialDataInfiniteOptions<...>>`, i.e. this `Accessor`
16 * called); Solid's reactivity applies where the result is consumed instead, e.g.
17 * `useInfiniteQuery(() => options)`.
18 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
19 * @template TError - The type of errors your `queryFn` may throw.
20 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
21 * the shape of all fetched pages plus their page params.
22 * @template TQueryKey - The type of your `queryKey`.
23 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
24 */
25export type UndefinedInitialDataInfiniteOptions<
26 TQueryFnData,
27 TError = DefaultError,
28 TData = InfiniteData<TQueryFnData>,
29 TQueryKey extends QueryKey = QueryKey,
30 TPageParam = unknown,
31 TMode extends InfiniteQueryMode | undefined = undefined,
32> = Accessor<
33 InfiniteQueryOptions<
34 TQueryFnData,
35 TError,
36 TData,
37 TQueryKey,
38 TPageParam,
39 TMode
40 > & {
41 initialData?: undefined
42 }
44​
45/**
46 * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set β€” `data` is
47 * never `undefined` (unless a `select` changes `TData` to include `undefined`).
48 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
49 * @template TError - The type of errors your `queryFn` may throw.
50 * @template TData - The type `data` ends up as after `select` runs β€” defaults to `InfiniteData<TQueryFnData>`,
51 * the shape of all fetched pages plus their page params.
52 * @template TQueryKey - The type of your `queryKey`.
53 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
54 */
55export type DefinedInitialDataInfiniteOptions<
56 TQueryFnData,
57 TError = DefaultError,
58 // should we handle page param correctly
59 TData = InfiniteData<TQueryFnData>,
60 TQueryKey extends QueryKey = QueryKey,
61 TPageParam = unknown,
62 TMode extends InfiniteQueryMode | undefined = undefined,
63> = Accessor<
64 InfiniteQueryOptions<
65 TQueryFnData,
66 TError,
67 TData,
68 TQueryKey,
69 TPageParam,
70 TMode
71 > & {
72 initialData:
73 | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
74 | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
75 }
77​
78/**
79 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
80 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
81 * `options.queryKey` is required and is the query key to generate options for.
82 *
83 * This overload is selected when `initialData` is set.
84 * @param options - The {@link DefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`, with `initialData` set.
85 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
86 * @see {@link useInfiniteQuery} to run an infinite query with these options.
87 * @example
88 * ```tsx
89 * import { For } from 'solid-js'
90 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/solid-query'
91 *
92 * const projectsOptions = infiniteQueryOptions({
93 * queryKey: ['projects'],
94 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
95 * initialPageParam: 0,
96 * getNextPageParam: (lastPage) => lastPage.nextId,
97 * initialData: { pages: [], pageParams: [] },
98 * })
99 *
100 * function Projects() {
101 * // `projectsQuery.data` is never `undefined`, thanks to `initialData` β€” even if a refetch fails, so the
102 * // list stays visible alongside the error.
103 * const projectsQuery = useInfiniteQuery(() => projectsOptions)
104 *
105 * return (
106 * <div>
107 * {projectsQuery.isError ? <span>Error: {projectsQuery.error.message}</span> : null}
108 * <ul>
109 * <For each={projectsQuery.data.pages}>
110 * {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
111 * </For>
112 * </ul>
113 * </div>
114 * )
115 * }
116 * ```
117 */
118export function infiniteQueryOptions<
119 TQueryFnData,
120 TError = DefaultError,
121 TData = InfiniteData<TQueryFnData>,
122 TQueryKey extends QueryKey = QueryKey,
123 TPageParam = unknown,
124>(
125 options: ReturnType<
126 DefinedInitialDataInfiniteOptions<
127 TQueryFnData,
128 TError,
129 TData,
130 TQueryKey,
131 TPageParam,
132 undefined
133 >
134 >,
135): ReturnType<
136 DefinedInitialDataInfiniteOptions<
137 TQueryFnData,
138 TError,
139 TData,
140 TQueryKey,
141 TPageParam,
142 undefined
143 >
144> &
145 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
146export function infiniteQueryOptions<
147 TQueryFnData,
148 TError = DefaultError,
149 TData = InfiniteData<TQueryFnData>,
150 TQueryKey extends QueryKey = QueryKey,
151 TPageParam = unknown,
152>(
153 options: ReturnType<
154 DefinedInitialDataInfiniteOptions<
155 TQueryFnData,
156 TError,
157 TData,
158 TQueryKey,
159 TPageParam,
160 InfiniteQueryMode
161 >
162 >,
163): ReturnType<
164 DefinedInitialDataInfiniteOptions<
165 TQueryFnData,
166 TError,
167 TData,
168 TQueryKey,
169 TPageParam,
170 InfiniteQueryMode
171 >
172> &
173 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
174​
175/**
176 * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
177 * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
178 * `options.queryKey` is required and is the query key to generate options for.
179 * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use β€” everything you can pass to `useInfiniteQuery`.
180 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
181 * @see {@link useInfiniteQuery} to run an infinite query with these options.
182 * @example
183 * A parameterized factory, so the same options object can be reused per `postId`:
184 * ```tsx
185 * import { For, Match, Switch } from 'solid-js'
186 * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/solid-query'
187 *
188 * const commentsOptions = (postId: string) =>
189 * infiniteQueryOptions({
190 * queryKey: ['post', postId, 'comments'],
191 * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
192 * initialPageParam: 0,
193 * getNextPageParam: (lastPage) => lastPage.nextId,
194 * })
195 *
196 * function Comments(props: { postId: string }) {
197 * const commentsQuery = useInfiniteQuery(() => commentsOptions(props.postId))
198 *
199 * return (
200 * <Switch>
201 * <Match when={commentsQuery.isPending}>Loading...</Match>
202 * <Match when={commentsQuery.isError}>Error: {commentsQuery.error.message}</Match>
203 * <Match when={commentsQuery.isSuccess}>
204 * <ul>
205 * <For each={commentsQuery.data.pages}>
206 * {(page) => <For each={page.comments}>{(c) => <li>{c.text}</li>}</For>}
207 * </For>
208 * </ul>
209 * </Match>
210 * </Switch>
211 * )
212 * }
213 * ```
214 */
215export function infiniteQueryOptions<
216 TQueryFnData,
217 TError = DefaultError,
218 TData = InfiniteData<TQueryFnData>,
219 TQueryKey extends QueryKey = QueryKey,
220 TPageParam = unknown,
221>(
222 options: ReturnType<
223 UndefinedInitialDataInfiniteOptions<
224 TQueryFnData,
225 TError,
226 TData,
227 TQueryKey,
228 TPageParam,
229 undefined
230 >
231 >,
232): ReturnType<
233 UndefinedInitialDataInfiniteOptions<
234 TQueryFnData,
235 TError,
236 TData,
237 TQueryKey,
238 TPageParam,
239 undefined
240 >
241> &
242 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
243export function infiniteQueryOptions<
244 TQueryFnData,
245 TError = DefaultError,
246 TData = InfiniteData<TQueryFnData>,
247 TQueryKey extends QueryKey = QueryKey,
248 TPageParam = unknown,
249>(
250 options: ReturnType<
251 UndefinedInitialDataInfiniteOptions<
252 TQueryFnData,
253 TError,
254 TData,
255 TQueryKey,
256 TPageParam,
257 InfiniteQueryMode
258 >
259 >,
260): ReturnType<
261 UndefinedInitialDataInfiniteOptions<
262 TQueryFnData,
263 TError,
264 TData,
265 TQueryKey,
266 TPageParam,
267 InfiniteQueryMode
268 >
269> &
270 QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
271​
272export function infiniteQueryOptions(options: unknown) {
273 return options