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

infiniteQueryOptions.ts

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