Open History to see the last commit
1/* istanbul ignore file */
2β
3import type {
4 DefaultError,
5 DefinedInfiniteQueryObserverResult,
6 DefinedQueryObserverResult,
7 InfiniteQueryObserverResult,
8 MutationObserverOptions,
9 MutationObserverResult,
10 OmitKeyof,
11 Override,
12 QueryKey,
13 QueryObserverResult,
14} from '@tanstack/query-core'
15import type {
16 InfiniteQueryObserverOptions,
17 QueryObserverOptions,
18} from './QueryClient'
19import type { Accessor } from 'solid-js'
20β
21/**
22 * The options accepted by `useQuery`. Extends {@link QueryObserverOptions} from `@tanstack/query-core` with
23 * the `solid-query`-specific `deferStream` and `reconcile` options.
24 * @template TQueryFnData - The type your `queryFn` resolves to.
25 * @template TError - The type of errors your `queryFn` may throw.
26 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
27 * `select` is used.
28 * @template TQueryData - The type of the data actually held in the query cache β the input to `select` and
29 * `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
30 * @template TQueryKey - The type of your `queryKey`.
31 */
32export interface UseBaseQueryOptions<
33 TQueryFnData = unknown,
34 TError = DefaultError,
35 TData = TQueryFnData,
36 TQueryData = TQueryFnData,
37 TQueryKey extends QueryKey = QueryKey,
38> extends OmitKeyof<
39 QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
40 'suspense'
41> {
42 /**
43 * Defer the SSR stream flush until this query resolves on the server,
44 * instead of letting the surrounding `<Loading>` boundary render its
45 * fallback into the HTML. Passed through to Solid's own per-computation
46 * `deferStream` option; server-only, ignored on the client.
47 */
48 deferStream?: boolean
49 /**
50 * Reconciliation key for the data store. `data` is served as a Solid
51 * store projection: every landing (refetch, invalidation, placeholder
52 * upgrade) reconciles into the existing proxy graph instead of replacing
53 * it, so deep reads are fine-grained and item identity survives across
54 * fetches. This sets the identity key for that reconciliation β a
55 * property name, a key-extraction function, or `null` to merge
56 * positionally. Defaults to `'id'`. Read once at creation.
57 */
58 reconcile?: string | ((item: NonNullable<any>) => any) | null
59}
60β
61/**
62 * The options accepted by `useQuery` and `queryOptions`.
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 * @template TQueryFnData - The type your `queryFn` resolves to.
86 * @template TError - The type of errors your `queryFn` may throw.
87 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
88 * `select` is used.
89 * @template TQueryKey - The type of your `queryKey`.
90 */
91export type UseQueryOptions<
92 TQueryFnData = unknown,
93 TError = DefaultError,
94 TData = TQueryFnData,
95 TQueryKey extends QueryKey = QueryKey,
96> = Accessor<QueryOptions<TQueryFnData, TError, TData, TQueryKey>>
97β
98/* --- Create Query and Create Base Query Types --- */
99β
100/**
101 * `data` is non-optional: it is a suspending async read. It never returns
102 * `undefined` β a read either suspends into the nearest `<Loading>`
103 * boundary (first fetch in flight, disabled, restoring), returns a value
104 * (committed, placeholder, initial), or throws (`<Errored>` /
105 * `throwOnError`). The v5 `TData | undefined` face existed because reads
106 * could observe the pre-fetch gap; here that gap is suspension.
107 */
108export type UseBaseQueryResult<
109 TData = unknown,
110 TError = DefaultError,
111> = Override<
112 OmitKeyof<QueryObserverResult<TData, TError>, 'isInitialLoading'>,
113 { data: TData }
114>
115β
116/**
117 * The object `useQuery` returns. Same as {@link UseBaseQueryResult}.
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 * Distributes an `isInitialLoading` omit over result unions so status
128 * discriminants survive (a plain Omit over a union collapses it).
129 */
130type OmitIsInitialLoading<T> = T extends unknown
131 ? OmitKeyof<T, 'isInitialLoading' & keyof T>
132 : never
133β
134/**
135 * The object `useQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
136 * `select` changes `TData` to include `undefined`).
137 * @template TData - The type `data` ends up as, after `select` runs (if set).
138 * @template TError - The type of errors this query may hold.
139 */
140export type DefinedUseBaseQueryResult<
141 TData = unknown,
142 TError = DefaultError,
143> = OmitIsInitialLoading<DefinedQueryObserverResult<TData, TError>>
144β
145/**
146 * The object `useQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
147 * `select` changes `TData` to include `undefined`).
148 * @template TData - The type `data` ends up as, after `select` runs (if set).
149 * @template TError - The type of errors this query may hold.
150 */
151export type DefinedUseQueryResult<
152 TData = unknown,
153 TError = DefaultError,
154> = DefinedUseBaseQueryResult<TData, TError>
155β
156/* --- Create Infinite Queries Types --- */
157/**
158 * The options accepted by `useInfiniteQuery`.
159 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
160 * @template TError - The type of errors your `queryFn` may throw.
161 * @template TData - The type `data` ends up as after `select` runs.
162 * @template TQueryKey - The type of your `queryKey`.
163 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
164 */
165export interface InfiniteQueryOptions<
166 TQueryFnData = unknown,
167 TError = DefaultError,
168 TData = TQueryFnData,
169 TQueryKey extends QueryKey = QueryKey,
170 TPageParam = unknown,
171> extends OmitKeyof<
172 InfiniteQueryObserverOptions<
173 TQueryFnData,
174 TError,
175 TData,
176 TQueryKey,
177 TPageParam
178 >,
179 'queryKey' | 'suspense'
180> {
181 /**
182 * The query key to use for this query. Required here, unlike on the options this type extends.
183 *
184 * The query key will be hashed into a stable hash. See [Query Keys](https://tanstack.com/query/latest/docs/framework/solid/guides/query-keys)
185 * for more information.
186 *
187 * The query will automatically update when this key changes (as long as `enabled` is not set to `false`).
188 */
189 queryKey: TQueryKey
190 /**
191 * Defer the SSR stream flush until this query resolves on the server β
192 * see {@link UseBaseQueryOptions.deferStream}.
193 */
194 deferStream?: boolean
195 /**
196 * Reconciliation key for the data store β see
197 * {@link UseBaseQueryOptions.reconcile}. For infinite queries this keys
198 * items within pages; pages themselves merge positionally.
199 */
200 reconcile?: string | ((item: NonNullable<any>) => any) | null
201}
202β
203/**
204 * The accessor `useInfiniteQuery` expects as its first argument β Solid re-evaluates it reactively, so
205 * `queryKey` and other options can depend on signals.
206 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
207 * @template TError - The type of errors your `queryFn` may throw.
208 * @template TData - The type `data` ends up as after `select` runs.
209 * @template TQueryKey - The type of your `queryKey`.
210 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
211 */
212export type UseInfiniteQueryOptions<
213 TQueryFnData = unknown,
214 TError = DefaultError,
215 TData = TQueryFnData,
216 TQueryKey extends QueryKey = QueryKey,
217 TPageParam = unknown,
218> = Accessor<
219 InfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>
220>
221β
222/** Non-optional `data` for the same reason as {@link UseBaseQueryResult}. */
223export type UseInfiniteQueryResult<
224 TData = unknown,
225 TError = DefaultError,
226> = Override<
227 OmitKeyof<InfiniteQueryObserverResult<TData, TError>, 'isInitialLoading'>,
228 { data: TData }
229>
230β
231/**
232 * The object `useInfiniteQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
233 * `select` changes `TData` to include `undefined`).
234 * @template TData - The type `data` ends up as, after `select` runs (if set).
235 * @template TError - The type of errors this query may hold.
236 */
237export type DefinedUseInfiniteQueryResult<
238 TData = unknown,
239 TError = DefaultError,
240> = OmitIsInitialLoading<DefinedInfiniteQueryObserverResult<TData, TError>>
241β
242/* --- Create Mutation Types --- */
243/**
244 * The options accepted by `useMutation` and `mutationOptions`.
245 * @template TData - The type your `mutationFn` resolves to.
246 * @template TError - The type of errors your `mutationFn` may throw.
247 * @template TVariables - The type of the variables your `mutationFn` accepts.
248 * @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
249 */
250export interface MutationOptions<
251 TData = unknown,
252 TError = DefaultError,
253 TVariables = void,
254 TOnMutateResult = unknown,
255> extends OmitKeyof<
256 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
257 '_defaulted'
258> {}
259β
260/**
261 * The accessor `useMutation` expects as its first argument β Solid re-evaluates it reactively, so callbacks
262 * and other options can depend on signals.
263 * @template TData - The type your `mutationFn` resolves to.
264 * @template TError - The type of errors your `mutationFn` may throw.
265 * @template TVariables - The type of the variables your `mutationFn` accepts.
266 * @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
267 */
268export type UseMutationOptions<
269 TData = unknown,
270 TError = DefaultError,
271 TVariables = void,
272 TOnMutateResult = unknown,
273> = Accessor<MutationOptions<TData, TError, TVariables, TOnMutateResult>>
274β
275/**
276 * One `mutate`, returning a safe-to-ignore promise: errors are also routed
277 * into reactive state, and ignoring the promise never surfaces an
278 * unhandled rejection. Call-site callbacks are gone β settle logic that
279 * used to live in `mutate(vars, { onSuccess })` is linear code after
280 * `await mutate(vars)` or a config-level callback.
281 */
282export type UseMutateFunction<
283 TData = unknown,
284 // oxlint-disable-next-line tanstack-query/type-parameter-naming
285 _TError = DefaultError,
286 TVariables = void,
287 // oxlint-disable-next-line tanstack-query/type-parameter-naming
288 _TOnMutateResult = unknown,
289> = (
290 ...args: undefined extends TVariables
291 ? [variables?: TVariables]
292 : [variables: TVariables]
293) => Promise<TData>
294β
295/**
296 * The result of `useMutation`. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with
297 * `mutate` replaced by the promise-returning {@link UseMutateFunction}.
298 * @template TData - The type your `mutationFn` resolves to.
299 * @template TError - The type of errors your `mutationFn` may throw.
300 * @template TVariables - The type of the variable passed to `mutate`.
301 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
302 * their `onMutateResult` parameter β useful for optimistic-update rollback data.
303 */
304export type UseBaseMutationResult<
305 TData = unknown,
306 TError = DefaultError,
307 TVariables = unknown,
308 TOnMutateResult = unknown,
309> = Override<
310 OmitKeyof<
311 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
312 'context'
313 >,
314 { mutate: UseMutateFunction<TData, TError, TVariables, TOnMutateResult> }
315>
316β
317/**
318 * The result of `useMutation`. Same as {@link UseBaseMutationResult}.
319 * @template TData - The type your `mutationFn` resolves to.
320 * @template TError - The type of errors your `mutationFn` may throw.
321 * @template TVariables - The type of the variable passed to `mutate`.
322 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
323 * their `onMutateResult` parameter β useful for optimistic-update rollback data.
324 */
325export type UseMutationResult<
326 TData = unknown,
327 TError = DefaultError,
328 TVariables = unknown,
329 TOnMutateResult = unknown,
330> = UseBaseMutationResult<TData, TError, TVariables, TOnMutateResult>