@tanstack/solid-query 6.0.0-rc.4 → 6.0.0-rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/build/dev.cjs +1470 -849
- package/build/dev.js +1455 -842
- package/build/index.cjs +3 -913
- package/build/index.d.cts +1191 -177
- package/build/index.d.cts.map +1 -0
- package/build/index.d.ts +1191 -177
- package/build/index.d.ts.map +1 -0
- package/build/index.js +4 -896
- package/build/index.js.map +1 -0
- package/package.json +23 -24
- package/src/QueryClient.ts +24 -0
- package/src/QueryClientProvider.tsx +33 -16
- package/src/cacheAggregate.ts +6 -3
- package/src/dehydrateSettled.ts +3 -0
- package/src/index.ts +1 -1
- package/src/infiniteQueryOptions.ts +117 -8
- package/src/isRestoring.ts +6 -0
- package/src/mutationOptions.ts +51 -0
- package/src/queryOptions.ts +112 -13
- package/src/types.ts +115 -3
- package/src/useBaseQuery.ts +93 -53
- package/src/useInfiniteQuery.ts +133 -11
- package/src/useIsFetching.ts +21 -1
- package/src/useIsMutating.ts +20 -1
- package/src/useMutation.ts +19 -9
- package/src/useMutationState.ts +108 -7
- package/src/useQueries.ts +86 -1
- package/src/useQuery.ts +207 -4
- package/build/_tsup-dts-rollup.d.cts +0 -922
- package/build/_tsup-dts-rollup.d.ts +0 -922
- package/build/dev.d.cts +0 -177
- package/build/dev.d.ts +0 -177
package/build/index.d.cts
CHANGED
|
@@ -1,177 +1,1191 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
export
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
1
|
+
import { DefaultError, DefaultOptions as DefaultOptions$1, DefinedInfiniteQueryObserverResult, DefinedQueryObserverResult, DehydrateOptions, DehydratedState, InfiniteData, InfiniteQueryObserverOptions as InfiniteQueryObserverOptions$1, InfiniteQueryObserverResult, InitialDataFunction, Mutation, MutationFilters, MutationObserverOptions, MutationObserverResult, MutationState, NonUndefinedGuard, OmitKeyof, Override, QueriesPlaceholderDataFunction, QueryClient as QueryClient$1, QueryClientConfig as QueryClientConfig$1, QueryFilters, QueryFunction, QueryKey, QueryKeyWithDataTag, QueryObserverOptions as QueryObserverOptions$1, QueryObserverResult, ThrowOnError, WithRequired } from "@tanstack/query-core";
|
|
2
|
+
import { Accessor } from "solid-js";
|
|
3
|
+
import { JSX } from "@solidjs/web";
|
|
4
|
+
export * from "@tanstack/query-core";
|
|
5
|
+
/**
|
|
6
|
+
* Core observer options pass through unchanged. The old adapter omitted
|
|
7
|
+
* `structuralSharing` and replaced it with a store-level `reconcile`
|
|
8
|
+
* option; with reads derived directly from cache state, core's
|
|
9
|
+
* cache-level structural sharing is exactly what keeps the data memo
|
|
10
|
+
* referentially stable, so it is exposed again and `reconcile` is gone.
|
|
11
|
+
*/
|
|
12
|
+
type QueryObserverOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = never> = QueryObserverOptions$1<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>;
|
|
13
|
+
/**
|
|
14
|
+
* The core `InfiniteQueryObserverOptions`, passed through unchanged.
|
|
15
|
+
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
16
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
17
|
+
* @template TData - The type `data` ends up as after `select` runs.
|
|
18
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
19
|
+
* @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
|
|
20
|
+
*/
|
|
21
|
+
type InfiniteQueryObserverOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = InfiniteQueryObserverOptions$1<TQueryFnData, TError, TData, TQueryKey, TPageParam>;
|
|
22
|
+
/**
|
|
23
|
+
* The default options a `QueryClient` applies to every query and mutation.
|
|
24
|
+
* @template TError - The default type of errors thrown by queries and mutations using this `QueryClient`.
|
|
25
|
+
*/
|
|
26
|
+
interface DefaultOptions<TError = DefaultError> extends DefaultOptions$1<TError> {
|
|
27
|
+
/**
|
|
28
|
+
* Default options applied to every query, unless overridden per-query.
|
|
29
|
+
*/
|
|
30
|
+
queries?: OmitKeyof<QueryObserverOptions<unknown, TError>, 'queryKey'>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The config accepted by `new QueryClient(config)`, with Solid's {@link DefaultOptions}.
|
|
34
|
+
*/
|
|
35
|
+
interface QueryClientConfig extends QueryClientConfig$1 {
|
|
36
|
+
/**
|
|
37
|
+
* The default options of the queries and mutations of this `QueryClient`.
|
|
38
|
+
*/
|
|
39
|
+
defaultOptions?: DefaultOptions;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The core `@tanstack/query-core` `QueryClient`, typed with Solid's {@link QueryClientConfig}.
|
|
43
|
+
*/
|
|
44
|
+
export declare class QueryClient extends QueryClient$1 {
|
|
45
|
+
constructor(config?: QueryClientConfig);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The options accepted by `useQuery`. Extends {@link QueryObserverOptions} from `@tanstack/query-core` with
|
|
49
|
+
* the `solid-query`-specific `deferStream` and `reconcile` options.
|
|
50
|
+
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
51
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
52
|
+
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
|
|
53
|
+
* `select` is used.
|
|
54
|
+
* @template TQueryData - The type of the data actually held in the query cache — the input to `select` and
|
|
55
|
+
* `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
|
|
56
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
57
|
+
*/
|
|
58
|
+
interface UseBaseQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> extends OmitKeyof<QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>, 'suspense'> {
|
|
59
|
+
/**
|
|
60
|
+
* Defer the SSR stream flush until this query resolves on the server,
|
|
61
|
+
* instead of letting the surrounding `<Loading>` boundary render its
|
|
62
|
+
* fallback into the HTML. Passed through to Solid's own per-computation
|
|
63
|
+
* `deferStream` option; server-only, ignored on the client.
|
|
64
|
+
*/
|
|
65
|
+
deferStream?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Reconciliation key for the data store. `data` is served as a Solid
|
|
68
|
+
* store projection: every landing (refetch, invalidation, placeholder
|
|
69
|
+
* upgrade) reconciles into the existing proxy graph instead of replacing
|
|
70
|
+
* it, so deep reads are fine-grained and item identity survives across
|
|
71
|
+
* fetches. This sets the identity key for that reconciliation — a
|
|
72
|
+
* property name, a key-extraction function, or `null` to merge
|
|
73
|
+
* positionally. Defaults to `'id'`. Read once at creation.
|
|
74
|
+
*/
|
|
75
|
+
reconcile?: string | ((item: NonNullable<any>) => any) | null;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The options accepted by `useQuery` and `queryOptions`.
|
|
79
|
+
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
80
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
81
|
+
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
|
|
82
|
+
* `select` is used.
|
|
83
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
84
|
+
*/
|
|
85
|
+
interface QueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> extends UseBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey> {}
|
|
86
|
+
/**
|
|
87
|
+
* The accessor `useQuery` expects as its first argument — Solid re-evaluates it reactively, so `queryKey` and
|
|
88
|
+
* other options can depend on signals.
|
|
89
|
+
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
90
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
91
|
+
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
|
|
92
|
+
* `select` is used.
|
|
93
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
94
|
+
*/
|
|
95
|
+
type UseQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = Accessor<QueryOptions<TQueryFnData, TError, TData, TQueryKey>>;
|
|
96
|
+
/**
|
|
97
|
+
* `data` is non-optional: it is a suspending async read. It never returns
|
|
98
|
+
* `undefined` — a read either suspends into the nearest `<Loading>`
|
|
99
|
+
* boundary (first fetch in flight, disabled, restoring), returns a value
|
|
100
|
+
* (committed, placeholder, initial), or throws (`<Errored>` /
|
|
101
|
+
* `throwOnError`). The v5 `TData | undefined` face existed because reads
|
|
102
|
+
* could observe the pre-fetch gap; here that gap is suspension.
|
|
103
|
+
*/
|
|
104
|
+
type UseBaseQueryResult<TData = unknown, TError = DefaultError> = Override<OmitKeyof<QueryObserverResult<TData, TError>, 'isInitialLoading'>, {
|
|
105
|
+
data: TData;
|
|
106
|
+
}>;
|
|
107
|
+
/**
|
|
108
|
+
* The object `useQuery` returns. Same as {@link UseBaseQueryResult}.
|
|
109
|
+
* @template TData - The type `data` ends up as, after `select` runs (if set).
|
|
110
|
+
* @template TError - The type of errors this query may hold.
|
|
111
|
+
*/
|
|
112
|
+
type UseQueryResult<TData = unknown, TError = DefaultError> = UseBaseQueryResult<TData, TError>;
|
|
113
|
+
/**
|
|
114
|
+
* Distributes an `isInitialLoading` omit over result unions so status
|
|
115
|
+
* discriminants survive (a plain Omit over a union collapses it).
|
|
116
|
+
*/
|
|
117
|
+
type OmitIsInitialLoading<T> = T extends unknown ? OmitKeyof<T, 'isInitialLoading' & keyof T> : never;
|
|
118
|
+
/**
|
|
119
|
+
* The object `useQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
|
|
120
|
+
* `select` changes `TData` to include `undefined`).
|
|
121
|
+
* @template TData - The type `data` ends up as, after `select` runs (if set).
|
|
122
|
+
* @template TError - The type of errors this query may hold.
|
|
123
|
+
*/
|
|
124
|
+
type DefinedUseBaseQueryResult<TData = unknown, TError = DefaultError> = OmitIsInitialLoading<DefinedQueryObserverResult<TData, TError>>;
|
|
125
|
+
/**
|
|
126
|
+
* The object `useQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
|
|
127
|
+
* `select` changes `TData` to include `undefined`).
|
|
128
|
+
* @template TData - The type `data` ends up as, after `select` runs (if set).
|
|
129
|
+
* @template TError - The type of errors this query may hold.
|
|
130
|
+
*/
|
|
131
|
+
type DefinedUseQueryResult<TData = unknown, TError = DefaultError> = DefinedUseBaseQueryResult<TData, TError>;
|
|
132
|
+
/**
|
|
133
|
+
* The options accepted by `useInfiniteQuery`.
|
|
134
|
+
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
135
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
136
|
+
* @template TData - The type `data` ends up as after `select` runs.
|
|
137
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
138
|
+
* @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
|
|
139
|
+
*/
|
|
140
|
+
interface InfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends OmitKeyof<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, 'queryKey' | 'suspense'> {
|
|
141
|
+
/**
|
|
142
|
+
* The query key to use for this query. Required here, unlike on the options this type extends.
|
|
143
|
+
*
|
|
144
|
+
* The query key will be hashed into a stable hash. See [Query Keys](https://tanstack.com/query/latest/docs/framework/solid/guides/query-keys)
|
|
145
|
+
* for more information.
|
|
146
|
+
*
|
|
147
|
+
* The query will automatically update when this key changes (as long as `enabled` is not set to `false`).
|
|
148
|
+
*/
|
|
149
|
+
queryKey: TQueryKey;
|
|
150
|
+
/**
|
|
151
|
+
* Defer the SSR stream flush until this query resolves on the server —
|
|
152
|
+
* see {@link UseBaseQueryOptions.deferStream}.
|
|
153
|
+
*/
|
|
154
|
+
deferStream?: boolean;
|
|
155
|
+
/**
|
|
156
|
+
* Reconciliation key for the data store — see
|
|
157
|
+
* {@link UseBaseQueryOptions.reconcile}. For infinite queries this keys
|
|
158
|
+
* items within pages; pages themselves merge positionally.
|
|
159
|
+
*/
|
|
160
|
+
reconcile?: string | ((item: NonNullable<any>) => any) | null;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The accessor `useInfiniteQuery` expects as its first argument — Solid re-evaluates it reactively, so
|
|
164
|
+
* `queryKey` and other options can depend on signals.
|
|
165
|
+
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
166
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
167
|
+
* @template TData - The type `data` ends up as after `select` runs.
|
|
168
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
169
|
+
* @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
|
|
170
|
+
*/
|
|
171
|
+
type UseInfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = Accessor<InfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>>;
|
|
172
|
+
/** Non-optional `data` for the same reason as {@link UseBaseQueryResult}. */
|
|
173
|
+
type UseInfiniteQueryResult<TData = unknown, TError = DefaultError> = Override<OmitKeyof<InfiniteQueryObserverResult<TData, TError>, 'isInitialLoading'>, {
|
|
174
|
+
data: TData;
|
|
175
|
+
}>;
|
|
176
|
+
/**
|
|
177
|
+
* The object `useInfiniteQuery` returns when `initialData` guarantees `data` is never `undefined` (unless a
|
|
178
|
+
* `select` changes `TData` to include `undefined`).
|
|
179
|
+
* @template TData - The type `data` ends up as, after `select` runs (if set).
|
|
180
|
+
* @template TError - The type of errors this query may hold.
|
|
181
|
+
*/
|
|
182
|
+
type DefinedUseInfiniteQueryResult<TData = unknown, TError = DefaultError> = OmitIsInitialLoading<DefinedInfiniteQueryObserverResult<TData, TError>>;
|
|
183
|
+
/**
|
|
184
|
+
* The options accepted by `useMutation` and `mutationOptions`.
|
|
185
|
+
* @template TData - The type your `mutationFn` resolves to.
|
|
186
|
+
* @template TError - The type of errors your `mutationFn` may throw.
|
|
187
|
+
* @template TVariables - The type of the variables your `mutationFn` accepts.
|
|
188
|
+
* @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
|
|
189
|
+
*/
|
|
190
|
+
interface MutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown> extends OmitKeyof<MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>, '_defaulted'> {}
|
|
191
|
+
/**
|
|
192
|
+
* The accessor `useMutation` expects as its first argument — Solid re-evaluates it reactively, so callbacks
|
|
193
|
+
* and other options can depend on signals.
|
|
194
|
+
* @template TData - The type your `mutationFn` resolves to.
|
|
195
|
+
* @template TError - The type of errors your `mutationFn` may throw.
|
|
196
|
+
* @template TVariables - The type of the variables your `mutationFn` accepts.
|
|
197
|
+
* @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
|
|
198
|
+
*/
|
|
199
|
+
type UseMutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown> = Accessor<MutationOptions<TData, TError, TVariables, TOnMutateResult>>;
|
|
200
|
+
/**
|
|
201
|
+
* One `mutate`, returning a safe-to-ignore promise: errors are also routed
|
|
202
|
+
* into reactive state, and ignoring the promise never surfaces an
|
|
203
|
+
* unhandled rejection. Call-site callbacks are gone — settle logic that
|
|
204
|
+
* used to live in `mutate(vars, { onSuccess })` is linear code after
|
|
205
|
+
* `await mutate(vars)` or a config-level callback.
|
|
206
|
+
*/
|
|
207
|
+
type UseMutateFunction<TData = unknown, _TError = DefaultError, TVariables = void, _TOnMutateResult = unknown> = (...args: undefined extends TVariables ? [variables?: TVariables] : [variables: TVariables]) => Promise<TData>;
|
|
208
|
+
/**
|
|
209
|
+
* The result of `useMutation`. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with
|
|
210
|
+
* `mutate` replaced by the promise-returning {@link UseMutateFunction}.
|
|
211
|
+
* @template TData - The type your `mutationFn` resolves to.
|
|
212
|
+
* @template TError - The type of errors your `mutationFn` may throw.
|
|
213
|
+
* @template TVariables - The type of the variable passed to `mutate`.
|
|
214
|
+
* @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
|
|
215
|
+
* their `onMutateResult` parameter — useful for optimistic-update rollback data.
|
|
216
|
+
*/
|
|
217
|
+
type UseBaseMutationResult<TData = unknown, TError = DefaultError, TVariables = unknown, TOnMutateResult = unknown> = Override<OmitKeyof<MutationObserverResult<TData, TError, TVariables, TOnMutateResult>, 'context'>, {
|
|
218
|
+
mutate: UseMutateFunction<TData, TError, TVariables, TOnMutateResult>;
|
|
219
|
+
}>;
|
|
220
|
+
/**
|
|
221
|
+
* The result of `useMutation`. Same as {@link UseBaseMutationResult}.
|
|
222
|
+
* @template TData - The type your `mutationFn` resolves to.
|
|
223
|
+
* @template TError - The type of errors your `mutationFn` may throw.
|
|
224
|
+
* @template TVariables - The type of the variable passed to `mutate`.
|
|
225
|
+
* @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
|
|
226
|
+
* their `onMutateResult` parameter — useful for optimistic-update rollback data.
|
|
227
|
+
*/
|
|
228
|
+
type UseMutationResult<TData = unknown, TError = DefaultError, TVariables = unknown, TOnMutateResult = unknown> = UseBaseMutationResult<TData, TError, TVariables, TOnMutateResult>;
|
|
229
|
+
/**
|
|
230
|
+
* The options accepted by the `queryOptions` overload selected when `initialData` is omitted, may be
|
|
231
|
+
* `undefined`, or is a function that may return `undefined` — `data` may be `undefined` while the query is
|
|
232
|
+
* `pending`. `queryOptions` itself accepts and returns a plain object (its parameter type is
|
|
233
|
+
* `ReturnType<UndefinedInitialDataOptions<...>>`, i.e. this `Accessor` called); Solid's reactivity applies
|
|
234
|
+
* where the result is consumed instead, e.g. `useQuery(() => options)`.
|
|
235
|
+
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
236
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
237
|
+
* @template TData - The type `data` ends up as after `select` runs.
|
|
238
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
239
|
+
*/
|
|
240
|
+
type UndefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = Accessor<QueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
241
|
+
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>> | NonUndefinedGuard<TQueryFnData>;
|
|
242
|
+
}>;
|
|
243
|
+
/**
|
|
244
|
+
* The options accepted by the `queryOptions` overload selected when `initialData` is known to be defined —
|
|
245
|
+
* `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
246
|
+
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
247
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
248
|
+
* @template TData - The type `data` ends up as after `select` runs.
|
|
249
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
250
|
+
*/
|
|
251
|
+
type DefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = Accessor<QueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
252
|
+
initialData: NonUndefinedGuard<TQueryFnData> | (() => NonUndefinedGuard<TQueryFnData>);
|
|
253
|
+
}>;
|
|
254
|
+
/**
|
|
255
|
+
* You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
|
|
256
|
+
* be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
|
|
257
|
+
* is the query key to generate options for.
|
|
258
|
+
*
|
|
259
|
+
* This overload is selected when `initialData` is known to be defined, so the resulting `data` is never
|
|
260
|
+
* `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
261
|
+
* @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` known to be defined.
|
|
262
|
+
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
|
|
263
|
+
* @see {@link useQuery} to run a query with these options.
|
|
264
|
+
* @example
|
|
265
|
+
* ```tsx
|
|
266
|
+
* import { For } from 'solid-js'
|
|
267
|
+
* import { queryOptions, useQuery } from '@tanstack/solid-query'
|
|
268
|
+
*
|
|
269
|
+
* const postsOptions = queryOptions({
|
|
270
|
+
* queryKey: ['posts'],
|
|
271
|
+
* queryFn: fetchPosts,
|
|
272
|
+
* initialData: [],
|
|
273
|
+
* })
|
|
274
|
+
*
|
|
275
|
+
* function Posts() {
|
|
276
|
+
* // `postsQuery.data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
|
|
277
|
+
* // so the list stays visible alongside the error.
|
|
278
|
+
* const postsQuery = useQuery(() => postsOptions)
|
|
279
|
+
*
|
|
280
|
+
* return (
|
|
281
|
+
* <div>
|
|
282
|
+
* {postsQuery.isError ? <span>Error: {postsQuery.error.message}</span> : null}
|
|
283
|
+
* <ul>
|
|
284
|
+
* <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
|
|
285
|
+
* </ul>
|
|
286
|
+
* </div>
|
|
287
|
+
* )
|
|
288
|
+
* }
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
291
|
+
export declare function queryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: ReturnType<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>): ReturnType<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
|
|
292
|
+
/**
|
|
293
|
+
* You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
|
|
294
|
+
* be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
|
|
295
|
+
* is the query key to generate options for.
|
|
296
|
+
* @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
|
|
297
|
+
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
|
|
298
|
+
* @see {@link useQuery} to run a query with these options.
|
|
299
|
+
* @example
|
|
300
|
+
* A parameterized factory, so the same options object can be reused per `id`:
|
|
301
|
+
* ```tsx
|
|
302
|
+
* import { Match, Switch } from 'solid-js'
|
|
303
|
+
* import { queryOptions, useQuery } from '@tanstack/solid-query'
|
|
304
|
+
*
|
|
305
|
+
* const postOptions = (id: string) =>
|
|
306
|
+
* queryOptions({
|
|
307
|
+
* queryKey: ['post', id],
|
|
308
|
+
* queryFn: () => fetchPost(id),
|
|
309
|
+
* })
|
|
310
|
+
*
|
|
311
|
+
* function Post(props: { id: string }) {
|
|
312
|
+
* const postQuery = useQuery(() => postOptions(props.id))
|
|
313
|
+
*
|
|
314
|
+
* return (
|
|
315
|
+
* <Switch>
|
|
316
|
+
* <Match when={postQuery.isPending}>Loading...</Match>
|
|
317
|
+
* <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
|
|
318
|
+
* <Match when={postQuery.isSuccess}>
|
|
319
|
+
* <h1>{postQuery.data.title}</h1>
|
|
320
|
+
* </Match>
|
|
321
|
+
* </Switch>
|
|
322
|
+
* )
|
|
323
|
+
* }
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
export declare function queryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: ReturnType<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>): ReturnType<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
|
|
327
|
+
/**
|
|
328
|
+
* Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
329
|
+
* The query runs when the options call for it — `enabled: false` skips the initial fetch.
|
|
330
|
+
*
|
|
331
|
+
* This overload is selected when `initialData` is known to be defined, so the resulting `data` is typed as never
|
|
332
|
+
* `undefined` (unless a `select` changes `TData` to include `undefined`). At runtime, `data` is still `undefined`
|
|
333
|
+
* if `select` throws on its first call.
|
|
334
|
+
* @param options - An accessor returning the {@link DefinedInitialDataOptions} to use — everything you can
|
|
335
|
+
* pass to `useQuery`, with `initialData` known to be defined.
|
|
336
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
337
|
+
* will be used.
|
|
338
|
+
* @returns The current query result, as a Solid store, typed so that `status` is `success` or `error`
|
|
339
|
+
* (`status` never resolves to `pending` in this overload's type, since `initialData` guarantees data upfront).
|
|
340
|
+
* `status` is `error` if the query has an error from a fetch that failed after its retries or if `select`
|
|
341
|
+
* threw, and `success` otherwise. `isSuccess`/`isError` are derived booleans for convenience.
|
|
342
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
343
|
+
* @example
|
|
344
|
+
* ```tsx
|
|
345
|
+
* import { For } from 'solid-js'
|
|
346
|
+
* import { useQuery } from '@tanstack/solid-query'
|
|
347
|
+
*
|
|
348
|
+
* function Posts() {
|
|
349
|
+
* // `postsQuery.data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the
|
|
350
|
+
* // list stays visible alongside the error.
|
|
351
|
+
* const postsQuery = useQuery(() => ({
|
|
352
|
+
* queryKey: ['posts'],
|
|
353
|
+
* queryFn: fetchPosts,
|
|
354
|
+
* initialData: [],
|
|
355
|
+
* }))
|
|
356
|
+
*
|
|
357
|
+
* return (
|
|
358
|
+
* <div>
|
|
359
|
+
* {postsQuery.isError ? <span>Error: {postsQuery.error.message}</span> : null}
|
|
360
|
+
* <ul>
|
|
361
|
+
* <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
|
|
362
|
+
* </ul>
|
|
363
|
+
* </div>
|
|
364
|
+
* )
|
|
365
|
+
* }
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
export declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: () => QueryClient): DefinedUseQueryResult<TData, TError>;
|
|
369
|
+
/**
|
|
370
|
+
* Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
371
|
+
* The query runs when the options call for it — `enabled: false` skips the initial fetch.
|
|
372
|
+
* @param options - An accessor returning the {@link UndefinedInitialDataOptions} to use — everything you can
|
|
373
|
+
* pass to `useQuery`.
|
|
374
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
375
|
+
* will be used.
|
|
376
|
+
* @returns The current query result, as a Solid store. `status` is `error` if the query has an error from
|
|
377
|
+
* a fetch that failed after its retries or if `select` threw, `success` if the query has data to display
|
|
378
|
+
* (including `placeholderData`), and `pending` otherwise. `isPending`/`isSuccess`/`isError` are derived
|
|
379
|
+
* booleans for convenience.
|
|
380
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
381
|
+
* @example
|
|
382
|
+
* ```tsx
|
|
383
|
+
* import { For, Match, Switch } from 'solid-js'
|
|
384
|
+
* import { useQuery } from '@tanstack/solid-query'
|
|
385
|
+
*
|
|
386
|
+
* function Posts() {
|
|
387
|
+
* const postsQuery = useQuery(() => ({
|
|
388
|
+
* queryKey: ['posts'],
|
|
389
|
+
* queryFn: fetchPosts,
|
|
390
|
+
* }))
|
|
391
|
+
*
|
|
392
|
+
* return (
|
|
393
|
+
* <Switch>
|
|
394
|
+
* <Match when={postsQuery.isPending}>Loading...</Match>
|
|
395
|
+
* <Match when={postsQuery.isError}>Error: {postsQuery.error.message}</Match>
|
|
396
|
+
* <Match when={postsQuery.isSuccess}>
|
|
397
|
+
* <ul>
|
|
398
|
+
* <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
|
|
399
|
+
* </ul>
|
|
400
|
+
* <div>{postsQuery.isFetching ? 'Background Updating...' : ' '}</div>
|
|
401
|
+
* </Match>
|
|
402
|
+
* </Switch>
|
|
403
|
+
* )
|
|
404
|
+
* }
|
|
405
|
+
* ```
|
|
406
|
+
* @example
|
|
407
|
+
* `select` derives whatever `data` a component needs from the cached value, without changing what's
|
|
408
|
+
* actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:
|
|
409
|
+
* ```tsx
|
|
410
|
+
* import { Match, Switch } from 'solid-js'
|
|
411
|
+
* import { useQuery } from '@tanstack/solid-query'
|
|
412
|
+
*
|
|
413
|
+
* function PostCount() {
|
|
414
|
+
* const postsQuery = useQuery(() => ({
|
|
415
|
+
* queryKey: ['posts'],
|
|
416
|
+
* queryFn: fetchPosts,
|
|
417
|
+
* select: (posts) => posts.length,
|
|
418
|
+
* }))
|
|
419
|
+
*
|
|
420
|
+
* return (
|
|
421
|
+
* <Switch>
|
|
422
|
+
* <Match when={postsQuery.isPending}>Loading...</Match>
|
|
423
|
+
* <Match when={postsQuery.isError}>Error: {postsQuery.error.message}</Match>
|
|
424
|
+
* <Match when={postsQuery.isSuccess}>{postsQuery.data} posts</Match>
|
|
425
|
+
* </Switch>
|
|
426
|
+
* )
|
|
427
|
+
* }
|
|
428
|
+
* ```
|
|
429
|
+
* @example
|
|
430
|
+
* A dependent query, only enabled once `postId` is set:
|
|
431
|
+
* ```tsx
|
|
432
|
+
* import { Match, Switch } from 'solid-js'
|
|
433
|
+
* import { useQuery } from '@tanstack/solid-query'
|
|
434
|
+
*
|
|
435
|
+
* function Post(props: { postId: number | undefined }) {
|
|
436
|
+
* const postQuery = useQuery(() => ({
|
|
437
|
+
* queryKey: ['post', props.postId],
|
|
438
|
+
* queryFn: () => fetchPost(props.postId!),
|
|
439
|
+
* enabled: props.postId != null,
|
|
440
|
+
* }))
|
|
441
|
+
*
|
|
442
|
+
* return (
|
|
443
|
+
* <Switch fallback={<h1>{postQuery.data?.title}</h1>}>
|
|
444
|
+
* <Match when={props.postId == null}>Select a post</Match>
|
|
445
|
+
* <Match when={postQuery.isLoading}>Loading...</Match>
|
|
446
|
+
* <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
|
|
447
|
+
* </Switch>
|
|
448
|
+
* )
|
|
449
|
+
* }
|
|
450
|
+
* ```
|
|
451
|
+
* @example
|
|
452
|
+
* The same dependent query, using `skipToken` to disable it in a type-safe way instead of relying on
|
|
453
|
+
* `enabled`. The non-null assertion is still needed — Solid's `props` narrowing doesn't survive into the
|
|
454
|
+
* `queryFn` closure the way a local `const` would — but `skipToken` keeps `queryFn`'s return type accurate
|
|
455
|
+
* without it. `refetch` doesn't work while `queryFn` is `skipToken` — use `enabled: false` instead if you
|
|
456
|
+
* need to trigger the query manually:
|
|
457
|
+
* ```tsx
|
|
458
|
+
* import { Match, Switch } from 'solid-js'
|
|
459
|
+
* import { skipToken, useQuery } from '@tanstack/solid-query'
|
|
460
|
+
*
|
|
461
|
+
* function Post(props: { postId: number | undefined }) {
|
|
462
|
+
* const postQuery = useQuery(() => ({
|
|
463
|
+
* queryKey: ['post', props.postId],
|
|
464
|
+
* queryFn: props.postId != null ? () => fetchPost(props.postId!) : skipToken,
|
|
465
|
+
* }))
|
|
466
|
+
*
|
|
467
|
+
* return (
|
|
468
|
+
* <Switch fallback={<h1>{postQuery.data?.title}</h1>}>
|
|
469
|
+
* <Match when={props.postId == null}>Select a post</Match>
|
|
470
|
+
* <Match when={postQuery.isLoading}>Loading...</Match>
|
|
471
|
+
* <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
|
|
472
|
+
* </Switch>
|
|
473
|
+
* )
|
|
474
|
+
* }
|
|
475
|
+
* ```
|
|
476
|
+
* @example
|
|
477
|
+
* Seeding a detail query from an already-cached list, to skip the loading state. `initialDataUpdatedAt` carries
|
|
478
|
+
* over the list's own fetch time, so that if you set a `staleTime`, it's measured from when the list was
|
|
479
|
+
* fetched rather than from now:
|
|
480
|
+
* ```tsx
|
|
481
|
+
* import { useQuery, useQueryClient } from '@tanstack/solid-query'
|
|
482
|
+
*
|
|
483
|
+
* function Post(props: { postId: number }) {
|
|
484
|
+
* const queryClient = useQueryClient()
|
|
485
|
+
*
|
|
486
|
+
* const postQuery = useQuery(() => ({
|
|
487
|
+
* queryKey: ['post', props.postId],
|
|
488
|
+
* queryFn: () => fetchPost(props.postId),
|
|
489
|
+
* initialData: () =>
|
|
490
|
+
* queryClient
|
|
491
|
+
* .getQueryData<Array<Post>>(['posts'])
|
|
492
|
+
* ?.find((post) => post.id === props.postId),
|
|
493
|
+
* initialDataUpdatedAt: () =>
|
|
494
|
+
* queryClient.getQueryState(['posts'])?.dataUpdatedAt,
|
|
495
|
+
* }))
|
|
496
|
+
*
|
|
497
|
+
* return postQuery.isError ? <span>Error: {postQuery.error.message}</span> : <h1>{postQuery.data?.title}</h1>
|
|
498
|
+
* }
|
|
499
|
+
* ```
|
|
500
|
+
* @example
|
|
501
|
+
* Paginated data, keeping the previous page's data visible while the next page loads:
|
|
502
|
+
* ```tsx
|
|
503
|
+
* import { For, createSignal } from 'solid-js'
|
|
504
|
+
* import { keepPreviousData, useQuery } from '@tanstack/solid-query'
|
|
505
|
+
*
|
|
506
|
+
* function Posts() {
|
|
507
|
+
* const [page, setPage] = createSignal(0)
|
|
508
|
+
*
|
|
509
|
+
* const postsQuery = useQuery(() => ({
|
|
510
|
+
* queryKey: ['posts', page()],
|
|
511
|
+
* queryFn: () => fetchPosts(page()),
|
|
512
|
+
* placeholderData: keepPreviousData,
|
|
513
|
+
* }))
|
|
514
|
+
*
|
|
515
|
+
* return (
|
|
516
|
+
* <div>
|
|
517
|
+
* <ul>
|
|
518
|
+
* <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
|
|
519
|
+
* </ul>
|
|
520
|
+
* <button
|
|
521
|
+
* disabled={postsQuery.isPlaceholderData}
|
|
522
|
+
* onClick={() => setPage((old) => old + 1)}
|
|
523
|
+
* >
|
|
524
|
+
* Next Page
|
|
525
|
+
* </button>
|
|
526
|
+
* </div>
|
|
527
|
+
* )
|
|
528
|
+
* }
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
531
|
+
export declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: () => QueryClient): UseQueryResult<TData, TError>;
|
|
532
|
+
/**
|
|
533
|
+
* The query cache's single-flight source id. Mutation responses can fold
|
|
534
|
+
* fresh data for multiple caches at once (Solid's multi-source
|
|
535
|
+
* single-flight protocol); this is the slice the query cache claims — the
|
|
536
|
+
* provider subscribes its consumer under it, and server collectors
|
|
537
|
+
* register under it to produce the data:
|
|
538
|
+
*
|
|
539
|
+
* ```ts
|
|
540
|
+
* import { registerFlightDataSource } from '@solidjs/web/server-functions/server'
|
|
541
|
+
* import { FLIGHT_DATA_SOURCE, dehydrate } from '@tanstack/solid-query'
|
|
542
|
+
*
|
|
543
|
+
* registerFlightDataSource(FLIGHT_DATA_SOURCE, async (event, outcome) => {
|
|
544
|
+
* // rebuild the data for outcome.targetUrl into a QueryClient, then
|
|
545
|
+
* return dehydrate(queryClient)
|
|
546
|
+
* })
|
|
547
|
+
* ```
|
|
548
|
+
*
|
|
549
|
+
* The slice's payload is a `DehydratedState`; the provider consumes it
|
|
550
|
+
* with `hydrate()`, so every mounted query on those keys updates before
|
|
551
|
+
* the mutation's promise resolves — no follow-up refetches.
|
|
552
|
+
*
|
|
553
|
+
* A mutation can additionally declare its invalidation SCOPE with
|
|
554
|
+
* `X-Revalidate` keys (the `revalidate` option of Solid's `reload`/
|
|
555
|
+
* `redirect`/`respond` response helpers). The payload covers the slice of
|
|
556
|
+
* that scope the server could recompute; whatever a declared key matches
|
|
557
|
+
* beyond it is swept client-side (see the consumer below). The transport
|
|
558
|
+
* delivers a response carrying keys to the consumer whether or not the
|
|
559
|
+
* collector contributed a slice — with no slice, the whole scope is swept.
|
|
560
|
+
*/
|
|
561
|
+
export declare const FLIGHT_DATA_SOURCE = "sq";
|
|
562
|
+
export declare const QueryClientContext: import("solid-js").Context<(() => QueryClient) | null>;
|
|
563
|
+
/**
|
|
564
|
+
* The `useQueryClient` primitive returns the current `QueryClient` instance.
|
|
565
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
566
|
+
* be used.
|
|
567
|
+
* @returns The current `QueryClient` instance.
|
|
568
|
+
* @throws {Error} If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
|
|
569
|
+
* component tree.
|
|
570
|
+
*/
|
|
571
|
+
export declare const useQueryClient: (queryClient?: QueryClient) => QueryClient;
|
|
572
|
+
/**
|
|
573
|
+
* The props accepted by `QueryClientProvider`.
|
|
574
|
+
*/
|
|
575
|
+
type QueryClientProviderProps = {
|
|
576
|
+
/**
|
|
577
|
+
* **Required**
|
|
578
|
+
*
|
|
579
|
+
* The `QueryClient` instance to provide.
|
|
580
|
+
*/
|
|
581
|
+
client: QueryClient;
|
|
582
|
+
/**
|
|
583
|
+
* The components that get access to the provided `QueryClient`.
|
|
584
|
+
*/
|
|
585
|
+
children?: JSX.Element;
|
|
586
|
+
};
|
|
587
|
+
/**
|
|
588
|
+
* Provides the QueryClient and manages its mount lifecycle. On the server
|
|
589
|
+
* it also registers the cache serializer above; on the client, hooks prime
|
|
590
|
+
* the cache from their hash-keyed registry entries themselves (see
|
|
591
|
+
* `useBaseQuery`) and the provider subscribes the cache's single-flight
|
|
592
|
+
* consumer: mutation responses carrying a `FLIGHT_DATA_SOURCE` slice (a
|
|
593
|
+
* `DehydratedState` produced by a server collector registered under the
|
|
594
|
+
* same id) hydrate this client before the mutation's promise resolves.
|
|
595
|
+
* Subscribing is inert when no server collector exists — the server just
|
|
596
|
+
* folds nothing — so it is unconditional. One consumer per source: with
|
|
597
|
+
* nested providers, the innermost mounted one owns the slice.
|
|
598
|
+
* @param props - The `client` to provide, and the `children` that get access to it.
|
|
599
|
+
* @returns The provided `children`, wrapped so they can read the `QueryClient` via `useQueryClient`.
|
|
600
|
+
*/
|
|
601
|
+
export declare const QueryClientProvider: (props: QueryClientProviderProps) => JSX.Element;
|
|
602
|
+
/**
|
|
603
|
+
* Waits for every fetch the client has in flight to settle, then
|
|
604
|
+
* dehydrates. This is the extraction half of a single-flight collector:
|
|
605
|
+
* after route data functions run for the mutation's target URL, loaders
|
|
606
|
+
* commonly kick off prefetches without awaiting them — plain
|
|
607
|
+
* `dehydrate()` would snapshot those mid-fetch and ship nothing.
|
|
608
|
+
*
|
|
609
|
+
* ```ts
|
|
610
|
+
* registerFlightDataSource(FLIGHT_DATA_SOURCE, (event, outcome) =>
|
|
611
|
+
* loadFlightTarget({
|
|
612
|
+
* router,
|
|
613
|
+
* event,
|
|
614
|
+
* outcome,
|
|
615
|
+
* collect: () => dehydrateSettled(queryClient),
|
|
616
|
+
* }),
|
|
617
|
+
* )
|
|
618
|
+
* ```
|
|
619
|
+
*
|
|
620
|
+
* Settling is chased to quiescence — awaiting one batch of fetches can
|
|
621
|
+
* dispatch more (dependent queries keyed off a first result) — so an
|
|
622
|
+
* unconditionally self-refetching query would keep this pending; that's
|
|
623
|
+
* an app bug mirrored, not guarded.
|
|
624
|
+
* @param client - The `QueryClient` whose cache is dehydrated.
|
|
625
|
+
* @param options - The `DehydrateOptions` forwarded to `dehydrate`.
|
|
626
|
+
* @returns A promise for the dehydrated state, taken once no fetch is in flight.
|
|
627
|
+
*/
|
|
628
|
+
export declare function dehydrateSettled(client: QueryClient, options?: DehydrateOptions): Promise<DehydratedState>;
|
|
629
|
+
/**
|
|
630
|
+
* The `useIsFetching` primitive returns the `number` of the queries that your application is loading or fetching
|
|
631
|
+
* in the background (useful for app-wide loading indicators).
|
|
632
|
+
* @param filters - An accessor returning the {@link QueryFilters} to narrow down the matched queries.
|
|
633
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
634
|
+
* will be used.
|
|
635
|
+
* @returns An accessor for the `number` of the queries that your application is currently loading or fetching
|
|
636
|
+
* in the background.
|
|
637
|
+
* @example
|
|
638
|
+
* ```tsx
|
|
639
|
+
* import { useIsFetching } from '@tanstack/solid-query'
|
|
640
|
+
*
|
|
641
|
+
* function GlobalLoadingIndicator() {
|
|
642
|
+
* // How many queries matching the posts prefix are fetching?
|
|
643
|
+
* const isFetchingPosts = useIsFetching(() => ({ queryKey: ['posts'] }))
|
|
644
|
+
*
|
|
645
|
+
* return isFetchingPosts() > 0 ? <span>Loading posts...</span> : null
|
|
646
|
+
* }
|
|
647
|
+
* ```
|
|
648
|
+
*/
|
|
649
|
+
export declare function useIsFetching(filters?: Accessor<Omit<QueryFilters, 'fetchStatus'>>, queryClient?: Accessor<QueryClient>): Accessor<number>;
|
|
650
|
+
/**
|
|
651
|
+
* The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is omitted, may be
|
|
652
|
+
* `undefined`, or is a function that may return `undefined` — `data` may be `undefined` while the query is
|
|
653
|
+
* `pending`. `infiniteQueryOptions` itself accepts and returns a plain object (its parameter type is
|
|
654
|
+
* `ReturnType<UndefinedInitialDataInfiniteOptions<...>>`, i.e. this `Accessor` called); Solid's reactivity
|
|
655
|
+
* applies where the result is consumed instead, e.g. `useInfiniteQuery(() => options)`.
|
|
656
|
+
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
657
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
658
|
+
* @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
|
|
659
|
+
* the shape of all fetched pages plus their page params.
|
|
660
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
661
|
+
* @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
|
|
662
|
+
*/
|
|
663
|
+
type UndefinedInitialDataInfiniteOptions<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = Accessor<InfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & {
|
|
664
|
+
initialData?: undefined | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>> | InitialDataFunction<NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>>;
|
|
665
|
+
}>;
|
|
666
|
+
/**
|
|
667
|
+
* The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be
|
|
668
|
+
* defined — `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
669
|
+
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
670
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
671
|
+
* @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
|
|
672
|
+
* the shape of all fetched pages plus their page params.
|
|
673
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
674
|
+
* @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
|
|
675
|
+
*/
|
|
676
|
+
type DefinedInitialDataInfiniteOptions<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = Accessor<InfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> & {
|
|
677
|
+
initialData: NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>> | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>);
|
|
678
|
+
}>;
|
|
679
|
+
/**
|
|
680
|
+
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
|
|
681
|
+
* These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
|
|
682
|
+
* `options.queryKey` is required and is the query key to generate options for.
|
|
683
|
+
*
|
|
684
|
+
* This overload is selected when `initialData` is known to be defined.
|
|
685
|
+
* @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` known to be defined.
|
|
686
|
+
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
|
|
687
|
+
* @see {@link useInfiniteQuery} to run an infinite query with these options.
|
|
688
|
+
* @example
|
|
689
|
+
* ```tsx
|
|
690
|
+
* import { For } from 'solid-js'
|
|
691
|
+
* import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/solid-query'
|
|
692
|
+
*
|
|
693
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
694
|
+
* queryKey: ['projects'],
|
|
695
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
696
|
+
* initialPageParam: 0,
|
|
697
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
698
|
+
* initialData: { pages: [], pageParams: [] },
|
|
699
|
+
* })
|
|
700
|
+
*
|
|
701
|
+
* function Projects() {
|
|
702
|
+
* // `projectsQuery.data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the
|
|
703
|
+
* // list stays visible alongside the error.
|
|
704
|
+
* const projectsQuery = useInfiniteQuery(() => projectsOptions)
|
|
705
|
+
*
|
|
706
|
+
* return (
|
|
707
|
+
* <div>
|
|
708
|
+
* {projectsQuery.isError ? <span>Error: {projectsQuery.error.message}</span> : null}
|
|
709
|
+
* <ul>
|
|
710
|
+
* <For each={projectsQuery.data.pages}>
|
|
711
|
+
* {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
|
|
712
|
+
* </For>
|
|
713
|
+
* </ul>
|
|
714
|
+
* </div>
|
|
715
|
+
* )
|
|
716
|
+
* }
|
|
717
|
+
* ```
|
|
718
|
+
*/
|
|
719
|
+
export declare function infiniteQueryOptions<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: ReturnType<DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>>): ReturnType<DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
|
|
720
|
+
/**
|
|
721
|
+
* You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
|
|
722
|
+
* These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
|
|
723
|
+
* `options.queryKey` is required and is the query key to generate options for.
|
|
724
|
+
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
|
|
725
|
+
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
|
|
726
|
+
* @see {@link useInfiniteQuery} to run an infinite query with these options.
|
|
727
|
+
* @example
|
|
728
|
+
* A parameterized factory, so the same options object can be reused per `postId`:
|
|
729
|
+
* ```tsx
|
|
730
|
+
* import { For, Match, Switch } from 'solid-js'
|
|
731
|
+
* import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/solid-query'
|
|
732
|
+
*
|
|
733
|
+
* const commentsOptions = (postId: string) =>
|
|
734
|
+
* infiniteQueryOptions({
|
|
735
|
+
* queryKey: ['post', postId, 'comments'],
|
|
736
|
+
* queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
|
|
737
|
+
* initialPageParam: 0,
|
|
738
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
739
|
+
* })
|
|
740
|
+
*
|
|
741
|
+
* function Comments(props: { postId: string }) {
|
|
742
|
+
* const commentsQuery = useInfiniteQuery(() => commentsOptions(props.postId))
|
|
743
|
+
*
|
|
744
|
+
* return (
|
|
745
|
+
* <Switch>
|
|
746
|
+
* <Match when={commentsQuery.isPending}>Loading...</Match>
|
|
747
|
+
* <Match when={commentsQuery.isError}>Error: {commentsQuery.error.message}</Match>
|
|
748
|
+
* <Match when={commentsQuery.isSuccess}>
|
|
749
|
+
* <ul>
|
|
750
|
+
* <For each={commentsQuery.data.pages}>
|
|
751
|
+
* {(page) => <For each={page.comments}>{(c) => <li>{c.text}</li>}</For>}
|
|
752
|
+
* </For>
|
|
753
|
+
* </ul>
|
|
754
|
+
* </Match>
|
|
755
|
+
* </Switch>
|
|
756
|
+
* )
|
|
757
|
+
* }
|
|
758
|
+
* ```
|
|
759
|
+
*/
|
|
760
|
+
export declare function infiniteQueryOptions<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: ReturnType<UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>>): ReturnType<UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>> & QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>;
|
|
761
|
+
export declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: Accessor<QueryClient>): DefinedUseInfiniteQueryResult<TData, TError>;
|
|
762
|
+
/**
|
|
763
|
+
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
764
|
+
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
765
|
+
* @param options - An accessor returning the {@link UndefinedInitialDataInfiniteOptions} to use — everything
|
|
766
|
+
* you can pass to `useInfiniteQuery`.
|
|
767
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
768
|
+
* will be used.
|
|
769
|
+
* @returns The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
770
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
|
|
771
|
+
* `data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
772
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
773
|
+
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
774
|
+
* refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
|
|
775
|
+
* actions, or add conditions like `hasNextPage && !isFetching`.
|
|
776
|
+
* @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
|
|
777
|
+
* @example
|
|
778
|
+
* Fetching the next page from a "Load More" button click:
|
|
779
|
+
* ```tsx
|
|
780
|
+
* import { For, Match, Switch } from 'solid-js'
|
|
781
|
+
* import { useInfiniteQuery } from '@tanstack/solid-query'
|
|
782
|
+
*
|
|
783
|
+
* function Projects() {
|
|
784
|
+
* const projectsQuery = useInfiniteQuery(() => ({
|
|
785
|
+
* queryKey: ['projects'],
|
|
786
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
787
|
+
* initialPageParam: 0,
|
|
788
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
789
|
+
* }))
|
|
790
|
+
*
|
|
791
|
+
* return (
|
|
792
|
+
* <Switch>
|
|
793
|
+
* <Match when={projectsQuery.isPending}>Loading...</Match>
|
|
794
|
+
* <Match when={projectsQuery.isError}>Error: {projectsQuery.error.message}</Match>
|
|
795
|
+
* <Match when={projectsQuery.isSuccess}>
|
|
796
|
+
* <ul>
|
|
797
|
+
* <For each={projectsQuery.data.pages}>
|
|
798
|
+
* {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
|
|
799
|
+
* </For>
|
|
800
|
+
* </ul>
|
|
801
|
+
* <button
|
|
802
|
+
* onClick={() => projectsQuery.fetchNextPage()}
|
|
803
|
+
* disabled={!projectsQuery.hasNextPage || projectsQuery.isFetching}
|
|
804
|
+
* >
|
|
805
|
+
* {projectsQuery.isFetchingNextPage
|
|
806
|
+
* ? 'Loading more...'
|
|
807
|
+
* : projectsQuery.hasNextPage
|
|
808
|
+
* ? 'Load More'
|
|
809
|
+
* : 'Nothing more to load'}
|
|
810
|
+
* </button>
|
|
811
|
+
* </Match>
|
|
812
|
+
* </Switch>
|
|
813
|
+
* )
|
|
814
|
+
* }
|
|
815
|
+
* ```
|
|
816
|
+
* @example
|
|
817
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
818
|
+
* sentinel element after the list:
|
|
819
|
+
* ```tsx
|
|
820
|
+
* import { For, Match, Switch, createEffect, onCleanup } from 'solid-js'
|
|
821
|
+
* import { useInfiniteQuery } from '@tanstack/solid-query'
|
|
822
|
+
*
|
|
823
|
+
* function Projects() {
|
|
824
|
+
* const projectsQuery = useInfiniteQuery(() => ({
|
|
825
|
+
* queryKey: ['projects'],
|
|
826
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
827
|
+
* initialPageParam: 0,
|
|
828
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
829
|
+
* }))
|
|
830
|
+
*
|
|
831
|
+
* let sentinelRef: HTMLDivElement | undefined
|
|
832
|
+
*
|
|
833
|
+
* createEffect(() => {
|
|
834
|
+
* if (sentinelRef == null || !projectsQuery.hasNextPage || projectsQuery.isFetching) return
|
|
835
|
+
*
|
|
836
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
837
|
+
* if (entry?.isIntersecting) projectsQuery.fetchNextPage()
|
|
838
|
+
* })
|
|
839
|
+
* observer.observe(sentinelRef)
|
|
840
|
+
*
|
|
841
|
+
* onCleanup(() => observer.disconnect())
|
|
842
|
+
* })
|
|
843
|
+
*
|
|
844
|
+
* return (
|
|
845
|
+
* <Switch>
|
|
846
|
+
* <Match when={projectsQuery.isPending}>Loading...</Match>
|
|
847
|
+
* <Match when={projectsQuery.isError}>Error: {projectsQuery.error.message}</Match>
|
|
848
|
+
* <Match when={projectsQuery.isSuccess}>
|
|
849
|
+
* <ul>
|
|
850
|
+
* <For each={projectsQuery.data.pages}>
|
|
851
|
+
* {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
|
|
852
|
+
* </For>
|
|
853
|
+
* </ul>
|
|
854
|
+
* <div ref={sentinelRef}>{projectsQuery.isFetchingNextPage ? 'Loading more...' : null}</div>
|
|
855
|
+
* </Match>
|
|
856
|
+
* </Switch>
|
|
857
|
+
* )
|
|
858
|
+
* }
|
|
859
|
+
* ```
|
|
860
|
+
*/
|
|
861
|
+
export declare function useInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: Accessor<QueryClient>): UseInfiniteQueryResult<TData, TError>;
|
|
862
|
+
/**
|
|
863
|
+
* Mutations ride core `action`: each `mutate()` call is one transaction.
|
|
864
|
+
*
|
|
865
|
+
* - Transient flight state (`isPending`, in-flight `variables`) is a
|
|
866
|
+
* `createOptimistic` overlay written at the top of the action — visible
|
|
867
|
+
* immediately, dropped automatically when the transition settles. The
|
|
868
|
+
* engine's revert-on-settle IS the submission state machine.
|
|
869
|
+
* - `options.onMutate(variables)` runs inside the same window: it is the
|
|
870
|
+
* place to apply the caller's own optimistic overlays
|
|
871
|
+
* (`createOptimistic` / `createOptimisticStore` writes). No context
|
|
872
|
+
* object, no rollback plumbing — reverting is the engine's job.
|
|
873
|
+
* - The fetch itself goes through query-core:
|
|
874
|
+
* `mutationCache.build().execute()` supplies retry/backoff, offline
|
|
875
|
+
* pausing, scoped serial execution, and cache-level lifecycle callbacks.
|
|
876
|
+
* No `MutationObserver` — the adapter reads the Mutation's state
|
|
877
|
+
* directly where flight metadata (failureCount, isPaused) is needed.
|
|
878
|
+
* - Options-level callbacks are stripped from the built mutation and
|
|
879
|
+
* re-run *post-`yield`*, inside the transaction: an
|
|
880
|
+
* `onSuccess → invalidateQueries` chain issues version bumps whose
|
|
881
|
+
* refetches hold this very transition, so mutation success and fresh
|
|
882
|
+
* query data land in one atomic paint (and a single-flight payload has
|
|
883
|
+
* already primed the cache by the time `execute` resolves).
|
|
884
|
+
* - One `mutate`, returning a safe-to-ignore promise: rejection is also
|
|
885
|
+
* routed into reactive state, and a no-op catch keeps the ignored
|
|
886
|
+
* branch from surfacing as an unhandled rejection. The
|
|
887
|
+
* `mutate`/`mutateAsync` split existed for React's callback-vs-promise
|
|
888
|
+
* error routing; it has no space here.
|
|
889
|
+
* @param options - An accessor returning the {@link UseMutationOptions} to use.
|
|
890
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
891
|
+
* will be used.
|
|
892
|
+
* @returns The mutation state, with `mutate` to run the mutation and `reset` to clear its state.
|
|
893
|
+
*/
|
|
894
|
+
export declare function useMutation<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown>(options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, queryClient?: Accessor<QueryClient>): UseMutationResult<TData, TError, TVariables, TOnMutateResult>;
|
|
895
|
+
/**
|
|
896
|
+
* You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A
|
|
897
|
+
* `mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
|
|
898
|
+
* `useMutationState`.
|
|
899
|
+
* @param options - The mutation options to use, identical to what you'd pass to `useMutation`, with a
|
|
900
|
+
* required `mutationKey`.
|
|
901
|
+
* @returns The same options object, unchanged.
|
|
902
|
+
* @see {@link useMutation} to run the mutation these options describe.
|
|
903
|
+
* @example
|
|
904
|
+
* Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
|
|
905
|
+
* ```tsx
|
|
906
|
+
* import { mutationOptions, useMutationState } from '@tanstack/solid-query'
|
|
907
|
+
*
|
|
908
|
+
* const createPostOptions = mutationOptions({
|
|
909
|
+
* mutationKey: ['posts', 'create'],
|
|
910
|
+
* mutationFn: createPost,
|
|
911
|
+
* })
|
|
912
|
+
*
|
|
913
|
+
* function SavingIndicator() {
|
|
914
|
+
* const isCreatingPost = useMutationState(() => ({
|
|
915
|
+
* filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
|
|
916
|
+
* }))
|
|
917
|
+
*
|
|
918
|
+
* return isCreatingPost().length > 0 ? <span>Saving…</span> : null
|
|
919
|
+
* }
|
|
920
|
+
* ```
|
|
921
|
+
*/
|
|
922
|
+
export declare function mutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown>(options: WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, 'mutationKey'>): WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, 'mutationKey'>;
|
|
923
|
+
/**
|
|
924
|
+
* You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No
|
|
925
|
+
* `mutationKey` is required on this overload — use this when you don't need to target the mutation via a
|
|
926
|
+
* `mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters,
|
|
927
|
+
* such as `status`.
|
|
928
|
+
* @param options - The mutation options to use, identical to what you'd pass to `useMutation`, without a
|
|
929
|
+
* `mutationKey`.
|
|
930
|
+
* @returns The same options object, unchanged.
|
|
931
|
+
* @remarks See the other overload's example for looking a mutation up via `useMutationState`.
|
|
932
|
+
* @see {@link useMutation} to run the mutation these options describe.
|
|
933
|
+
* @example
|
|
934
|
+
* ```tsx
|
|
935
|
+
* import { mutationOptions, useMutation } from '@tanstack/solid-query'
|
|
936
|
+
*
|
|
937
|
+
* const createPostOptions = mutationOptions({
|
|
938
|
+
* mutationFn: createPost,
|
|
939
|
+
* })
|
|
940
|
+
*
|
|
941
|
+
* function CreatePost() {
|
|
942
|
+
* const createPostMutation = useMutation(() => createPostOptions)
|
|
943
|
+
* return <button onClick={() => createPostMutation.mutate({ title: 'Hello' })}>Create</button>
|
|
944
|
+
* }
|
|
945
|
+
* ```
|
|
946
|
+
*/
|
|
947
|
+
export declare function mutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown>(options: Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, 'mutationKey'>): Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, 'mutationKey'>;
|
|
948
|
+
/**
|
|
949
|
+
* The `useIsMutating` primitive returns the `number` of mutations that your application currently has `pending`
|
|
950
|
+
* (useful for app-wide loading indicators).
|
|
951
|
+
* @param filters - An accessor returning the {@link MutationFilters} to narrow down the matched mutations.
|
|
952
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
953
|
+
* will be used.
|
|
954
|
+
* @returns An accessor for the `number` of the mutations that your application currently has `pending`.
|
|
955
|
+
* @example
|
|
956
|
+
* ```tsx
|
|
957
|
+
* import { useIsMutating } from '@tanstack/solid-query'
|
|
958
|
+
*
|
|
959
|
+
* function PostsMutatingIndicator() {
|
|
960
|
+
* // How many mutations matching the posts prefix are in progress?
|
|
961
|
+
* const isMutatingPosts = useIsMutating(() => ({ mutationKey: ['posts'] }))
|
|
962
|
+
*
|
|
963
|
+
* return isMutatingPosts() > 0 ? <span>Saving posts...</span> : null
|
|
964
|
+
* }
|
|
965
|
+
* ```
|
|
966
|
+
*/
|
|
967
|
+
export declare function useIsMutating(filters?: Accessor<Omit<MutationFilters, 'status'>>, queryClient?: Accessor<QueryClient>): Accessor<number>;
|
|
968
|
+
type MutationTypeFromResult<TResult> = [TResult] extends [MutationState<infer TData, infer TError, infer TVariables, infer TOnMutateResult>] ? Mutation<TData, TError, TVariables, TOnMutateResult> : Mutation;
|
|
969
|
+
type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<any, any, any, any> = MutationTypeFromResult<TResult>> = {
|
|
970
|
+
filters?: MutationFilters;
|
|
971
|
+
select?: (mutation: TMutation) => TResult;
|
|
972
|
+
};
|
|
973
|
+
/**
|
|
974
|
+
* `useMutationState` is a primitive that gives you access to all mutations in the `MutationCache`. You can pass
|
|
975
|
+
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
976
|
+
* state.
|
|
977
|
+
* @param options - An accessor returning the `filters` to narrow down matched mutations, and an optional
|
|
978
|
+
* `select` to transform the mutation state.
|
|
979
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
980
|
+
* will be used.
|
|
981
|
+
* @returns An accessor for an array of whatever `select` returns for each matching mutation.
|
|
982
|
+
* @example
|
|
983
|
+
* Get all variables of all running mutations:
|
|
984
|
+
* ```tsx
|
|
985
|
+
* import { useMutationState } from '@tanstack/solid-query'
|
|
986
|
+
*
|
|
987
|
+
* function PendingPosts() {
|
|
988
|
+
* const pendingVariables = useMutationState(() => ({
|
|
989
|
+
* filters: { status: 'pending' },
|
|
990
|
+
* select: (mutation) => mutation.state.variables,
|
|
991
|
+
* }))
|
|
992
|
+
*
|
|
993
|
+
* return <>{pendingVariables().length} posts saving...</>
|
|
994
|
+
* }
|
|
995
|
+
* ```
|
|
996
|
+
* @example
|
|
997
|
+
* Get all data for specific mutations via the `mutationKey`:
|
|
998
|
+
* ```tsx
|
|
999
|
+
* import { useMutation, useMutationState } from '@tanstack/solid-query'
|
|
1000
|
+
*
|
|
1001
|
+
* const mutationKey = ['posts']
|
|
1002
|
+
*
|
|
1003
|
+
* function Posts() {
|
|
1004
|
+
* // Some mutation that we want to get the state for
|
|
1005
|
+
* const createPostsMutation = useMutation(() => ({
|
|
1006
|
+
* mutationKey,
|
|
1007
|
+
* mutationFn: createPosts,
|
|
1008
|
+
* }))
|
|
1009
|
+
*
|
|
1010
|
+
* const savedPosts = useMutationState(() => ({
|
|
1011
|
+
* // this mutation key needs to match the mutation key of the given mutation (see above)
|
|
1012
|
+
* filters: { mutationKey, status: 'success' },
|
|
1013
|
+
* select: (mutation) => mutation.state.data,
|
|
1014
|
+
* }))
|
|
1015
|
+
*
|
|
1016
|
+
* return (
|
|
1017
|
+
* <button onClick={() => createPostsMutation.mutate(['New Post'])}>
|
|
1018
|
+
* Create post ({savedPosts().length} saved so far)
|
|
1019
|
+
* </button>
|
|
1020
|
+
* )
|
|
1021
|
+
* }
|
|
1022
|
+
* ```
|
|
1023
|
+
* @example
|
|
1024
|
+
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
1025
|
+
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
1026
|
+
* last item that `useMutationState` returns to get the latest successful invocation:
|
|
1027
|
+
* ```tsx
|
|
1028
|
+
* import { useMutationState } from '@tanstack/solid-query'
|
|
1029
|
+
*
|
|
1030
|
+
* function LatestPost() {
|
|
1031
|
+
* const savedPosts = useMutationState(() => ({
|
|
1032
|
+
* filters: { mutationKey: ['posts'], status: 'success' },
|
|
1033
|
+
* select: (mutation) => mutation.state.data,
|
|
1034
|
+
* }))
|
|
1035
|
+
*
|
|
1036
|
+
* const latestPost = () => savedPosts()[savedPosts().length - 1]
|
|
1037
|
+
*
|
|
1038
|
+
* return <span>{latestPost()?.title}</span>
|
|
1039
|
+
* }
|
|
1040
|
+
* ```
|
|
1041
|
+
*/
|
|
1042
|
+
export declare function useMutationState<TResult = MutationState, TMutation extends Mutation<any, any, any, any> = MutationTypeFromResult<TResult>>(options?: Accessor<MutationStateOptions<TResult, TMutation>>, queryClient?: Accessor<QueryClient>): Accessor<Array<TResult>>;
|
|
1043
|
+
type UseQueryOptionsForUseQueries<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = OmitKeyof<QueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'placeholderData'> & {
|
|
1044
|
+
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>;
|
|
1045
|
+
/**
|
|
1046
|
+
* @deprecated The `suspense` option has been removed. Suspense is the
|
|
1047
|
+
* model: each result's `data` is an async read that suspends into the
|
|
1048
|
+
* nearest `<Loading>` boundary while its first fetch is in flight, same
|
|
1049
|
+
* as `useQuery`. Setting `suspense` is a no-op.
|
|
1050
|
+
*/
|
|
1051
|
+
suspense?: boolean;
|
|
1052
|
+
};
|
|
1053
|
+
type MAXIMUM_DEPTH = 20;
|
|
1054
|
+
type SkipTokenForUseQueries = symbol;
|
|
1055
|
+
type GetOptions<T> = T extends {
|
|
1056
|
+
queryFnData: infer TQueryFnData;
|
|
1057
|
+
error?: infer TError;
|
|
1058
|
+
data: infer TData;
|
|
1059
|
+
} ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData> : T extends {
|
|
1060
|
+
queryFnData: infer TQueryFnData;
|
|
1061
|
+
error?: infer TError;
|
|
1062
|
+
} ? UseQueryOptionsForUseQueries<TQueryFnData, TError> : T extends {
|
|
1063
|
+
data: infer TData;
|
|
1064
|
+
error?: infer TError;
|
|
1065
|
+
} ? UseQueryOptionsForUseQueries<unknown, TError, TData> : T extends [infer TQueryFnData, infer TError, infer TData] ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData> : T extends [infer TQueryFnData, infer TError] ? UseQueryOptionsForUseQueries<TQueryFnData, TError> : T extends [infer TQueryFnData] ? UseQueryOptionsForUseQueries<TQueryFnData> : T extends {
|
|
1066
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipTokenForUseQueries;
|
|
1067
|
+
select?: (data: any) => infer TData;
|
|
1068
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
1069
|
+
} ? UseQueryOptionsForUseQueries<TQueryFnData, unknown extends TError ? DefaultError : TError, unknown extends TData ? TQueryFnData : TData, TQueryKey> : UseQueryOptionsForUseQueries;
|
|
1070
|
+
type GetResults<T> = T extends {
|
|
1071
|
+
queryFnData: any;
|
|
1072
|
+
error?: infer TError;
|
|
1073
|
+
data: infer TData;
|
|
1074
|
+
} ? UseQueryResult<TData, TError> : T extends {
|
|
1075
|
+
queryFnData: infer TQueryFnData;
|
|
1076
|
+
error?: infer TError;
|
|
1077
|
+
} ? UseQueryResult<TQueryFnData, TError> : T extends {
|
|
1078
|
+
data: infer TData;
|
|
1079
|
+
error?: infer TError;
|
|
1080
|
+
} ? UseQueryResult<TData, TError> : T extends [any, infer TError, infer TData] ? UseQueryResult<TData, TError> : T extends [infer TQueryFnData, infer TError] ? UseQueryResult<TQueryFnData, TError> : T extends [infer TQueryFnData] ? UseQueryResult<TQueryFnData> : T extends {
|
|
1081
|
+
queryFn?: QueryFunction<infer TQueryFnData, any> | SkipTokenForUseQueries;
|
|
1082
|
+
select?: (data: any) => infer TData;
|
|
1083
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
1084
|
+
} ? UseQueryResult<unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError> : UseQueryResult;
|
|
1085
|
+
/**
|
|
1086
|
+
* QueriesOptions reducer recursively snapshots function arguments to infer/enforce type param
|
|
1087
|
+
*/
|
|
1088
|
+
type QueriesOptions<T extends Array<any>, TResult extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<UseQueryOptionsForUseQueries> : T extends [] ? [] : T extends [infer Head] ? [...TResult, GetOptions<Head>] : T extends [infer Head, ...infer Tail] ? QueriesOptions<[...Tail], [...TResult, GetOptions<Head>], [...TDepth, 1]> : ReadonlyArray<unknown> extends T ? T : T extends Array<UseQueryOptionsForUseQueries<infer TQueryFnData, infer TError, infer TData, infer TQueryKey>> ? Array<UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>> : Array<UseQueryOptionsForUseQueries>;
|
|
1089
|
+
/**
|
|
1090
|
+
* QueriesResults reducer recursively maps type param to results
|
|
1091
|
+
*/
|
|
1092
|
+
type QueriesResults<T extends Array<any>, TResult extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<UseQueryResult> : T extends [] ? [] : T extends [infer Head] ? [...TResult, GetResults<Head>] : T extends [infer Head, ...infer Tail] ? QueriesResults<[...Tail], [...TResult, GetResults<Head>], [...TDepth, 1]> : { [K in keyof T]: GetResults<T[K]>; };
|
|
1093
|
+
/**
|
|
1094
|
+
* The `useQueries` primitive can be used to fetch a variable number of queries.
|
|
1095
|
+
*
|
|
1096
|
+
* The `queries` key accepts an array with query option objects mostly identical to `useQuery` — see
|
|
1097
|
+
* `placeholderData` below for the one difference. A custom `QueryClient` is supplied once, as `useQueries`'
|
|
1098
|
+
* own top-level second argument, rather than per query.
|
|
1099
|
+
*
|
|
1100
|
+
* Having the same query key more than once in the array of query objects may cause some data to be shared
|
|
1101
|
+
* between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
|
|
1102
|
+
* structure.
|
|
1103
|
+
*
|
|
1104
|
+
* The `combine` option can be used to combine the results of the queries into a single value. The result will
|
|
1105
|
+
* be structurally shared to be as referentially stable as possible.
|
|
1106
|
+
*
|
|
1107
|
+
* `placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from
|
|
1108
|
+
* previously rendered queries, because the number of queries can differ between renders.
|
|
1109
|
+
* @param queriesOptions - An accessor returning the `queries` array to run, and an optional `combine`
|
|
1110
|
+
* function.
|
|
1111
|
+
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
1112
|
+
* will be used.
|
|
1113
|
+
* @returns The combined result. Without `combine`, this is an array with all the query results, in the same
|
|
1114
|
+
* order as the input. When `combine` is provided, this is the value returned by `combine` instead.
|
|
1115
|
+
* @example
|
|
1116
|
+
* ```tsx
|
|
1117
|
+
* import { For } from 'solid-js'
|
|
1118
|
+
* import { useQueries } from '@tanstack/solid-query'
|
|
1119
|
+
*
|
|
1120
|
+
* function Posts(props: { ids: Array<number> }) {
|
|
1121
|
+
* const postQueries = useQueries(() => ({
|
|
1122
|
+
* queries: props.ids.map((id) => ({
|
|
1123
|
+
* queryKey: ['post', id],
|
|
1124
|
+
* queryFn: () => fetchPost(id),
|
|
1125
|
+
* staleTime: Infinity,
|
|
1126
|
+
* })),
|
|
1127
|
+
* }))
|
|
1128
|
+
*
|
|
1129
|
+
* return (
|
|
1130
|
+
* <ul>
|
|
1131
|
+
* <For each={postQueries}>
|
|
1132
|
+
* {(postQuery) => {
|
|
1133
|
+
* if (postQuery.isPending) return <li>Loading...</li>
|
|
1134
|
+
* if (postQuery.isError) return <li>Error: {postQuery.error.message}</li>
|
|
1135
|
+
* return <li>{postQuery.data.title}</li>
|
|
1136
|
+
* }}
|
|
1137
|
+
* </For>
|
|
1138
|
+
* </ul>
|
|
1139
|
+
* )
|
|
1140
|
+
* }
|
|
1141
|
+
* ```
|
|
1142
|
+
* @example
|
|
1143
|
+
* Combining results into a single value:
|
|
1144
|
+
* ```tsx
|
|
1145
|
+
* import { For, Match, Switch } from 'solid-js'
|
|
1146
|
+
* import { useQueries } from '@tanstack/solid-query'
|
|
1147
|
+
*
|
|
1148
|
+
* function Posts(props: { ids: Array<number> }) {
|
|
1149
|
+
* const combinedPostsQuery = useQueries(() => ({
|
|
1150
|
+
* queries: props.ids.map((id) => ({
|
|
1151
|
+
* queryKey: ['post', id],
|
|
1152
|
+
* queryFn: () => fetchPost(id),
|
|
1153
|
+
* })),
|
|
1154
|
+
* combine: (postQueries) => {
|
|
1155
|
+
* return {
|
|
1156
|
+
* data: postQueries.map((postQuery) => postQuery.data),
|
|
1157
|
+
* isPending: postQueries.some((postQuery) => postQuery.isPending),
|
|
1158
|
+
* isError: postQueries.some((postQuery) => postQuery.isError),
|
|
1159
|
+
* }
|
|
1160
|
+
* },
|
|
1161
|
+
* }))
|
|
1162
|
+
*
|
|
1163
|
+
* return (
|
|
1164
|
+
* <Switch
|
|
1165
|
+
* fallback={
|
|
1166
|
+
* <ul>
|
|
1167
|
+
* <For each={combinedPostsQuery.data}>{(post) => <li>{post?.title}</li>}</For>
|
|
1168
|
+
* </ul>
|
|
1169
|
+
* }
|
|
1170
|
+
* >
|
|
1171
|
+
* <Match when={combinedPostsQuery.isPending}>Loading...</Match>
|
|
1172
|
+
* <Match when={combinedPostsQuery.isError}>Error loading posts</Match>
|
|
1173
|
+
* </Switch>
|
|
1174
|
+
* )
|
|
1175
|
+
* }
|
|
1176
|
+
* ```
|
|
1177
|
+
*/
|
|
1178
|
+
export declare function useQueries<T extends Array<any>, TCombinedResult = QueriesResults<T>>(queriesOptions: Accessor<{
|
|
1179
|
+
queries: readonly [...QueriesOptions<T>] | readonly [...{ [K in keyof T]: GetOptions<T[K]>; }];
|
|
1180
|
+
combine?: (result: QueriesResults<T>) => TCombinedResult;
|
|
1181
|
+
}>, queryClient?: Accessor<QueryClient>): TCombinedResult;
|
|
1182
|
+
export declare const IsRestoringContext: import("solid-js").Context<Accessor<boolean>>;
|
|
1183
|
+
/**
|
|
1184
|
+
* If you are using `PersistQueryClientProvider`, you can also use the `useIsRestoring` primitive alongside it to
|
|
1185
|
+
* check if a restore is currently in progress. `useQuery` and friends also check this internally to avoid
|
|
1186
|
+
* race conditions between the restore and mounting queries.
|
|
1187
|
+
* @returns An accessor that reads `true` while a persisted client is being restored, `false` otherwise.
|
|
1188
|
+
*/
|
|
1189
|
+
export declare const useIsRestoring: () => Accessor<boolean>;
|
|
1190
|
+
export type { DefaultOptions, DefinedInitialDataInfiniteOptions, DefinedInitialDataOptions, DefinedUseBaseQueryResult, DefinedUseInfiniteQueryResult, DefinedUseQueryResult, InfiniteQueryObserverOptions, InfiniteQueryOptions, MutationOptions, QueryClientConfig, QueryClientProviderProps, QueryObserverOptions, QueryOptions, UndefinedInitialDataInfiniteOptions, UndefinedInitialDataOptions, UseBaseMutationResult, UseBaseQueryOptions, UseBaseQueryResult, UseInfiniteQueryOptions, UseInfiniteQueryResult, UseMutateFunction, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult };
|
|
1191
|
+
//# sourceMappingURL=index.d.cts.map
|