@apollo/client 3.10.4 → 3.10.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/CHANGELOG.md +31 -0
- package/apollo-client.cjs +63 -36
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/core/core.cjs +1 -1
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +1 -1
- package/dev/dev.cjs +1 -1
- package/dev/dev.cjs.map +1 -1
- package/dev/dev.cjs.native.js +1 -1
- package/link/batch-http/batch-http.cjs +6 -5
- package/link/batch-http/batch-http.cjs.map +1 -1
- package/link/batch-http/batch-http.cjs.native.js +6 -5
- package/link/batch-http/batchHttpLink.js +15 -10
- package/link/batch-http/batchHttpLink.js.map +1 -1
- package/link/error/error.cjs +3 -2
- package/link/error/error.cjs.map +1 -1
- package/link/error/error.cjs.native.js +3 -2
- package/link/error/index.js +3 -2
- package/link/error/index.js.map +1 -1
- package/package.json +10 -6
- package/react/hooks/hooks.cjs +61 -34
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +61 -34
- package/react/hooks/internal/useRenderGuard.js +37 -12
- package/react/hooks/internal/useRenderGuard.js.map +1 -1
- package/react/hooks/useLazyQuery.js +7 -8
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useLoadableQuery.d.ts +1 -1
- package/react/hooks/useLoadableQuery.js +9 -2
- package/react/hooks/useLoadableQuery.js.map +1 -1
- package/react/hooks/useMutation.js +11 -7
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts +2 -2
- package/react/hooks/useQuery.js +26 -12
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useQueryRefHandlers.js +4 -0
- package/react/hooks/useQueryRefHandlers.js.map +1 -1
- package/react/hooks/useReadQuery.js +5 -1
- package/react/hooks/useReadQuery.js.map +1 -1
- package/react/hooks/useSubscription.js +4 -0
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/hooks/useSuspenseQuery.js +2 -2
- package/react/hooks/useSuspenseQuery.js.map +1 -1
- package/react/hooks/useSyncExternalStore.js +4 -0
- package/react/hooks/useSyncExternalStore.js.map +1 -1
- package/react/internal/internal.cjs +1 -1
- package/react/internal/internal.cjs.map +1 -1
- package/react/internal/internal.cjs.native.js +1 -1
- package/testing/core/mocking/mockLink.d.ts +8 -3
- package/testing/core/mocking/mockLink.js.map +1 -1
- package/testing/internal/profile/profile.js +6 -2
- package/testing/internal/profile/profile.js.map +1 -1
- package/utilities/globals/globals.cjs +1 -1
- package/utilities/globals/globals.cjs.map +1 -1
- package/utilities/globals/globals.cjs.native.js +1 -1
- package/version.js +1 -1
|
@@ -95,15 +95,28 @@ function useDeepMemo(memoFn, deps) {
|
|
|
95
95
|
return ref.current.value;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
var RenderDispatcher = null;
|
|
98
|
+
var useIsomorphicLayoutEffect = utilities.canUseDOM ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
99
|
+
|
|
100
|
+
var Ctx;
|
|
101
|
+
function noop() { }
|
|
103
102
|
function useRenderGuard() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
if (!Ctx) {
|
|
104
|
+
Ctx = React__namespace.createContext(null);
|
|
105
|
+
}
|
|
106
|
+
return React__namespace.useCallback(
|
|
107
|
+
function () {
|
|
108
|
+
var orig = console.error;
|
|
109
|
+
try {
|
|
110
|
+
console.error = noop;
|
|
111
|
+
React__namespace["useContext" ](Ctx);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
console.error = orig;
|
|
119
|
+
}
|
|
107
120
|
}, []);
|
|
108
121
|
}
|
|
109
122
|
|
|
@@ -148,14 +161,16 @@ function _useQuery(query, options) {
|
|
|
148
161
|
return useInternalState(useApolloClient(options.client), query).useQuery(options);
|
|
149
162
|
}
|
|
150
163
|
function useInternalState(client, query) {
|
|
151
|
-
var
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
164
|
+
var forceUpdateState = React__namespace.useReducer(function (tick) { return tick + 1; }, 0)[1];
|
|
165
|
+
function createInternalState(previous) {
|
|
166
|
+
return Object.assign(new InternalState(client, query, previous), {
|
|
167
|
+
forceUpdateState: forceUpdateState,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
var _a = React__namespace.useState(createInternalState), state = _a[0], updateState = _a[1];
|
|
171
|
+
if (client !== state.client || query !== state.query) {
|
|
172
|
+
updateState((state = createInternalState(state)));
|
|
156
173
|
}
|
|
157
|
-
var state = stateRef.current;
|
|
158
|
-
state.forceUpdateState = React__namespace.useReducer(function (tick) { return tick + 1; }, 0)[1];
|
|
159
174
|
return state;
|
|
160
175
|
}
|
|
161
176
|
var InternalState = (function () {
|
|
@@ -218,7 +233,8 @@ var InternalState = (function () {
|
|
|
218
233
|
this.renderPromises = React__namespace.useContext(context.getApolloContext()).renderPromises;
|
|
219
234
|
this.useOptions(options);
|
|
220
235
|
var obsQuery = this.useObservableQuery();
|
|
221
|
-
var result = useSyncExternalStore(
|
|
236
|
+
var result = useSyncExternalStore(
|
|
237
|
+
React__namespace.useCallback(function (handleStoreChange) {
|
|
222
238
|
if (_this.renderPromises) {
|
|
223
239
|
return function () { };
|
|
224
240
|
}
|
|
@@ -453,17 +469,15 @@ function useLazyQuery(query, options) {
|
|
|
453
469
|
var useQueryResult = internalState.useQuery(tslib.__assign(tslib.__assign({}, merged), { skip: !execOptionsRef.current }));
|
|
454
470
|
var initialFetchPolicy = useQueryResult.observable.options.initialFetchPolicy ||
|
|
455
471
|
internalState.getDefaultFetchPolicy();
|
|
456
|
-
var
|
|
457
|
-
called: !!execOptionsRef.current,
|
|
458
|
-
});
|
|
472
|
+
var forceUpdateState = internalState.forceUpdateState, obsQueryFields = internalState.obsQueryFields;
|
|
459
473
|
var eagerMethods = React__namespace.useMemo(function () {
|
|
460
474
|
var eagerMethods = {};
|
|
461
475
|
var _loop_1 = function (key) {
|
|
462
|
-
var method =
|
|
476
|
+
var method = obsQueryFields[key];
|
|
463
477
|
eagerMethods[key] = function () {
|
|
464
478
|
if (!execOptionsRef.current) {
|
|
465
479
|
execOptionsRef.current = Object.create(null);
|
|
466
|
-
|
|
480
|
+
forceUpdateState();
|
|
467
481
|
}
|
|
468
482
|
return method.apply(this, arguments);
|
|
469
483
|
};
|
|
@@ -473,8 +487,9 @@ function useLazyQuery(query, options) {
|
|
|
473
487
|
_loop_1(key);
|
|
474
488
|
}
|
|
475
489
|
return eagerMethods;
|
|
476
|
-
}, []);
|
|
477
|
-
|
|
490
|
+
}, [forceUpdateState, obsQueryFields]);
|
|
491
|
+
var called = !!execOptionsRef.current;
|
|
492
|
+
var result = React__namespace.useMemo(function () { return (tslib.__assign(tslib.__assign(tslib.__assign({}, useQueryResult), eagerMethods), { called: called })); }, [useQueryResult, eagerMethods, called]);
|
|
478
493
|
var execute = React__namespace.useCallback(function (executeOptions) {
|
|
479
494
|
execOptionsRef.current =
|
|
480
495
|
executeOptions ? tslib.__assign(tslib.__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
|
@@ -486,7 +501,7 @@ function useLazyQuery(query, options) {
|
|
|
486
501
|
.then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
|
487
502
|
promise.catch(function () { });
|
|
488
503
|
return promise;
|
|
489
|
-
}, []);
|
|
504
|
+
}, [eagerMethods, initialFetchPolicy, internalState]);
|
|
490
505
|
return [execute, result];
|
|
491
506
|
}
|
|
492
507
|
|
|
@@ -506,9 +521,9 @@ function useMutation(mutation, options) {
|
|
|
506
521
|
mutation: mutation,
|
|
507
522
|
options: options,
|
|
508
523
|
});
|
|
509
|
-
{
|
|
524
|
+
useIsomorphicLayoutEffect(function () {
|
|
510
525
|
Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
|
511
|
-
}
|
|
526
|
+
});
|
|
512
527
|
var execute = React__namespace.useCallback(function (executeOptions) {
|
|
513
528
|
if (executeOptions === void 0) { executeOptions = {}; }
|
|
514
529
|
var _a = ref.current, options = _a.options, mutation = _a.mutation;
|
|
@@ -582,15 +597,20 @@ function useMutation(mutation, options) {
|
|
|
582
597
|
}, []);
|
|
583
598
|
var reset = React__namespace.useCallback(function () {
|
|
584
599
|
if (ref.current.isMounted) {
|
|
585
|
-
var result_3 = {
|
|
600
|
+
var result_3 = {
|
|
601
|
+
called: false,
|
|
602
|
+
loading: false,
|
|
603
|
+
client: ref.current.client,
|
|
604
|
+
};
|
|
586
605
|
Object.assign(ref.current, { mutationId: 0, result: result_3 });
|
|
587
606
|
setResult(result_3);
|
|
588
607
|
}
|
|
589
608
|
}, []);
|
|
590
609
|
React__namespace.useEffect(function () {
|
|
591
|
-
|
|
610
|
+
var current = ref.current;
|
|
611
|
+
current.isMounted = true;
|
|
592
612
|
return function () {
|
|
593
|
-
|
|
613
|
+
current.isMounted = false;
|
|
594
614
|
};
|
|
595
615
|
}, []);
|
|
596
616
|
return [execute, tslib.__assign({ reset: reset }, result)];
|
|
@@ -846,11 +866,11 @@ function _useSuspenseQuery(query, options) {
|
|
|
846
866
|
};
|
|
847
867
|
}, [queryRef.result]);
|
|
848
868
|
var result = fetchPolicy === "standby" ? skipResult : __use(promise);
|
|
849
|
-
var fetchMore = React__namespace.useCallback(
|
|
869
|
+
var fetchMore = React__namespace.useCallback(function (options) {
|
|
850
870
|
var promise = queryRef.fetchMore(options);
|
|
851
871
|
setPromise([queryRef.key, queryRef.promise]);
|
|
852
872
|
return promise;
|
|
853
|
-
}
|
|
873
|
+
}, [queryRef]);
|
|
854
874
|
var refetch = React__namespace.useCallback(function (variables) {
|
|
855
875
|
var promise = queryRef.refetch(variables);
|
|
856
876
|
setPromise([queryRef.key, queryRef.promise]);
|
|
@@ -1013,10 +1033,17 @@ function useLoadableQuery(query, options) {
|
|
|
1013
1033
|
return client.watchQuery(tslib.__assign(tslib.__assign({}, watchQueryOptions), { variables: variables }));
|
|
1014
1034
|
});
|
|
1015
1035
|
setQueryRef(internal.wrapQueryRef(queryRef));
|
|
1016
|
-
}, [
|
|
1036
|
+
}, [
|
|
1037
|
+
query,
|
|
1038
|
+
queryKey,
|
|
1039
|
+
suspenseCache,
|
|
1040
|
+
watchQueryOptions,
|
|
1041
|
+
calledDuringRender,
|
|
1042
|
+
client,
|
|
1043
|
+
]);
|
|
1017
1044
|
var reset = React__namespace.useCallback(function () {
|
|
1018
1045
|
setQueryRef(null);
|
|
1019
|
-
}, [
|
|
1046
|
+
}, []);
|
|
1020
1047
|
return [loadQuery, queryRef, { fetchMore: fetchMore, refetch: refetch, reset: reset }];
|
|
1021
1048
|
}
|
|
1022
1049
|
|
|
@@ -1071,7 +1098,7 @@ function _useReadQuery(queryRef) {
|
|
|
1071
1098
|
internal.updateWrappedQueryRef(queryRef, promise);
|
|
1072
1099
|
forceUpdate();
|
|
1073
1100
|
});
|
|
1074
|
-
}, [internalQueryRef]), getPromise, getPromise);
|
|
1101
|
+
}, [internalQueryRef, queryRef]), getPromise, getPromise);
|
|
1075
1102
|
var result = __use(promise);
|
|
1076
1103
|
return React__namespace.useMemo(function () {
|
|
1077
1104
|
return {
|
|
@@ -1,17 +1,42 @@
|
|
|
1
1
|
import * as React from "rehackt";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return (_b = (_a = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === null || _a === void 0 ? void 0 : _a.ReactCurrentDispatcher) === null || _b === void 0 ? void 0 : _b.current;
|
|
5
|
-
}
|
|
6
|
-
var RenderDispatcher = null;
|
|
7
|
-
/*
|
|
8
|
-
Relay does this too, so we hope this is safe.
|
|
9
|
-
https://github.com/facebook/relay/blob/8651fbca19adbfbb79af7a3bc40834d105fd7747/packages/react-relay/relay-hooks/loadQuery.js#L90-L98
|
|
10
|
-
*/
|
|
2
|
+
var Ctx;
|
|
3
|
+
function noop() { }
|
|
11
4
|
export function useRenderGuard() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
if (!Ctx) {
|
|
6
|
+
// we want the intialization to be lazy because `createContext` would error on import in a RSC
|
|
7
|
+
Ctx = React.createContext(null);
|
|
8
|
+
}
|
|
9
|
+
return React.useCallback(
|
|
10
|
+
/**
|
|
11
|
+
* @returns true if the hook was called during render
|
|
12
|
+
*/ function () {
|
|
13
|
+
var orig = console.error;
|
|
14
|
+
try {
|
|
15
|
+
console.error = noop;
|
|
16
|
+
/**
|
|
17
|
+
* `useContext` can be called conditionally during render, so this is safe.
|
|
18
|
+
* (Also, during render we would want to throw as a reaction to this anyways, so it
|
|
19
|
+
* wouldn't even matter if we got the order of hooks mixed up...)
|
|
20
|
+
*
|
|
21
|
+
* They cannot however be called outside of Render, and that's what we're testing here.
|
|
22
|
+
*
|
|
23
|
+
* Different versions of React have different behaviour on an invalid hook call:
|
|
24
|
+
*
|
|
25
|
+
* React 16.8 - 17: throws an error
|
|
26
|
+
* https://github.com/facebook/react/blob/2b93d686e359c7afa299e2ec5cf63160a32a1155/packages/react/src/ReactHooks.js#L18-L26
|
|
27
|
+
*
|
|
28
|
+
* React 18 & 19: `console.error` in development, then `resolveDispatcher` returns `null` and a member access on `null` throws.
|
|
29
|
+
* https://github.com/facebook/react/blob/58e8304483ebfadd02a295339b5e9a989ac98c6e/packages/react/src/ReactHooks.js#L28-L35
|
|
30
|
+
*/
|
|
31
|
+
React["useContext" /* hide this from the linter */](Ctx);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
console.error = orig;
|
|
39
|
+
}
|
|
15
40
|
}, []);
|
|
16
41
|
}
|
|
17
42
|
//# sourceMappingURL=useRenderGuard.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRenderGuard.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/useRenderGuard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,SAAS,
|
|
1
|
+
{"version":3,"file":"useRenderGuard.js","sourceRoot":"","sources":["../../../../src/react/hooks/internal/useRenderGuard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,IAAI,GAAwB,CAAC;AAE7B,SAAS,IAAI,KAAI,CAAC;AAClB,MAAM,UAAU,cAAc;IAC5B,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,8FAA8F;QAC9F,GAAG,GAAG,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,OAAO,KAAK,CAAC,WAAW;IACtB;;OAEG,CAAC;QACF,IAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC;YACH,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;YAErB;;;;;;;;;;;;;;eAcG;YACH,KAAK,CAAC,YAAY,CAAC,+BAA+B,CAAC,CAAC,GAAG,CAAC,CAAC;YACzD,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,KAAK,CAAC;QACf,CAAC;gBAAS,CAAC;YACT,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;QACvB,CAAC;IACH,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import * as React from \"rehackt\";\n\nlet Ctx: React.Context<null>;\n\nfunction noop() {}\nexport function useRenderGuard() {\n if (!Ctx) {\n // we want the intialization to be lazy because `createContext` would error on import in a RSC\n Ctx = React.createContext(null);\n }\n\n return React.useCallback(\n /**\n * @returns true if the hook was called during render\n */ () => {\n const orig = console.error;\n try {\n console.error = noop;\n\n /**\n * `useContext` can be called conditionally during render, so this is safe.\n * (Also, during render we would want to throw as a reaction to this anyways, so it\n * wouldn't even matter if we got the order of hooks mixed up...)\n *\n * They cannot however be called outside of Render, and that's what we're testing here.\n *\n * Different versions of React have different behaviour on an invalid hook call:\n *\n * React 16.8 - 17: throws an error\n * https://github.com/facebook/react/blob/2b93d686e359c7afa299e2ec5cf63160a32a1155/packages/react/src/ReactHooks.js#L18-L26\n *\n * React 18 & 19: `console.error` in development, then `resolveDispatcher` returns `null` and a member access on `null` throws.\n * https://github.com/facebook/react/blob/58e8304483ebfadd02a295339b5e9a989ac98c6e/packages/react/src/ReactHooks.js#L28-L35\n */\n React[\"useContext\" /* hide this from the linter */](Ctx);\n return true;\n } catch (e) {\n return false;\n } finally {\n console.error = orig;\n }\n },\n []\n );\n}\n"]}
|
|
@@ -63,19 +63,17 @@ export function useLazyQuery(query, options) {
|
|
|
63
63
|
var useQueryResult = internalState.useQuery(__assign(__assign({}, merged), { skip: !execOptionsRef.current }));
|
|
64
64
|
var initialFetchPolicy = useQueryResult.observable.options.initialFetchPolicy ||
|
|
65
65
|
internalState.getDefaultFetchPolicy();
|
|
66
|
-
var
|
|
67
|
-
called: !!execOptionsRef.current,
|
|
68
|
-
});
|
|
66
|
+
var forceUpdateState = internalState.forceUpdateState, obsQueryFields = internalState.obsQueryFields;
|
|
69
67
|
// We use useMemo here to make sure the eager methods have a stable identity.
|
|
70
68
|
var eagerMethods = React.useMemo(function () {
|
|
71
69
|
var eagerMethods = {};
|
|
72
70
|
var _loop_1 = function (key) {
|
|
73
|
-
var method =
|
|
71
|
+
var method = obsQueryFields[key];
|
|
74
72
|
eagerMethods[key] = function () {
|
|
75
73
|
if (!execOptionsRef.current) {
|
|
76
74
|
execOptionsRef.current = Object.create(null);
|
|
77
75
|
// Only the first time populating execOptionsRef.current matters here.
|
|
78
|
-
|
|
76
|
+
forceUpdateState();
|
|
79
77
|
}
|
|
80
78
|
// @ts-expect-error this is just too generic to type
|
|
81
79
|
return method.apply(this, arguments);
|
|
@@ -86,8 +84,9 @@ export function useLazyQuery(query, options) {
|
|
|
86
84
|
_loop_1(key);
|
|
87
85
|
}
|
|
88
86
|
return eagerMethods;
|
|
89
|
-
}, []);
|
|
90
|
-
|
|
87
|
+
}, [forceUpdateState, obsQueryFields]);
|
|
88
|
+
var called = !!execOptionsRef.current;
|
|
89
|
+
var result = React.useMemo(function () { return (__assign(__assign(__assign({}, useQueryResult), eagerMethods), { called: called })); }, [useQueryResult, eagerMethods, called]);
|
|
91
90
|
var execute = React.useCallback(function (executeOptions) {
|
|
92
91
|
execOptionsRef.current =
|
|
93
92
|
executeOptions ? __assign(__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
|
@@ -101,7 +100,7 @@ export function useLazyQuery(query, options) {
|
|
|
101
100
|
// to catch the promise to prevent unhandled rejections.
|
|
102
101
|
promise.catch(function () { });
|
|
103
102
|
return promise;
|
|
104
|
-
}, []);
|
|
103
|
+
}, [eagerMethods, initialFetchPolicy, internalState]);
|
|
105
104
|
return [execute, result];
|
|
106
105
|
}
|
|
107
106
|
//# sourceMappingURL=useLazyQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLazyQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAGjC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"useLazyQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAGjC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAOxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,2EAA2E;AAC3E,+DAA+D;A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AAI,CAAC,UAAC,WAAW,IAAK,OAAA,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,YAAY,CAAC,EAAxC,CAAwC,CAAC,CAAC;QAEnE,yEAAyE;QACzE,wDAAwD;QACxD,OAAO,CAAC,KAAK,CAAC,cAAO,CAAC,CAAC,CAAC;QAExB,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,YAAY,EAAE,kBAAkB,EAAE,aAAa,CAAC,CAClD,CAAC;IAEF,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import type { DocumentNode } from \"graphql\";\nimport type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport * as React from \"rehackt\";\n\nimport type { OperationVariables } from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport type {\n LazyQueryHookExecOptions,\n LazyQueryHookOptions,\n LazyQueryResultTuple,\n NoInfer,\n} from \"../types/types.js\";\nimport { useInternalState } from \"./useQuery.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\n// The following methods, when called will execute the query, regardless of\n// whether the useLazyQuery execute function was called before.\nconst EAGER_METHODS = [\n \"refetch\",\n \"reobserve\",\n \"fetchMore\",\n \"updateQuery\",\n \"startPolling\",\n \"subscribeToMore\",\n] as const;\n\n/**\n * A hook for imperatively executing queries in an Apollo application, e.g. in response to user interaction.\n *\n * > Refer to the [Queries - Manual execution with useLazyQuery](https://www.apollographql.com/docs/react/data/queries#manual-execution-with-uselazyquery) section for a more in-depth overview of `useLazyQuery`.\n *\n * @example\n * ```jsx\n * import { gql, useLazyQuery } from \"@apollo/client\";\n *\n * const GET_GREETING = gql`\n * query GetGreeting($language: String!) {\n * greeting(language: $language) {\n * message\n * }\n * }\n * `;\n *\n * function Hello() {\n * const [loadGreeting, { called, loading, data }] = useLazyQuery(\n * GET_GREETING,\n * { variables: { language: \"english\" } }\n * );\n * if (called && loading) return <p>Loading ...</p>\n * if (!called) {\n * return <button onClick={() => loadGreeting()}>Load greeting</button>\n * }\n * return <h1>Hello {data.greeting.message}!</h1>;\n * }\n * ```\n * @since 3.0.0\n *\n * @param query - A GraphQL query document parsed into an AST by `gql`.\n * @param options - Default options to control how the query is executed.\n * @returns A tuple in the form of `[execute, result]`\n */\nexport function useLazyQuery<\n TData = any,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LazyQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>\n): LazyQueryResultTuple<TData, TVariables> {\n const execOptionsRef =\n React.useRef<Partial<LazyQueryHookExecOptions<TData, TVariables>>>();\n const optionsRef = React.useRef<LazyQueryHookOptions<TData, TVariables>>();\n const queryRef = React.useRef<\n DocumentNode | TypedDocumentNode<TData, TVariables>\n >();\n const merged = mergeOptions(options, execOptionsRef.current || {});\n const document = merged?.query ?? query;\n\n // Use refs to track options and the used query to ensure the `execute`\n // function remains referentially stable between renders.\n optionsRef.current = options;\n queryRef.current = document;\n\n const internalState = useInternalState<TData, TVariables>(\n useApolloClient(options && options.client),\n document\n );\n\n const useQueryResult = internalState.useQuery({\n ...merged,\n skip: !execOptionsRef.current,\n });\n\n const initialFetchPolicy =\n useQueryResult.observable.options.initialFetchPolicy ||\n internalState.getDefaultFetchPolicy();\n\n const { forceUpdateState, obsQueryFields } = internalState;\n // We use useMemo here to make sure the eager methods have a stable identity.\n const eagerMethods = React.useMemo(() => {\n const eagerMethods: Record<string, any> = {};\n for (const key of EAGER_METHODS) {\n const method = obsQueryFields[key];\n eagerMethods[key] = function () {\n if (!execOptionsRef.current) {\n execOptionsRef.current = Object.create(null);\n // Only the first time populating execOptionsRef.current matters here.\n forceUpdateState();\n }\n // @ts-expect-error this is just too generic to type\n return method.apply(this, arguments);\n };\n }\n\n return eagerMethods;\n }, [forceUpdateState, obsQueryFields]);\n\n const called = !!execOptionsRef.current;\n const result = React.useMemo(\n () => ({\n ...useQueryResult,\n ...eagerMethods,\n called,\n }),\n [useQueryResult, eagerMethods, called]\n );\n\n const execute = React.useCallback<LazyQueryResultTuple<TData, TVariables>[0]>(\n (executeOptions) => {\n execOptionsRef.current =\n executeOptions ?\n {\n ...executeOptions,\n fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy,\n }\n : {\n fetchPolicy: initialFetchPolicy,\n };\n\n const options = mergeOptions(optionsRef.current, {\n query: queryRef.current,\n ...execOptionsRef.current,\n });\n\n const promise = internalState\n .executeQuery({ ...options, skip: false })\n .then((queryResult) => Object.assign(queryResult, eagerMethods));\n\n // Because the return value of `useLazyQuery` is usually floated, we need\n // to catch the promise to prevent unhandled rejections.\n promise.catch(() => {});\n\n return promise;\n },\n [eagerMethods, initialFetchPolicy, internalState]\n );\n\n return [execute, result];\n}\n"]}
|
|
@@ -8,7 +8,7 @@ type ResetFunction = () => void;
|
|
|
8
8
|
export type UseLoadableQueryResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> = [
|
|
9
9
|
loadQuery: LoadQueryFunction<TVariables>,
|
|
10
10
|
queryRef: QueryRef<TData, TVariables> | null,
|
|
11
|
-
{
|
|
11
|
+
handlers: {
|
|
12
12
|
/**
|
|
13
13
|
* A function that helps you fetch the next set of results for a [paginated list field](https://www.apollographql.com/docs/react/pagination/core-api/).
|
|
14
14
|
*
|
|
@@ -51,10 +51,17 @@ export function useLoadableQuery(query, options) {
|
|
|
51
51
|
return client.watchQuery(__assign(__assign({}, watchQueryOptions), { variables: variables }));
|
|
52
52
|
});
|
|
53
53
|
setQueryRef(wrapQueryRef(queryRef));
|
|
54
|
-
}, [
|
|
54
|
+
}, [
|
|
55
|
+
query,
|
|
56
|
+
queryKey,
|
|
57
|
+
suspenseCache,
|
|
58
|
+
watchQueryOptions,
|
|
59
|
+
calledDuringRender,
|
|
60
|
+
client,
|
|
61
|
+
]);
|
|
55
62
|
var reset = React.useCallback(function () {
|
|
56
63
|
setQueryRef(null);
|
|
57
|
-
}, [
|
|
64
|
+
}, []);
|
|
58
65
|
return [loadQuery, queryRef, { fetchMore: fetchMore, refetch: refetch, reset: reset }];
|
|
59
66
|
}
|
|
60
67
|
//# sourceMappingURL=useLoadableQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLoadableQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLoadableQuery.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAQjC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,YAAY,GACb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAS,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAK1D,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAqI7D,MAAM,UAAU,gBAAgB,CAI9B,KAA0D,EAC1D,OAAuD;IAAvD,wBAAA,EAAA,UAAoC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IAEvD,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAM,aAAa,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAM,iBAAiB,GAAG,oBAAoB,CAAC,EAAE,MAAM,QAAA,EAAE,KAAK,OAAA,EAAE,OAAO,SAAA,EAAE,CAAC,CAAC;IACnE,IAAA,KAAkB,OAAO,SAAZ,EAAb,QAAQ,mBAAG,EAAE,KAAA,CAAa;IAE5B,IAAA,KAA0B,KAAK,CAAC,QAAQ,CAGpC,IAAI,CAAC,EAHR,QAAQ,QAAA,EAAE,WAAW,QAGb,CAAC;IAEhB,qBAAqB,CAAC,QAAQ,CAAC,CAAC;IAEhC,IAAM,gBAAgB,GAAG,QAAQ,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QAEK,IAAA,SAAS,GAAI,IAAI,GAAR,CAAS;QAEzB,IAAM,QAAQ;YACZ,KAAK;YACL,kBAAkB,CAAC,SAAS,CAAC;WACzB,EAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,OAClC,CAAC;QAEF,IAAM,QAAQ,GAAG,aAAa,CAAC,WAAW,CAAC,QAAQ,EAAE;YACnD,OAAA,MAAM,CAAC,UAAU,CAAC,sBACb,iBAAiB,KACpB,SAAS,WAAA,GACqB,CAAC;QAHjC,CAGiC,CAClC,CAAC;QAEF,WAAW,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;IACtC,CAAC,EACD,CAAC,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,CAAC,CACxE,CAAC;IAEF,IAAM,KAAK,GAAkB,KAAK,CAAC,WAAW,CAAC;QAC7C,WAAW,CAAC,IAAI,CAAC,CAAC;IACpB,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,OAAO,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE,SAAS,WAAA,EAAE,OAAO,SAAA,EAAE,KAAK,OAAA,EAAE,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type {\n DocumentNode,\n FetchMoreQueryOptions,\n OperationVariables,\n TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n assertWrappedQueryRef,\n getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryRef } from \"../internal/index.js\";\nimport type { LoadableQueryHookOptions } from \"../types/types.js\";\nimport { __use, useRenderGuard } from \"./internal/index.js\";\nimport { useWatchQueryOptions } from \"./useSuspenseQuery.js\";\nimport type { FetchMoreFunction, RefetchFunction } from \"./useSuspenseQuery.js\";\nimport { canonicalStringify } from \"../../cache/index.js\";\nimport type {\n DeepPartial,\n OnlyRequiredProperties,\n} from \"../../utilities/index.js\";\nimport { invariant } from \"../../utilities/globals/index.js\";\n\nexport type LoadQueryFunction<TVariables extends OperationVariables> = (\n // Use variadic args to handle cases where TVariables is type `never`, in\n // which case we don't want to allow a variables argument. In other\n // words, we don't want to allow variables to be passed as an argument to this\n // function if the query does not expect variables in the document.\n ...args: [TVariables] extends [never] ? []\n : {} extends OnlyRequiredProperties<TVariables> ? [variables?: TVariables]\n : [variables: TVariables]\n) => void;\n\ntype ResetFunction = () => void;\n\nexport type UseLoadableQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n> = [\n loadQuery: LoadQueryFunction<TVariables>,\n queryRef: QueryRef<TData, TVariables> | null,\n {\n /** {@inheritDoc @apollo/client!QueryResultDocumentation#fetchMore:member} */\n fetchMore: FetchMoreFunction<TData, TVariables>;\n /** {@inheritDoc @apollo/client!QueryResultDocumentation#refetch:member} */\n refetch: RefetchFunction<TData, TVariables>;\n /**\n * A function that resets the `queryRef` back to `null`.\n */\n reset: ResetFunction;\n },\n];\n\nexport function useLoadableQuery<\n TData,\n TVariables extends OperationVariables,\n TOptions extends LoadableQueryHookOptions,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LoadableQueryHookOptions & TOptions\n): UseLoadableQueryResult<\n TOptions[\"errorPolicy\"] extends \"ignore\" | \"all\" ?\n TOptions[\"returnPartialData\"] extends true ?\n DeepPartial<TData> | undefined\n : TData | undefined\n : TOptions[\"returnPartialData\"] extends true ? DeepPartial<TData>\n : TData,\n TVariables\n>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n): UseLoadableQueryResult<DeepPartial<TData> | undefined, TVariables>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions & {\n errorPolicy: \"ignore\" | \"all\";\n }\n): UseLoadableQueryResult<TData | undefined, TVariables>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions & {\n returnPartialData: true;\n }\n): UseLoadableQueryResult<DeepPartial<TData>, TVariables>;\n\n/**\n * A hook for imperatively loading a query, such as responding to a user\n * interaction.\n *\n * > Refer to the [Suspense - Fetching in response to user interaction](https://www.apollographql.com/docs/react/data/suspense#fetching-in-response-to-user-interaction) section for a more in-depth overview of `useLoadableQuery`.\n *\n * @example\n * ```jsx\n * import { gql, useLoadableQuery } from \"@apollo/client\";\n *\n * const GET_GREETING = gql`\n * query GetGreeting($language: String!) {\n * greeting(language: $language) {\n * message\n * }\n * }\n * `;\n *\n * function App() {\n * const [loadGreeting, queryRef] = useLoadableQuery(GET_GREETING);\n *\n * return (\n * <>\n * <button onClick={() => loadGreeting({ language: \"english\" })}>\n * Load greeting\n * </button>\n * <Suspense fallback={<div>Loading...</div>}>\n * {queryRef && <Hello queryRef={queryRef} />}\n * </Suspense>\n * </>\n * );\n * }\n *\n * function Hello({ queryRef }) {\n * const { data } = useReadQuery(queryRef);\n *\n * return <div>{data.greeting.message}</div>;\n * }\n * ```\n *\n * @since 3.9.0\n * @param query - A GraphQL query document parsed into an AST by `gql`.\n * @param options - Options to control how the query is executed.\n * @returns A tuple in the form of `[loadQuery, queryRef, handlers]`\n */\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LoadableQueryHookOptions\n): UseLoadableQueryResult<TData, TVariables>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions = Object.create(null)\n): UseLoadableQueryResult<TData, TVariables> {\n const client = useApolloClient(options.client);\n const suspenseCache = getSuspenseCache(client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { queryKey = [] } = options;\n\n const [queryRef, setQueryRef] = React.useState<QueryRef<\n TData,\n TVariables\n > | null>(null);\n\n assertWrappedQueryRef(queryRef);\n\n const internalQueryRef = queryRef && unwrapQueryRef(queryRef);\n\n if (queryRef && internalQueryRef?.didChangeOptions(watchQueryOptions)) {\n const promise = internalQueryRef.applyOptions(watchQueryOptions);\n updateWrappedQueryRef(queryRef, promise);\n }\n\n const calledDuringRender = useRenderGuard();\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(\n (options) => {\n if (!internalQueryRef) {\n throw new Error(\n \"The query has not been loaded. Please load the query.\"\n );\n }\n\n const promise = internalQueryRef.fetchMore(\n options as FetchMoreQueryOptions<TVariables, TData>\n );\n\n setQueryRef(wrapQueryRef(internalQueryRef));\n\n return promise;\n },\n [internalQueryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = React.useCallback(\n (options) => {\n if (!internalQueryRef) {\n throw new Error(\n \"The query has not been loaded. Please load the query.\"\n );\n }\n\n const promise = internalQueryRef.refetch(options);\n\n setQueryRef(wrapQueryRef(internalQueryRef));\n\n return promise;\n },\n [internalQueryRef]\n );\n\n const loadQuery: LoadQueryFunction<TVariables> = React.useCallback(\n (...args) => {\n invariant(\n !calledDuringRender(),\n \"useLoadableQuery: 'loadQuery' should not be called during render. To start a query during render, use the 'useBackgroundQuery' hook.\"\n );\n\n const [variables] = args;\n\n const cacheKey: CacheKey = [\n query,\n canonicalStringify(variables),\n ...([] as any[]).concat(queryKey),\n ];\n\n const queryRef = suspenseCache.getQueryRef(cacheKey, () =>\n client.watchQuery({\n ...watchQueryOptions,\n variables,\n } as WatchQueryOptions<any, any>)\n );\n\n setQueryRef(wrapQueryRef(queryRef));\n },\n [query, queryKey, suspenseCache, watchQueryOptions, calledDuringRender]\n );\n\n const reset: ResetFunction = React.useCallback(() => {\n setQueryRef(null);\n }, [queryRef]);\n\n return [loadQuery, queryRef, { fetchMore, refetch, reset }];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useLoadableQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLoadableQuery.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAQjC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,YAAY,GACb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAS,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAK1D,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAqI7D,MAAM,UAAU,gBAAgB,CAI9B,KAA0D,EAC1D,OAAuD;IAAvD,wBAAA,EAAA,UAAoC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IAEvD,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAM,aAAa,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAM,iBAAiB,GAAG,oBAAoB,CAAC,EAAE,MAAM,QAAA,EAAE,KAAK,OAAA,EAAE,OAAO,SAAA,EAAE,CAAC,CAAC;IACnE,IAAA,KAAkB,OAAO,SAAZ,EAAb,QAAQ,mBAAG,EAAE,KAAA,CAAa;IAE5B,IAAA,KAA0B,KAAK,CAAC,QAAQ,CAGpC,IAAI,CAAC,EAHR,QAAQ,QAAA,EAAE,WAAW,QAGb,CAAC;IAEhB,qBAAqB,CAAC,QAAQ,CAAC,CAAC;IAEhC,IAAM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B,sIAAsI,CACvI,CAAC;QAEK,IAAA,SAAS,GAAI,IAAI,GAAR,CAAS;QAEzB,IAAM,QAAQ;YACZ,KAAK;YACL,kBAAkB,CAAC,SAAS,CAAC;WACzB,EAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,OAClC,CAAC;QAEF,IAAM,QAAQ,GAAG,aAAa,CAAC,WAAW,CAAC,QAAQ,EAAE;YACnD,OAAA,MAAM,CAAC,UAAU,CAAC,sBACb,iBAAiB,KACpB,SAAS,WAAA,GACqB,CAAC;QAHjC,CAGiC,CAClC,CAAC;QAEF,WAAW,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;IACtC,CAAC,EACD;QACE,KAAK;QACL,QAAQ;QACR,aAAa;QACb,iBAAiB;QACjB,kBAAkB;QAClB,MAAM;KACP,CACF,CAAC;IAEF,IAAM,KAAK,GAAkB,KAAK,CAAC,WAAW,CAAC;QAC7C,WAAW,CAAC,IAAI,CAAC,CAAC;IACpB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE,SAAS,WAAA,EAAE,OAAO,SAAA,EAAE,KAAK,OAAA,EAAE,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type {\n DocumentNode,\n FetchMoreQueryOptions,\n OperationVariables,\n TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n assertWrappedQueryRef,\n getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryRef } from \"../internal/index.js\";\nimport type { LoadableQueryHookOptions } from \"../types/types.js\";\nimport { __use, useRenderGuard } from \"./internal/index.js\";\nimport { useWatchQueryOptions } from \"./useSuspenseQuery.js\";\nimport type { FetchMoreFunction, RefetchFunction } from \"./useSuspenseQuery.js\";\nimport { canonicalStringify } from \"../../cache/index.js\";\nimport type {\n DeepPartial,\n OnlyRequiredProperties,\n} from \"../../utilities/index.js\";\nimport { invariant } from \"../../utilities/globals/index.js\";\n\nexport type LoadQueryFunction<TVariables extends OperationVariables> = (\n // Use variadic args to handle cases where TVariables is type `never`, in\n // which case we don't want to allow a variables argument. In other\n // words, we don't want to allow variables to be passed as an argument to this\n // function if the query does not expect variables in the document.\n ...args: [TVariables] extends [never] ? []\n : {} extends OnlyRequiredProperties<TVariables> ? [variables?: TVariables]\n : [variables: TVariables]\n) => void;\n\ntype ResetFunction = () => void;\n\nexport type UseLoadableQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n> = [\n loadQuery: LoadQueryFunction<TVariables>,\n queryRef: QueryRef<TData, TVariables> | null,\n handlers: {\n /** {@inheritDoc @apollo/client!QueryResultDocumentation#fetchMore:member} */\n fetchMore: FetchMoreFunction<TData, TVariables>;\n /** {@inheritDoc @apollo/client!QueryResultDocumentation#refetch:member} */\n refetch: RefetchFunction<TData, TVariables>;\n /**\n * A function that resets the `queryRef` back to `null`.\n */\n reset: ResetFunction;\n },\n];\n\nexport function useLoadableQuery<\n TData,\n TVariables extends OperationVariables,\n TOptions extends LoadableQueryHookOptions,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LoadableQueryHookOptions & TOptions\n): UseLoadableQueryResult<\n TOptions[\"errorPolicy\"] extends \"ignore\" | \"all\" ?\n TOptions[\"returnPartialData\"] extends true ?\n DeepPartial<TData> | undefined\n : TData | undefined\n : TOptions[\"returnPartialData\"] extends true ? DeepPartial<TData>\n : TData,\n TVariables\n>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n): UseLoadableQueryResult<DeepPartial<TData> | undefined, TVariables>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions & {\n errorPolicy: \"ignore\" | \"all\";\n }\n): UseLoadableQueryResult<TData | undefined, TVariables>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions & {\n returnPartialData: true;\n }\n): UseLoadableQueryResult<DeepPartial<TData>, TVariables>;\n\n/**\n * A hook for imperatively loading a query, such as responding to a user\n * interaction.\n *\n * > Refer to the [Suspense - Fetching in response to user interaction](https://www.apollographql.com/docs/react/data/suspense#fetching-in-response-to-user-interaction) section for a more in-depth overview of `useLoadableQuery`.\n *\n * @example\n * ```jsx\n * import { gql, useLoadableQuery } from \"@apollo/client\";\n *\n * const GET_GREETING = gql`\n * query GetGreeting($language: String!) {\n * greeting(language: $language) {\n * message\n * }\n * }\n * `;\n *\n * function App() {\n * const [loadGreeting, queryRef] = useLoadableQuery(GET_GREETING);\n *\n * return (\n * <>\n * <button onClick={() => loadGreeting({ language: \"english\" })}>\n * Load greeting\n * </button>\n * <Suspense fallback={<div>Loading...</div>}>\n * {queryRef && <Hello queryRef={queryRef} />}\n * </Suspense>\n * </>\n * );\n * }\n *\n * function Hello({ queryRef }) {\n * const { data } = useReadQuery(queryRef);\n *\n * return <div>{data.greeting.message}</div>;\n * }\n * ```\n *\n * @since 3.9.0\n * @param query - A GraphQL query document parsed into an AST by `gql`.\n * @param options - Options to control how the query is executed.\n * @returns A tuple in the form of `[loadQuery, queryRef, handlers]`\n */\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LoadableQueryHookOptions\n): UseLoadableQueryResult<TData, TVariables>;\n\nexport function useLoadableQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: LoadableQueryHookOptions = Object.create(null)\n): UseLoadableQueryResult<TData, TVariables> {\n const client = useApolloClient(options.client);\n const suspenseCache = getSuspenseCache(client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { queryKey = [] } = options;\n\n const [queryRef, setQueryRef] = React.useState<QueryRef<\n TData,\n TVariables\n > | null>(null);\n\n assertWrappedQueryRef(queryRef);\n\n const internalQueryRef = queryRef && unwrapQueryRef(queryRef);\n\n if (queryRef && internalQueryRef?.didChangeOptions(watchQueryOptions)) {\n const promise = internalQueryRef.applyOptions(watchQueryOptions);\n updateWrappedQueryRef(queryRef, promise);\n }\n\n const calledDuringRender = useRenderGuard();\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(\n (options) => {\n if (!internalQueryRef) {\n throw new Error(\n \"The query has not been loaded. Please load the query.\"\n );\n }\n\n const promise = internalQueryRef.fetchMore(\n options as FetchMoreQueryOptions<TVariables, TData>\n );\n\n setQueryRef(wrapQueryRef(internalQueryRef));\n\n return promise;\n },\n [internalQueryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = React.useCallback(\n (options) => {\n if (!internalQueryRef) {\n throw new Error(\n \"The query has not been loaded. Please load the query.\"\n );\n }\n\n const promise = internalQueryRef.refetch(options);\n\n setQueryRef(wrapQueryRef(internalQueryRef));\n\n return promise;\n },\n [internalQueryRef]\n );\n\n const loadQuery: LoadQueryFunction<TVariables> = React.useCallback(\n (...args) => {\n invariant(\n !calledDuringRender(),\n \"useLoadableQuery: 'loadQuery' should not be called during render. To start a query during render, use the 'useBackgroundQuery' hook.\"\n );\n\n const [variables] = args;\n\n const cacheKey: CacheKey = [\n query,\n canonicalStringify(variables),\n ...([] as any[]).concat(queryKey),\n ];\n\n const queryRef = suspenseCache.getQueryRef(cacheKey, () =>\n client.watchQuery({\n ...watchQueryOptions,\n variables,\n } as WatchQueryOptions<any, any>)\n );\n\n setQueryRef(wrapQueryRef(queryRef));\n },\n [\n query,\n queryKey,\n suspenseCache,\n watchQueryOptions,\n calledDuringRender,\n client,\n ]\n );\n\n const reset: ResetFunction = React.useCallback(() => {\n setQueryRef(null);\n }, []);\n\n return [loadQuery, queryRef, { fetchMore, refetch, reset }];\n}\n"]}
|
|
@@ -5,6 +5,7 @@ import { equal } from "@wry/equality";
|
|
|
5
5
|
import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
|
6
6
|
import { ApolloError } from "../../errors/index.js";
|
|
7
7
|
import { useApolloClient } from "./useApolloClient.js";
|
|
8
|
+
import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js";
|
|
8
9
|
/**
|
|
9
10
|
*
|
|
10
11
|
*
|
|
@@ -68,11 +69,9 @@ export function useMutation(mutation, options) {
|
|
|
68
69
|
mutation: mutation,
|
|
69
70
|
options: options,
|
|
70
71
|
});
|
|
71
|
-
|
|
72
|
-
// higher-order components.
|
|
73
|
-
{
|
|
72
|
+
useIsomorphicLayoutEffect(function () {
|
|
74
73
|
Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
|
75
|
-
}
|
|
74
|
+
});
|
|
76
75
|
var execute = React.useCallback(function (executeOptions) {
|
|
77
76
|
if (executeOptions === void 0) { executeOptions = {}; }
|
|
78
77
|
var _a = ref.current, options = _a.options, mutation = _a.mutation;
|
|
@@ -147,15 +146,20 @@ export function useMutation(mutation, options) {
|
|
|
147
146
|
}, []);
|
|
148
147
|
var reset = React.useCallback(function () {
|
|
149
148
|
if (ref.current.isMounted) {
|
|
150
|
-
var result_3 = {
|
|
149
|
+
var result_3 = {
|
|
150
|
+
called: false,
|
|
151
|
+
loading: false,
|
|
152
|
+
client: ref.current.client,
|
|
153
|
+
};
|
|
151
154
|
Object.assign(ref.current, { mutationId: 0, result: result_3 });
|
|
152
155
|
setResult(result_3);
|
|
153
156
|
}
|
|
154
157
|
}, []);
|
|
155
158
|
React.useEffect(function () {
|
|
156
|
-
|
|
159
|
+
var current = ref.current;
|
|
160
|
+
current.isMounted = true;
|
|
157
161
|
return function () {
|
|
158
|
-
|
|
162
|
+
current.isMounted = false;
|
|
159
163
|
};
|
|
160
164
|
}, []);
|
|
161
165
|
return [execute, __assign({ reset: reset }, result)];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAiBjC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OAKC;IAED,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,QAAQ,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAA,KAAsB,KAAK,CAAC,QAAQ,CAAgC;QACxE,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,KAAK;QACd,MAAM,QAAA;KACP,CAAC,EAJK,MAAM,QAAA,EAAE,SAAS,QAItB,CAAC;IAEH,IAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC;QACvB,MAAM,QAAA;QACN,UAAU,EAAE,CAAC;QACb,SAAS,EAAE,IAAI;QACf,MAAM,QAAA;QACN,QAAQ,UAAA;QACR,OAAO,SAAA;KACR,CAAC,CAAC;IAEH,wEAAwE;IACxE,2BAA2B;IAC3B,CAAC;QACC,MA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AAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type { DocumentNode } from \"graphql\";\nimport type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport type {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n NoInfer,\n} from \"../types/types.js\";\n\nimport type {\n ApolloCache,\n DefaultContext,\n MutationOptions,\n OperationVariables,\n} from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport { equal } from \"@wry/equality\";\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n\n/**\n *\n *\n * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.\n *\n * @example\n * ```jsx\n * import { gql, useMutation } from '@apollo/client';\n *\n * const ADD_TODO = gql`\n * mutation AddTodo($type: String!) {\n * addTodo(type: $type) {\n * id\n * type\n * }\n * }\n * `;\n *\n * function AddTodo() {\n * let input;\n * const [addTodo, { data }] = useMutation(ADD_TODO);\n *\n * return (\n * <div>\n * <form\n * onSubmit={e => {\n * e.preventDefault();\n * addTodo({ variables: { type: input.value } });\n * input.value = '';\n * }}\n * >\n * <input\n * ref={node => {\n * input = node;\n * }}\n * />\n * <button type=\"submit\">Add Todo</button>\n * </form>\n * </div>\n * );\n * }\n * ```\n * @since 3.0.0\n * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.\n * @param options - Options to control how the mutation is executed.\n * @returns A tuple in the form of `[mutate, result]`\n */\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>,\n TContext,\n TCache\n >\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = React.useState<Omit<MutationResult, \"reset\">>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = React.useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client, options, mutation });\n }\n\n const execute = React.useCallback(\n (\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const { options, mutation } = ref.current;\n const baseOptions = { ...options, mutation };\n const client = executeOptions.client || ref.current.client;\n\n if (\n !ref.current.result.loading &&\n !baseOptions.ignoreResults &&\n ref.current.isMounted\n ) {\n setResult(\n (ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n })\n );\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(baseOptions, executeOptions);\n\n return client\n .mutate(clientOptions as MutationOptions<TData, OperationVariables>)\n .then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0 ?\n new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (error && onError) {\n onError(\n error,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n }\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onCompleted =\n executeOptions.onCompleted || ref.current.options?.onCompleted;\n\n if (!error) {\n onCompleted?.(\n response.data!,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n }\n\n return response;\n })\n .catch((error) => {\n if (mutationId === ref.current.mutationId && ref.current.isMounted) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (onError) {\n onError(\n error,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n },\n []\n );\n\n const reset = React.useCallback(() => {\n if (ref.current.isMounted) {\n const result = { called: false, loading: false, client };\n Object.assign(ref.current, { mutationId: 0, result });\n setResult(result);\n }\n }, []);\n\n React.useEffect(() => {\n ref.current.isMounted = true;\n\n return () => {\n ref.current.isMounted = false;\n };\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAiBjC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AAEpF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OAKC;IAED,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,QAAQ,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAA,KAAsB,KAAK,CAAC,QAAQ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;YACF,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,UAAA,EAAE,CAAC,CAAC;YACtD,SAAS,CAAC,QAAM,CAAC,CAAC;QACpB,CAAC;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,KAAK,CAAC,SAAS,CAAC;QACd,IAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC;QAC5B,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;QAEzB,OAAO;YACL,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import * as React from \"rehackt\";\nimport type { DocumentNode } from \"graphql\";\nimport type { TypedDocumentNode } from \"@graphql-typed-document-node/core\";\nimport type {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n NoInfer,\n} from \"../types/types.js\";\n\nimport type {\n ApolloCache,\n DefaultContext,\n MutationOptions,\n OperationVariables,\n} from \"../../core/index.js\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport { equal } from \"@wry/equality\";\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useIsomorphicLayoutEffect } from \"./internal/useIsomorphicLayoutEffect.js\";\n\n/**\n *\n *\n * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.\n *\n * @example\n * ```jsx\n * import { gql, useMutation } from '@apollo/client';\n *\n * const ADD_TODO = gql`\n * mutation AddTodo($type: String!) {\n * addTodo(type: $type) {\n * id\n * type\n * }\n * }\n * `;\n *\n * function AddTodo() {\n * let input;\n * const [addTodo, { data }] = useMutation(ADD_TODO);\n *\n * return (\n * <div>\n * <form\n * onSubmit={e => {\n * e.preventDefault();\n * addTodo({ variables: { type: input.value } });\n * input.value = '';\n * }}\n * >\n * <input\n * ref={node => {\n * input = node;\n * }}\n * />\n * <button type=\"submit\">Add Todo</button>\n * </form>\n * </div>\n * );\n * }\n * ```\n * @since 3.0.0\n * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.\n * @param options - Options to control how the mutation is executed.\n * @returns A tuple in the form of `[mutate, result]`\n */\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<\n NoInfer<TData>,\n NoInfer<TVariables>,\n TContext,\n TCache\n >\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = React.useState<Omit<MutationResult, \"reset\">>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = React.useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n useIsomorphicLayoutEffect(() => {\n Object.assign(ref.current, { client, options, mutation });\n });\n\n const execute = React.useCallback(\n (\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const { options, mutation } = ref.current;\n const baseOptions = { ...options, mutation };\n const client = executeOptions.client || ref.current.client;\n\n if (\n !ref.current.result.loading &&\n !baseOptions.ignoreResults &&\n ref.current.isMounted\n ) {\n setResult(\n (ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n })\n );\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(baseOptions, executeOptions);\n\n return client\n .mutate(clientOptions as MutationOptions<TData, OperationVariables>)\n .then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0 ?\n new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (error && onError) {\n onError(\n error,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n }\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onCompleted =\n executeOptions.onCompleted || ref.current.options?.onCompleted;\n\n if (!error) {\n onCompleted?.(\n response.data!,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n }\n\n return response;\n })\n .catch((error) => {\n if (mutationId === ref.current.mutationId && ref.current.isMounted) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult((ref.current.result = result));\n }\n }\n\n const onError =\n executeOptions.onError || ref.current.options?.onError;\n\n if (onError) {\n onError(\n error,\n clientOptions as MutationOptions<TData, OperationVariables>\n );\n\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n },\n []\n );\n\n const reset = React.useCallback(() => {\n if (ref.current.isMounted) {\n const result = {\n called: false,\n loading: false,\n client: ref.current.client,\n };\n Object.assign(ref.current, { mutationId: 0, result });\n setResult(result);\n }\n }, []);\n\n React.useEffect(() => {\n const current = ref.current;\n current.isMounted = true;\n\n return () => {\n current.isMounted = false;\n };\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { OperationVariables, WatchQueryFetchPolicy } from "../../core/index.js";
|
|
2
2
|
import type { ApolloClient, ApolloQueryResult, DocumentNode, TypedDocumentNode } from "../../core/index.js";
|
|
3
|
-
import type { QueryHookOptions, QueryResult, NoInfer } from "../types/types.js";
|
|
3
|
+
import type { QueryHookOptions, QueryResult, ObservableQueryFields, NoInfer } from "../types/types.js";
|
|
4
4
|
import { useApolloClient } from "./useApolloClient.js";
|
|
5
5
|
/**
|
|
6
6
|
* A hook for executing queries in an Apollo application.
|
|
@@ -71,7 +71,7 @@ declare class InternalState<TData, TVariables extends OperationVariables> {
|
|
|
71
71
|
private onCompleted;
|
|
72
72
|
private onError;
|
|
73
73
|
private observable;
|
|
74
|
-
|
|
74
|
+
obsQueryFields: Omit<ObservableQueryFields<TData, TVariables>, "variables">;
|
|
75
75
|
private useObservableQuery;
|
|
76
76
|
private result;
|
|
77
77
|
private previousData;
|
package/react/hooks/useQuery.js
CHANGED
|
@@ -54,20 +54,27 @@ function _useQuery(query, options) {
|
|
|
54
54
|
return useInternalState(useApolloClient(options.client), query).useQuery(options);
|
|
55
55
|
}
|
|
56
56
|
export function useInternalState(client, query) {
|
|
57
|
-
var stateRef = React.useRef();
|
|
58
|
-
if (!stateRef.current ||
|
|
59
|
-
client !== stateRef.current.client ||
|
|
60
|
-
query !== stateRef.current.query) {
|
|
61
|
-
stateRef.current = new InternalState(client, query, stateRef.current);
|
|
62
|
-
}
|
|
63
|
-
var state = stateRef.current;
|
|
64
57
|
// By default, InternalState.prototype.forceUpdate is an empty function, but
|
|
65
58
|
// we replace it here (before anyone has had a chance to see this state yet)
|
|
66
59
|
// with a function that unconditionally forces an update, using the latest
|
|
67
|
-
// setTick function. Updating this state by calling state.forceUpdate
|
|
68
|
-
// only way we trigger React component updates
|
|
69
|
-
|
|
70
|
-
|
|
60
|
+
// setTick function. Updating this state by calling state.forceUpdate or the
|
|
61
|
+
// uSES notification callback are the only way we trigger React component updates.
|
|
62
|
+
var forceUpdateState = React.useReducer(function (tick) { return tick + 1; }, 0)[1];
|
|
63
|
+
function createInternalState(previous) {
|
|
64
|
+
return Object.assign(new InternalState(client, query, previous), {
|
|
65
|
+
forceUpdateState: forceUpdateState,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
var _a = React.useState(createInternalState), state = _a[0], updateState = _a[1];
|
|
69
|
+
if (client !== state.client || query !== state.query) {
|
|
70
|
+
// If the client or query have changed, we need to create a new InternalState.
|
|
71
|
+
// This will trigger a re-render with the new state, but it will also continue
|
|
72
|
+
// to run the current render function to completion.
|
|
73
|
+
// Since we sometimes trigger some side-effects in the render function, we
|
|
74
|
+
// re-assign `state` to the new state to ensure that those side-effects are
|
|
75
|
+
// triggered with the new state.
|
|
76
|
+
updateState((state = createInternalState(state)));
|
|
77
|
+
}
|
|
71
78
|
return state;
|
|
72
79
|
}
|
|
73
80
|
var InternalState = /** @class */ (function () {
|
|
@@ -161,10 +168,14 @@ var InternalState = /** @class */ (function () {
|
|
|
161
168
|
// initialization, this.renderPromises is usually undefined (unless SSR is
|
|
162
169
|
// happening), but that's fine as long as it has been initialized that way,
|
|
163
170
|
// rather than left uninitialized.
|
|
171
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
164
172
|
this.renderPromises = React.useContext(getApolloContext()).renderPromises;
|
|
165
173
|
this.useOptions(options);
|
|
166
174
|
var obsQuery = this.useObservableQuery();
|
|
167
|
-
|
|
175
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
176
|
+
var result = useSyncExternalStore(
|
|
177
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
178
|
+
React.useCallback(function (handleStoreChange) {
|
|
168
179
|
if (_this.renderPromises) {
|
|
169
180
|
return function () { };
|
|
170
181
|
}
|
|
@@ -220,7 +231,9 @@ var InternalState = /** @class */ (function () {
|
|
|
220
231
|
// effectively passing this dependency array to that useEffect buried
|
|
221
232
|
// inside useSyncExternalStore, as desired.
|
|
222
233
|
obsQuery,
|
|
234
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
223
235
|
this.renderPromises,
|
|
236
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
224
237
|
this.client.disableNetworkFetches,
|
|
225
238
|
]), function () { return _this.getCurrentResult(); }, function () { return _this.getCurrentResult(); });
|
|
226
239
|
// TODO Remove this method when we remove support for options.partialRefetch.
|
|
@@ -368,6 +381,7 @@ var InternalState = /** @class */ (function () {
|
|
|
368
381
|
this.renderPromises.getSSRObservable(this.watchQueryOptions)) ||
|
|
369
382
|
this.observable || // Reuse this.observable if possible (and not SSR)
|
|
370
383
|
this.client.watchQuery(this.getObsQueryOptions()));
|
|
384
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
371
385
|
this.obsQueryFields = React.useMemo(function () { return ({
|
|
372
386
|
refetch: obsQuery.refetch.bind(obsQuery),
|
|
373
387
|
reobserve: obsQuery.reobserve.bind(obsQuery),
|