@tanstack/solid-query 5.104.1 → 5.104.2
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/build/dev.cjs +49 -21
- package/build/dev.js +49 -21
- package/build/index.d.cts +37 -88
- package/build/index.d.cts.map +1 -1
- package/build/index.d.ts +37 -88
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/package.json +5 -4
- package/src/QueryClient.ts +13 -5
- package/src/QueryClientProvider.tsx +9 -4
- package/src/infiniteQueryOptions.ts +2 -8
- package/src/isRestoring.ts +0 -1
- package/src/mutationOptions.ts +2 -6
- package/src/queryOptions.ts +2 -8
- package/src/types.ts +11 -18
- package/src/useBaseQuery.ts +36 -2
- package/src/useInfiniteQuery.ts +8 -13
- package/src/useIsFetching.ts +0 -2
- package/src/useIsMutating.ts +0 -2
- package/src/useMutation.ts +0 -6
- package/src/useMutationState.ts +7 -4
- package/src/useQueries.ts +0 -2
- package/src/useQuery.ts +2 -11
package/build/dev.cjs
CHANGED
|
@@ -11,11 +11,11 @@ const QueryClientContext = (0, solid_js.createContext)(void 0);
|
|
|
11
11
|
const queryClientContextError = "No QueryClient set, use QueryClientProvider to set one";
|
|
12
12
|
/**
|
|
13
13
|
* The `useQueryClient` primitive returns the current `QueryClient` instance.
|
|
14
|
-
*
|
|
15
14
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
16
15
|
* be used.
|
|
17
16
|
* @returns The current `QueryClient` instance.
|
|
18
|
-
* @throws If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
|
|
17
|
+
* @throws {Error} If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
|
|
18
|
+
* component tree.
|
|
19
19
|
*/
|
|
20
20
|
const useQueryClient = (queryClient) => {
|
|
21
21
|
if (queryClient) return queryClient;
|
|
@@ -23,6 +23,12 @@ const useQueryClient = (queryClient) => {
|
|
|
23
23
|
if (!client) throw new Error(queryClientContextError);
|
|
24
24
|
return client();
|
|
25
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Resolves the `QueryClient` to use: the given one, or else the one from the nearest
|
|
28
|
+
* `QueryClientProvider`.
|
|
29
|
+
* @param queryClient - Returns a custom `QueryClient`, if any.
|
|
30
|
+
* @returns An accessor that returns the resolved client. It throws if neither client is available.
|
|
31
|
+
*/
|
|
26
32
|
const useQueryClientResolver = (queryClient) => {
|
|
27
33
|
const contextClient = (0, solid_js.useContext)(QueryClientContext);
|
|
28
34
|
return () => {
|
|
@@ -37,9 +43,8 @@ const useQueryClientResolver = (queryClient) => {
|
|
|
37
43
|
* calls `client.mount()`/`client.unmount()` as this component mounts/unmounts, which subscribes the client to
|
|
38
44
|
* focus/online events (resuming any paused mutations and refetching as needed when the app regains focus or
|
|
39
45
|
* comes back online).
|
|
40
|
-
*
|
|
46
|
+
* @param props - The `client` to provide, and the `children` that get access to it.
|
|
41
47
|
* @returns The provided `children`, wrapped so they can read the `QueryClient` via `useQueryClient`.
|
|
42
|
-
*
|
|
43
48
|
* @example
|
|
44
49
|
* ```tsx
|
|
45
50
|
* import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
|
|
@@ -72,7 +77,6 @@ const IsRestoringContext = (0, solid_js.createContext)(() => false);
|
|
|
72
77
|
* If you are using `PersistQueryClientProvider`, you can also use the `useIsRestoring` primitive alongside it to
|
|
73
78
|
* check if a restore is currently in progress. `useQuery` and friends also check this internally to avoid
|
|
74
79
|
* race conditions between the restore and mounting queries.
|
|
75
|
-
*
|
|
76
80
|
* @returns An accessor that reads `true` while a persisted client is being restored, `false` otherwise.
|
|
77
81
|
*/
|
|
78
82
|
const useIsRestoring = () => (0, solid_js.useContext)(IsRestoringContext);
|
|
@@ -83,6 +87,15 @@ const useIsRestoring = () => (0, solid_js.useContext)(IsRestoringContext);
|
|
|
83
87
|
const IsRestoringProvider = IsRestoringContext.Provider;
|
|
84
88
|
//#endregion
|
|
85
89
|
//#region src/useBaseQuery.ts
|
|
90
|
+
/**
|
|
91
|
+
* Applies the `reconcile` option when writing a new observer result into the store.
|
|
92
|
+
* @param store - The current result in the store.
|
|
93
|
+
* @param result - The new observer result.
|
|
94
|
+
* @param reconcileOption - `false` to replace the data, a function that merges the old and new
|
|
95
|
+
* data, or the key used by Solid's `reconcile` to match array items.
|
|
96
|
+
* @param queryHash - The query hash, used in the warning if the data can't be cloned.
|
|
97
|
+
* @returns The result to store, with its data reconciled.
|
|
98
|
+
*/
|
|
86
99
|
function reconcileFn(store, result, reconcileOption, queryHash) {
|
|
87
100
|
if (reconcileOption === false) return result;
|
|
88
101
|
if (typeof reconcileOption === "function") {
|
|
@@ -107,6 +120,10 @@ function reconcileFn(store, result, reconcileOption, queryHash) {
|
|
|
107
120
|
/**
|
|
108
121
|
* Solid's `onHydrated` functionality will silently "fail" (hydrate with an empty object)
|
|
109
122
|
* if the resource data is not serializable.
|
|
123
|
+
* @param query - The query the result belongs to.
|
|
124
|
+
* @param result - The observer result to make serializable.
|
|
125
|
+
* @returns On the server, a copy of the result without its functions and with the query's
|
|
126
|
+
* dehydrated state attached as `hydrationData`. On the client, the result as is.
|
|
110
127
|
*/
|
|
111
128
|
const hydratableObserverResult = (query, result) => {
|
|
112
129
|
if (!solid_js_web.isServer) return result;
|
|
@@ -126,6 +143,15 @@ const hydratableObserverResult = (query, result) => {
|
|
|
126
143
|
};
|
|
127
144
|
return obj;
|
|
128
145
|
};
|
|
146
|
+
/**
|
|
147
|
+
* Base Query Function that is used to create the query.
|
|
148
|
+
* @param options - A function that returns the query options.
|
|
149
|
+
* @param Observer - The observer class from query-core (`QueryObserver` or
|
|
150
|
+
* `InfiniteQueryObserver`).
|
|
151
|
+
* @param queryClient - A function that returns a custom `QueryClient`. Otherwise, the one from the
|
|
152
|
+
* nearest context is used.
|
|
153
|
+
* @returns The reactive query result.
|
|
154
|
+
*/
|
|
129
155
|
function useBaseQuery(options, Observer, queryClient) {
|
|
130
156
|
const resolveClient = useQueryClientResolver(queryClient);
|
|
131
157
|
const client = (0, solid_js.createMemo)(() => resolveClient());
|
|
@@ -174,6 +200,10 @@ function useBaseQuery(options, Observer, queryClient) {
|
|
|
174
200
|
});
|
|
175
201
|
});
|
|
176
202
|
};
|
|
203
|
+
/**
|
|
204
|
+
* Writes an observer result into the store, applying the query's `reconcile` option.
|
|
205
|
+
* @param res - The observer result to write.
|
|
206
|
+
*/
|
|
177
207
|
function setStateWithReconciliation(res) {
|
|
178
208
|
const opts = observer().options;
|
|
179
209
|
const reconcileOptions = opts.reconcile;
|
|
@@ -181,6 +211,11 @@ function useBaseQuery(options, Observer, queryClient) {
|
|
|
181
211
|
return reconcileFn(store, res, reconcileOptions === void 0 ? false : reconcileOptions, opts.queryHash);
|
|
182
212
|
});
|
|
183
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Creates the signal used as the resource's storage, backed by the query store. Hydration data is
|
|
216
|
+
* removed from values written to it.
|
|
217
|
+
* @returns A getter and setter for the store.
|
|
218
|
+
*/
|
|
184
219
|
function createDeepSignal() {
|
|
185
220
|
return [() => state, (v) => {
|
|
186
221
|
const unwrapped = (0, solid_js_store.unwrap)(state);
|
|
@@ -226,6 +261,8 @@ function useBaseQuery(options, Observer, queryClient) {
|
|
|
226
261
|
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
227
262
|
*
|
|
228
263
|
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
264
|
+
* @param _k - The resource source, unused.
|
|
265
|
+
* @param info - The hydrated resource value, which carries the query's `hydrationData`.
|
|
229
266
|
*/
|
|
230
267
|
onHydrated(_k, info) {
|
|
231
268
|
if (info.value && "hydrationData" in info.value) (0, _tanstack_query_core.hydrate)(client(), { queries: [{ ...info.value.hydrationData }] });
|
|
@@ -292,7 +329,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
292
329
|
/**
|
|
293
330
|
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
294
331
|
* `useMutation` is the primitive for that.
|
|
295
|
-
*
|
|
296
332
|
* @param options - An accessor returning the {@link UseMutationOptions} to use.
|
|
297
333
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
298
334
|
* will be used.
|
|
@@ -301,7 +337,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
301
337
|
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
302
338
|
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
303
339
|
* the mutation settles removes the subscription and prevents them from firing.
|
|
304
|
-
*
|
|
305
340
|
* @example
|
|
306
341
|
* ```tsx
|
|
307
342
|
* import { useMutation, useQueryClient } from '@tanstack/solid-query'
|
|
@@ -323,7 +358,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
323
358
|
* )
|
|
324
359
|
* }
|
|
325
360
|
* ```
|
|
326
|
-
*
|
|
327
361
|
* @example
|
|
328
362
|
* Rendering the mutation's own state, rather than just firing it off:
|
|
329
363
|
* ```tsx
|
|
@@ -349,7 +383,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
349
383
|
* )
|
|
350
384
|
* }
|
|
351
385
|
* ```
|
|
352
|
-
*
|
|
353
386
|
* @example
|
|
354
387
|
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
355
388
|
* ```tsx
|
|
@@ -385,7 +418,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
385
418
|
* )
|
|
386
419
|
* }
|
|
387
420
|
* ```
|
|
388
|
-
*
|
|
389
421
|
* @example
|
|
390
422
|
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
391
423
|
* promise per call instead, so you can wait for all of them when they succeed:
|
|
@@ -415,7 +447,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
415
447
|
* )
|
|
416
448
|
* }
|
|
417
449
|
* ```
|
|
418
|
-
*
|
|
419
450
|
* @example
|
|
420
451
|
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
421
452
|
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
@@ -503,7 +534,6 @@ function useMutation(options, queryClient) {
|
|
|
503
534
|
* will be used.
|
|
504
535
|
* @returns The combined result. Without `combine`, this is an array with all the query results, in the same
|
|
505
536
|
* order as the input. When `combine` is provided, this is the value returned by `combine` instead.
|
|
506
|
-
*
|
|
507
537
|
* @example
|
|
508
538
|
* ```tsx
|
|
509
539
|
* import { For } from 'solid-js'
|
|
@@ -531,7 +561,6 @@ function useMutation(options, queryClient) {
|
|
|
531
561
|
* )
|
|
532
562
|
* }
|
|
533
563
|
* ```
|
|
534
|
-
*
|
|
535
564
|
* @example
|
|
536
565
|
* Combining results into a single value:
|
|
537
566
|
* ```tsx
|
|
@@ -658,13 +687,11 @@ function queryOptions(options) {
|
|
|
658
687
|
/**
|
|
659
688
|
* The `useIsFetching` primitive returns the `number` of the queries that your application is loading or fetching
|
|
660
689
|
* in the background (useful for app-wide loading indicators).
|
|
661
|
-
*
|
|
662
690
|
* @param filters - An accessor returning the {@link QueryFilters} to narrow down the matched queries.
|
|
663
691
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
664
692
|
* will be used.
|
|
665
693
|
* @returns An accessor for the `number` of the queries that your application is currently loading or fetching
|
|
666
694
|
* in the background.
|
|
667
|
-
*
|
|
668
695
|
* @example
|
|
669
696
|
* ```tsx
|
|
670
697
|
* import { useIsFetching } from '@tanstack/solid-query'
|
|
@@ -706,12 +733,10 @@ function mutationOptions(options) {
|
|
|
706
733
|
/**
|
|
707
734
|
* The `useIsMutating` primitive returns the `number` of mutations that your application currently has `pending`
|
|
708
735
|
* (useful for app-wide loading indicators).
|
|
709
|
-
*
|
|
710
736
|
* @param filters - An accessor returning the {@link MutationFilters} to narrow down the matched mutations.
|
|
711
737
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
712
738
|
* will be used.
|
|
713
739
|
* @returns An accessor for the `number` of the mutations that your application currently has `pending`.
|
|
714
|
-
*
|
|
715
740
|
* @example
|
|
716
741
|
* ```tsx
|
|
717
742
|
* import { useIsMutating } from '@tanstack/solid-query'
|
|
@@ -740,6 +765,13 @@ function useIsMutating(filters, queryClient) {
|
|
|
740
765
|
}
|
|
741
766
|
//#endregion
|
|
742
767
|
//#region src/useMutationState.ts
|
|
768
|
+
/**
|
|
769
|
+
* Collects the mutations in the cache that match `options.filters`, mapped with `options.select`
|
|
770
|
+
* (or to their state, by default).
|
|
771
|
+
* @param mutationCache - The mutation cache to read.
|
|
772
|
+
* @param options - The `filters` to match and the `select` function to map each mutation with.
|
|
773
|
+
* @returns The selected value of every matching mutation.
|
|
774
|
+
*/
|
|
743
775
|
function getResult(mutationCache, options) {
|
|
744
776
|
return mutationCache.findAll(options.filters).map((mutation) => options.select ? options.select(mutation) : mutation.state);
|
|
745
777
|
}
|
|
@@ -747,13 +779,11 @@ function getResult(mutationCache, options) {
|
|
|
747
779
|
* `useMutationState` is a primitive that gives you access to all mutations in the `MutationCache`. You can pass
|
|
748
780
|
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
749
781
|
* state.
|
|
750
|
-
*
|
|
751
782
|
* @param options - An accessor returning the `filters` to narrow down matched mutations, and an optional
|
|
752
783
|
* `select` to transform the mutation state.
|
|
753
784
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
754
785
|
* will be used.
|
|
755
786
|
* @returns An accessor for an array of whatever `select` returns for each matching mutation.
|
|
756
|
-
*
|
|
757
787
|
* @example
|
|
758
788
|
* Get all variables of all running mutations:
|
|
759
789
|
* ```tsx
|
|
@@ -768,7 +798,6 @@ function getResult(mutationCache, options) {
|
|
|
768
798
|
* return <>{pendingVariables().length} posts saving...</>
|
|
769
799
|
* }
|
|
770
800
|
* ```
|
|
771
|
-
*
|
|
772
801
|
* @example
|
|
773
802
|
* Get all data for specific mutations via the `mutationKey`:
|
|
774
803
|
* ```tsx
|
|
@@ -796,7 +825,6 @@ function getResult(mutationCache, options) {
|
|
|
796
825
|
* )
|
|
797
826
|
* }
|
|
798
827
|
* ```
|
|
799
|
-
*
|
|
800
828
|
* @example
|
|
801
829
|
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
802
830
|
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
package/build/dev.js
CHANGED
|
@@ -11,11 +11,11 @@ const QueryClientContext = createContext(void 0);
|
|
|
11
11
|
const queryClientContextError = "No QueryClient set, use QueryClientProvider to set one";
|
|
12
12
|
/**
|
|
13
13
|
* The `useQueryClient` primitive returns the current `QueryClient` instance.
|
|
14
|
-
*
|
|
15
14
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
16
15
|
* be used.
|
|
17
16
|
* @returns The current `QueryClient` instance.
|
|
18
|
-
* @throws If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
|
|
17
|
+
* @throws {Error} If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
|
|
18
|
+
* component tree.
|
|
19
19
|
*/
|
|
20
20
|
const useQueryClient = (queryClient) => {
|
|
21
21
|
if (queryClient) return queryClient;
|
|
@@ -23,6 +23,12 @@ const useQueryClient = (queryClient) => {
|
|
|
23
23
|
if (!client) throw new Error(queryClientContextError);
|
|
24
24
|
return client();
|
|
25
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Resolves the `QueryClient` to use: the given one, or else the one from the nearest
|
|
28
|
+
* `QueryClientProvider`.
|
|
29
|
+
* @param queryClient - Returns a custom `QueryClient`, if any.
|
|
30
|
+
* @returns An accessor that returns the resolved client. It throws if neither client is available.
|
|
31
|
+
*/
|
|
26
32
|
const useQueryClientResolver = (queryClient) => {
|
|
27
33
|
const contextClient = useContext(QueryClientContext);
|
|
28
34
|
return () => {
|
|
@@ -37,9 +43,8 @@ const useQueryClientResolver = (queryClient) => {
|
|
|
37
43
|
* calls `client.mount()`/`client.unmount()` as this component mounts/unmounts, which subscribes the client to
|
|
38
44
|
* focus/online events (resuming any paused mutations and refetching as needed when the app regains focus or
|
|
39
45
|
* comes back online).
|
|
40
|
-
*
|
|
46
|
+
* @param props - The `client` to provide, and the `children` that get access to it.
|
|
41
47
|
* @returns The provided `children`, wrapped so they can read the `QueryClient` via `useQueryClient`.
|
|
42
|
-
*
|
|
43
48
|
* @example
|
|
44
49
|
* ```tsx
|
|
45
50
|
* import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
|
|
@@ -72,7 +77,6 @@ const IsRestoringContext = createContext(() => false);
|
|
|
72
77
|
* If you are using `PersistQueryClientProvider`, you can also use the `useIsRestoring` primitive alongside it to
|
|
73
78
|
* check if a restore is currently in progress. `useQuery` and friends also check this internally to avoid
|
|
74
79
|
* race conditions between the restore and mounting queries.
|
|
75
|
-
*
|
|
76
80
|
* @returns An accessor that reads `true` while a persisted client is being restored, `false` otherwise.
|
|
77
81
|
*/
|
|
78
82
|
const useIsRestoring = () => useContext(IsRestoringContext);
|
|
@@ -83,6 +87,15 @@ const useIsRestoring = () => useContext(IsRestoringContext);
|
|
|
83
87
|
const IsRestoringProvider = IsRestoringContext.Provider;
|
|
84
88
|
//#endregion
|
|
85
89
|
//#region src/useBaseQuery.ts
|
|
90
|
+
/**
|
|
91
|
+
* Applies the `reconcile` option when writing a new observer result into the store.
|
|
92
|
+
* @param store - The current result in the store.
|
|
93
|
+
* @param result - The new observer result.
|
|
94
|
+
* @param reconcileOption - `false` to replace the data, a function that merges the old and new
|
|
95
|
+
* data, or the key used by Solid's `reconcile` to match array items.
|
|
96
|
+
* @param queryHash - The query hash, used in the warning if the data can't be cloned.
|
|
97
|
+
* @returns The result to store, with its data reconciled.
|
|
98
|
+
*/
|
|
86
99
|
function reconcileFn(store, result, reconcileOption, queryHash) {
|
|
87
100
|
if (reconcileOption === false) return result;
|
|
88
101
|
if (typeof reconcileOption === "function") {
|
|
@@ -107,6 +120,10 @@ function reconcileFn(store, result, reconcileOption, queryHash) {
|
|
|
107
120
|
/**
|
|
108
121
|
* Solid's `onHydrated` functionality will silently "fail" (hydrate with an empty object)
|
|
109
122
|
* if the resource data is not serializable.
|
|
123
|
+
* @param query - The query the result belongs to.
|
|
124
|
+
* @param result - The observer result to make serializable.
|
|
125
|
+
* @returns On the server, a copy of the result without its functions and with the query's
|
|
126
|
+
* dehydrated state attached as `hydrationData`. On the client, the result as is.
|
|
110
127
|
*/
|
|
111
128
|
const hydratableObserverResult = (query, result) => {
|
|
112
129
|
if (!isServer) return result;
|
|
@@ -126,6 +143,15 @@ const hydratableObserverResult = (query, result) => {
|
|
|
126
143
|
};
|
|
127
144
|
return obj;
|
|
128
145
|
};
|
|
146
|
+
/**
|
|
147
|
+
* Base Query Function that is used to create the query.
|
|
148
|
+
* @param options - A function that returns the query options.
|
|
149
|
+
* @param Observer - The observer class from query-core (`QueryObserver` or
|
|
150
|
+
* `InfiniteQueryObserver`).
|
|
151
|
+
* @param queryClient - A function that returns a custom `QueryClient`. Otherwise, the one from the
|
|
152
|
+
* nearest context is used.
|
|
153
|
+
* @returns The reactive query result.
|
|
154
|
+
*/
|
|
129
155
|
function useBaseQuery(options, Observer, queryClient) {
|
|
130
156
|
const resolveClient = useQueryClientResolver(queryClient);
|
|
131
157
|
const client = createMemo(() => resolveClient());
|
|
@@ -174,6 +200,10 @@ function useBaseQuery(options, Observer, queryClient) {
|
|
|
174
200
|
});
|
|
175
201
|
});
|
|
176
202
|
};
|
|
203
|
+
/**
|
|
204
|
+
* Writes an observer result into the store, applying the query's `reconcile` option.
|
|
205
|
+
* @param res - The observer result to write.
|
|
206
|
+
*/
|
|
177
207
|
function setStateWithReconciliation(res) {
|
|
178
208
|
const opts = observer().options;
|
|
179
209
|
const reconcileOptions = opts.reconcile;
|
|
@@ -181,6 +211,11 @@ function useBaseQuery(options, Observer, queryClient) {
|
|
|
181
211
|
return reconcileFn(store, res, reconcileOptions === void 0 ? false : reconcileOptions, opts.queryHash);
|
|
182
212
|
});
|
|
183
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Creates the signal used as the resource's storage, backed by the query store. Hydration data is
|
|
216
|
+
* removed from values written to it.
|
|
217
|
+
* @returns A getter and setter for the store.
|
|
218
|
+
*/
|
|
184
219
|
function createDeepSignal() {
|
|
185
220
|
return [() => state, (v) => {
|
|
186
221
|
const unwrapped = unwrap(state);
|
|
@@ -226,6 +261,8 @@ function useBaseQuery(options, Observer, queryClient) {
|
|
|
226
261
|
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
227
262
|
*
|
|
228
263
|
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
264
|
+
* @param _k - The resource source, unused.
|
|
265
|
+
* @param info - The hydrated resource value, which carries the query's `hydrationData`.
|
|
229
266
|
*/
|
|
230
267
|
onHydrated(_k, info) {
|
|
231
268
|
if (info.value && "hydrationData" in info.value) hydrate(client(), { queries: [{ ...info.value.hydrationData }] });
|
|
@@ -292,7 +329,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
292
329
|
/**
|
|
293
330
|
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
294
331
|
* `useMutation` is the primitive for that.
|
|
295
|
-
*
|
|
296
332
|
* @param options - An accessor returning the {@link UseMutationOptions} to use.
|
|
297
333
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
298
334
|
* will be used.
|
|
@@ -301,7 +337,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
301
337
|
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
302
338
|
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
303
339
|
* the mutation settles removes the subscription and prevents them from firing.
|
|
304
|
-
*
|
|
305
340
|
* @example
|
|
306
341
|
* ```tsx
|
|
307
342
|
* import { useMutation, useQueryClient } from '@tanstack/solid-query'
|
|
@@ -323,7 +358,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
323
358
|
* )
|
|
324
359
|
* }
|
|
325
360
|
* ```
|
|
326
|
-
*
|
|
327
361
|
* @example
|
|
328
362
|
* Rendering the mutation's own state, rather than just firing it off:
|
|
329
363
|
* ```tsx
|
|
@@ -349,7 +383,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
349
383
|
* )
|
|
350
384
|
* }
|
|
351
385
|
* ```
|
|
352
|
-
*
|
|
353
386
|
* @example
|
|
354
387
|
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
355
388
|
* ```tsx
|
|
@@ -385,7 +418,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
385
418
|
* )
|
|
386
419
|
* }
|
|
387
420
|
* ```
|
|
388
|
-
*
|
|
389
421
|
* @example
|
|
390
422
|
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
391
423
|
* promise per call instead, so you can wait for all of them when they succeed:
|
|
@@ -415,7 +447,6 @@ function useInfiniteQuery(options, queryClient) {
|
|
|
415
447
|
* )
|
|
416
448
|
* }
|
|
417
449
|
* ```
|
|
418
|
-
*
|
|
419
450
|
* @example
|
|
420
451
|
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
421
452
|
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
@@ -503,7 +534,6 @@ function useMutation(options, queryClient) {
|
|
|
503
534
|
* will be used.
|
|
504
535
|
* @returns The combined result. Without `combine`, this is an array with all the query results, in the same
|
|
505
536
|
* order as the input. When `combine` is provided, this is the value returned by `combine` instead.
|
|
506
|
-
*
|
|
507
537
|
* @example
|
|
508
538
|
* ```tsx
|
|
509
539
|
* import { For } from 'solid-js'
|
|
@@ -531,7 +561,6 @@ function useMutation(options, queryClient) {
|
|
|
531
561
|
* )
|
|
532
562
|
* }
|
|
533
563
|
* ```
|
|
534
|
-
*
|
|
535
564
|
* @example
|
|
536
565
|
* Combining results into a single value:
|
|
537
566
|
* ```tsx
|
|
@@ -658,13 +687,11 @@ function queryOptions(options) {
|
|
|
658
687
|
/**
|
|
659
688
|
* The `useIsFetching` primitive returns the `number` of the queries that your application is loading or fetching
|
|
660
689
|
* in the background (useful for app-wide loading indicators).
|
|
661
|
-
*
|
|
662
690
|
* @param filters - An accessor returning the {@link QueryFilters} to narrow down the matched queries.
|
|
663
691
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
664
692
|
* will be used.
|
|
665
693
|
* @returns An accessor for the `number` of the queries that your application is currently loading or fetching
|
|
666
694
|
* in the background.
|
|
667
|
-
*
|
|
668
695
|
* @example
|
|
669
696
|
* ```tsx
|
|
670
697
|
* import { useIsFetching } from '@tanstack/solid-query'
|
|
@@ -706,12 +733,10 @@ function mutationOptions(options) {
|
|
|
706
733
|
/**
|
|
707
734
|
* The `useIsMutating` primitive returns the `number` of mutations that your application currently has `pending`
|
|
708
735
|
* (useful for app-wide loading indicators).
|
|
709
|
-
*
|
|
710
736
|
* @param filters - An accessor returning the {@link MutationFilters} to narrow down the matched mutations.
|
|
711
737
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
712
738
|
* will be used.
|
|
713
739
|
* @returns An accessor for the `number` of the mutations that your application currently has `pending`.
|
|
714
|
-
*
|
|
715
740
|
* @example
|
|
716
741
|
* ```tsx
|
|
717
742
|
* import { useIsMutating } from '@tanstack/solid-query'
|
|
@@ -740,6 +765,13 @@ function useIsMutating(filters, queryClient) {
|
|
|
740
765
|
}
|
|
741
766
|
//#endregion
|
|
742
767
|
//#region src/useMutationState.ts
|
|
768
|
+
/**
|
|
769
|
+
* Collects the mutations in the cache that match `options.filters`, mapped with `options.select`
|
|
770
|
+
* (or to their state, by default).
|
|
771
|
+
* @param mutationCache - The mutation cache to read.
|
|
772
|
+
* @param options - The `filters` to match and the `select` function to map each mutation with.
|
|
773
|
+
* @returns The selected value of every matching mutation.
|
|
774
|
+
*/
|
|
743
775
|
function getResult(mutationCache, options) {
|
|
744
776
|
return mutationCache.findAll(options.filters).map((mutation) => options.select ? options.select(mutation) : mutation.state);
|
|
745
777
|
}
|
|
@@ -747,13 +779,11 @@ function getResult(mutationCache, options) {
|
|
|
747
779
|
* `useMutationState` is a primitive that gives you access to all mutations in the `MutationCache`. You can pass
|
|
748
780
|
* `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
|
|
749
781
|
* state.
|
|
750
|
-
*
|
|
751
782
|
* @param options - An accessor returning the `filters` to narrow down matched mutations, and an optional
|
|
752
783
|
* `select` to transform the mutation state.
|
|
753
784
|
* @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
754
785
|
* will be used.
|
|
755
786
|
* @returns An accessor for an array of whatever `select` returns for each matching mutation.
|
|
756
|
-
*
|
|
757
787
|
* @example
|
|
758
788
|
* Get all variables of all running mutations:
|
|
759
789
|
* ```tsx
|
|
@@ -768,7 +798,6 @@ function getResult(mutationCache, options) {
|
|
|
768
798
|
* return <>{pendingVariables().length} posts saving...</>
|
|
769
799
|
* }
|
|
770
800
|
* ```
|
|
771
|
-
*
|
|
772
801
|
* @example
|
|
773
802
|
* Get all data for specific mutations via the `mutationKey`:
|
|
774
803
|
* ```tsx
|
|
@@ -796,7 +825,6 @@ function getResult(mutationCache, options) {
|
|
|
796
825
|
* )
|
|
797
826
|
* }
|
|
798
827
|
* ```
|
|
799
|
-
*
|
|
800
828
|
* @example
|
|
801
829
|
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
802
830
|
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|