Open History to see the last commit
1/* istanbul ignore file */
2β
3import type {
4 DefaultError,
5 DefinedInfiniteQueryObserverResult,
6 DefinedQueryObserverResult,
7 InfiniteQueryObserverResult,
8 MutateFunction,
9 MutationObserverOptions,
10 MutationObserverResult,
11 OmitKeyof,
12 Override,
13 QueryKey,
14 QueryObserverResult,
15} from '@tanstack/query-core'
16import type {
17 InfiniteQueryObserverOptions,
18 QueryObserverOptions,
19} from './QueryClient'
20import type { Accessor } from 'solid-js'
21β
22/**
23 * The options accepted by `useQuery`. Extends {@link QueryObserverOptions} from `@tanstack/query-core` with
24 * the `solid-query`-specific `deferStream` and `suspense` options.
25 *
26 * @template TQueryFnData - The type your `queryFn` resolves to.
27 * @template TError - The type of errors your `queryFn` may throw.
28 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
29 * `select` is used.
30 * @template TQueryData - The type of the data actually held in the query cache β the input to `select` and
31 * `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
32 * @template TQueryKey - The type of your `queryKey`.
33 */
34export interface UseBaseQueryOptions<
35 TQueryFnData = unknown,
36 TError = DefaultError,
37 TData = TQueryFnData,
38 TQueryData = TQueryFnData,
39 TQueryKey extends QueryKey = QueryKey,
40> extends OmitKeyof<
41 QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
42 'suspense'
43> {
44 /**
45 * Only applicable while rendering queries on the server with streaming.
46 * Set `deferStream` to `true` to wait for the query to resolve on the server before flushing the stream.
47 * This can be useful to avoid sending a loading state to the client before the query has resolved.
48 *
49 * @defaultValue false
50 */
51 deferStream?: boolean
52 /**
53 * @deprecated The `suspense` option has been deprecated in v5 and will be removed in the next major version.
54 * The `data` property on useQuery is a SolidJS resource and will automatically suspend when the data is loading.
55 * Setting `suspense` to `false` will be a no-op.
56 */
57 suspense?: boolean
58}
59β
60/**
61 * The options accepted by `useQuery` and `queryOptions`.
62 *
63 * @template TQueryFnData - The type your `queryFn` resolves to.
64 * @template TError - The type of errors your `queryFn` may throw.
65 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
66 * `select` is used.
67 * @template TQueryKey - The type of your `queryKey`.
68 */
69export interface QueryOptions<
70 TQueryFnData = unknown,
71 TError = DefaultError,
72 TData = TQueryFnData,
73 TQueryKey extends QueryKey = QueryKey,
74> extends UseBaseQueryOptions<
75 TQueryFnData,
76 TError,
77 TData,
78 TQueryFnData,
79 TQueryKey
80> {}
81β
82/**
83 * The accessor `useQuery` expects as its first argument β Solid re-evaluates it reactively, so `queryKey` and
84 * other options can depend on signals.
85 *
86 * @template TQueryFnData - The type your `queryFn` resolves to.
87 * @template TError - The type of errors your `queryFn` may throw.
88 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
89 * `select` is used.
90 * @template TQueryKey - The type of your `queryKey`.
91 */
92export type UseQueryOptions<
93 TQueryFnData = unknown,
94 TError = DefaultError,
95 TData = TQueryFnData,
96 TQueryKey extends QueryKey = QueryKey,
97> = Accessor<QueryOptions<TQueryFnData, TError, TData, TQueryKey>>
98β
99/* --- Create Query and Create Base Query Types --- */
100β
101/**
102 * The object `useQuery` returns when `initialData` isn't set β `data`/`error` may still be `undefined`/`null`
103 * while the query is `pending`. Re-exports {@link QueryObserverResult} from `@tanstack/query-core`.
104 * `useInfiniteQuery` returns {@link UseInfiniteQueryResult} instead.
105 *
106 * @template TData - The type `data` ends up as, after `select` runs (if set).
107 * @template TError - The type of errors this query may hold.
108 */
109export type UseBaseQueryResult<
110 TData = unknown,
111 TError = DefaultError,
112> = QueryObserverResult<TData, TError>
113β
114/**
115 * The object `useQuery` returns β `data`/`error` may still be `undefined`/`null` while the query is
116 * `pending`.
117 *
118 * @template TData - The type `data` ends up as, after `select` runs (if set).
119 * @template TError - The type of errors this query may hold.
120 */
121export type UseQueryResult<
122 TData = unknown,
123 TError = DefaultError,
124> = UseBaseQueryResult<TData, TError>
125β
126/**
127 * The object `useQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
128 * `select` changes `TData` to include `undefined`).
129 *
130 * @template TData - The type `data` ends up as, after `select` runs (if set).
131 * @template TError - The type of errors this query may hold.
132 */
133export type DefinedUseBaseQueryResult<
134 TData = unknown,
135 TError = DefaultError,
136> = DefinedQueryObserverResult<TData, TError>
137β
138/**
139 * The object `useQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
140 * `select` changes `TData` to include `undefined`).
141 *
142 * @template TData - The type `data` ends up as, after `select` runs (if set).
143 * @template TError - The type of errors this query may hold.
144 */
145export type DefinedUseQueryResult<
146 TData = unknown,
147 TError = DefaultError,
148> = DefinedUseBaseQueryResult<TData, TError>
149β
150/* --- Create Infinite Queries Types --- */
151/**
152 * The options accepted by `useInfiniteQuery`.
153 *
154 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
155 * @template TError - The type of errors your `queryFn` may throw.
156 * @template TData - The type `data` ends up as after `select` runs.
157 * @template TQueryKey - The type of your `queryKey`.
158 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
159 */
160export interface InfiniteQueryOptions<
161 TQueryFnData = unknown,
162 TError = DefaultError,
163 TData = TQueryFnData,
164 TQueryKey extends QueryKey = QueryKey,
165 TPageParam = unknown,
166> extends OmitKeyof<
167 InfiniteQueryObserverOptions<
168 TQueryFnData,
169 TError,
170 TData,
171 TQueryKey,
172 TPageParam
173 >,
174 'queryKey' | 'suspense'
175> {
176 /**
177 * The query key to use for this query. Required here, unlike on the options this type extends.
178 *
179 * The query key will be hashed into a stable hash. See [Query Keys](https://tanstack.com/query/latest/docs/framework/solid/guides/query-keys)
180 * for more information.
181 *
182 * The query will automatically update when this key changes (as long as `enabled` is not set to `false`).
183 */
184 queryKey: TQueryKey
185 /**
186 * Only applicable while rendering queries on the server with streaming.
187 * Set `deferStream` to `true` to wait for the query to resolve on the server before flushing the stream.
188 * This can be useful to avoid sending a loading state to the client before the query has resolved.
189 *
190 * @defaultValue false
191 */
192 deferStream?: boolean
193 /**
194 * @deprecated The `suspense` option has been deprecated in v5 and will be removed in the next major version.
195 * The `data` property on useInfiniteQuery is a SolidJS resource and will automatically suspend when the data is loading.
196 * Setting `suspense` to `false` will be a no-op.
197 */
198 suspense?: boolean
199}
200β
201/**
202 * The accessor `useInfiniteQuery` expects as its first argument β Solid re-evaluates it reactively, so
203 * `queryKey` and other options can depend on signals.
204 *
205 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
206 * @template TError - The type of errors your `queryFn` may throw.
207 * @template TData - The type `data` ends up as after `select` runs.
208 * @template TQueryKey - The type of your `queryKey`.
209 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
210 */
211export type UseInfiniteQueryOptions<
212 TQueryFnData = unknown,
213 TError = DefaultError,
214 TData = TQueryFnData,
215 TQueryKey extends QueryKey = QueryKey,
216 TPageParam = unknown,
217> = Accessor<
218 InfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>
219>
220β
221/**
222 * The object `useInfiniteQuery` returns β `data`/`error` may still be `undefined`/`null` while the query is
223 * `pending`.
224 *
225 * @template TData - The type `data` ends up as, after `select` runs (if set).
226 * @template TError - The type of errors this query may hold.
227 */
228export type UseInfiniteQueryResult<
229 TData = unknown,
230 TError = DefaultError,
231> = InfiniteQueryObserverResult<TData, TError>
232β
233/**
234 * The object `useInfiniteQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
235 * `select` changes `TData` to include `undefined`).
236 *
237 * @template TData - The type `data` ends up as, after `select` runs (if set).
238 * @template TError - The type of errors this query may hold.
239 */
240export type DefinedUseInfiniteQueryResult<
241 TData = unknown,
242 TError = DefaultError,
243> = DefinedInfiniteQueryObserverResult<TData, TError>
244β
245/* --- Create Mutation Types --- */
246/**
247 * The options accepted by `useMutation` and `mutationOptions`.
248 *
249 * @template TData - The type your `mutationFn` resolves to.
250 * @template TError - The type of errors your `mutationFn` may throw.
251 * @template TVariables - The type of the variables your `mutationFn` accepts.
252 * @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
253 */
254export interface MutationOptions<
255 TData = unknown,
256 TError = DefaultError,
257 TVariables = void,
258 TOnMutateResult = unknown,
259> extends OmitKeyof<
260 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
261 '_defaulted'
262> {}
263β
264/**
265 * The accessor `useMutation` expects as its first argument β Solid re-evaluates it reactively, so callbacks
266 * and other options can depend on signals.
267 *
268 * @template TData - The type your `mutationFn` resolves to.
269 * @template TError - The type of errors your `mutationFn` may throw.
270 * @template TVariables - The type of the variables your `mutationFn` accepts.
271 * @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
272 */
273export type UseMutationOptions<
274 TData = unknown,
275 TError = DefaultError,
276 TVariables = void,
277 TOnMutateResult = unknown,
278> = Accessor<MutationOptions<TData, TError, TVariables, TOnMutateResult>>
279β
280export type UseMutateFunction<
281 TData = unknown,
282 TError = DefaultError,
283 TVariables = void,
284 TOnMutateResult = unknown,
285> = (
286 ...args: Parameters<
287 MutateFunction<TData, TError, TVariables, TOnMutateResult>
288 >
289) => void
290β
291/**
292 * The type of `mutateAsync`, as returned by `useMutation`. Similar to {@link UseMutateFunction}, but returns a
293 * promise which can be awaited.
294 *
295 * @template TData - The type your `mutationFn` resolves to.
296 * @template TError - The type of errors your `mutationFn` may throw.
297 * @template TVariables - The type of the variable passed to `mutateAsync`.
298 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
299 * their `onMutateResult` parameter β useful for optimistic-update rollback data.
300 */
301export type UseMutateAsyncFunction<
302 TData = unknown,
303 TError = DefaultError,
304 TVariables = void,
305 TOnMutateResult = unknown,
306> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
307β
308/**
309 * The result of `useMutation`. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with
310 * `mutate` narrowed to the fire-and-forget {@link UseMutateFunction} signature, plus the added `mutateAsync`.
311 *
312 * @template TData - The type your `mutationFn` resolves to.
313 * @template TError - The type of errors your `mutationFn` may throw.
314 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
315 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
316 * their `onMutateResult` parameter β useful for optimistic-update rollback data.
317 */
318export type UseBaseMutationResult<
319 TData = unknown,
320 TError = DefaultError,
321 TVariables = unknown,
322 TOnMutateResult = unknown,
323> = Override<
324 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
325 { mutate: UseMutateFunction<TData, TError, TVariables, TOnMutateResult> }
326> & {
327 /**
328 * Similar to `mutate`, but returns a promise which can be awaited.
329 */
330 mutateAsync: UseMutateAsyncFunction<
331 TData,
332 TError,
333 TVariables,
334 TOnMutateResult
335 >
336}
337β
338/**
339 * The result of `useMutation`. Same as {@link UseBaseMutationResult}.
340 *
341 * @template TData - The type your `mutationFn` resolves to.
342 * @template TError - The type of errors your `mutationFn` may throw.
343 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
344 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
345 * their `onMutateResult` parameter β useful for optimistic-update rollback data.
346 */
347export type UseMutationResult<
348 TData = unknown,
349 TError = DefaultError,
350 TVariables = unknown,
351 TOnMutateResult = unknown,
352> = UseBaseMutationResult<TData, TError, TVariables, TOnMutateResult>