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

infiniteQueryOptions.ts

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