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

infiniteQueryOptions.ts

225 lines7.1 KB

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