@apollo/client 3.10.3 → 3.10.5
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 +59 -0
- package/apollo-client.cjs +166 -134
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/cache/cache.cjs +81 -8
- package/cache/cache.cjs.map +1 -1
- package/cache/cache.cjs.native.js +81 -8
- package/cache/core/cache.js +8 -4
- package/cache/core/cache.js.map +1 -1
- package/core/ApolloClient.js +2 -1
- package/core/ApolloClient.js.map +1 -1
- package/core/ObservableQuery.js +1 -1
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.js +7 -11
- package/core/QueryInfo.js.map +1 -1
- package/core/core.cjs +5 -7
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +5 -7
- package/dev/dev.cjs +32 -27
- package/dev/dev.cjs.map +1 -1
- package/dev/dev.cjs.native.js +32 -27
- package/invariantErrorCodes.js +32 -26
- 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 +62 -34
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +62 -34
- package/react/hooks/internal/useRenderGuard.js +37 -12
- package/react/hooks/internal/useRenderGuard.js.map +1 -1
- package/react/hooks/useBackgroundQuery.d.ts +10 -13
- package/react/hooks/useBackgroundQuery.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 +3 -3
- package/react/hooks/useLoadableQuery.js +11 -3
- package/react/hooks/useLoadableQuery.js.map +1 -1
- package/react/hooks/useMutation.js +10 -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.d.ts +3 -3
- package/react/hooks/useQueryRefHandlers.js +7 -2
- package/react/hooks/useQueryRefHandlers.js.map +1 -1
- package/react/hooks/useReadQuery.d.ts +2 -2
- package/react/hooks/useReadQuery.js +7 -2
- 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/cache/QueryReference.d.ts +68 -6
- package/react/internal/cache/QueryReference.js +4 -0
- package/react/internal/cache/QueryReference.js.map +1 -1
- package/react/internal/index.d.ts +2 -2
- package/react/internal/index.js +1 -1
- package/react/internal/index.js.map +1 -1
- package/react/internal/internal.cjs +92 -0
- package/react/internal/internal.cjs.map +1 -1
- package/react/internal/internal.cjs.native.js +92 -0
- package/react/parser/index.js +5 -5
- package/react/parser/parser.cjs +5 -5
- package/react/parser/parser.cjs.map +1 -1
- package/react/parser/parser.cjs.native.js +5 -5
- package/react/query-preloader/createQueryPreloader.d.ts +6 -6
- package/react/query-preloader/createQueryPreloader.js.map +1 -1
- package/react/types/types.d.ts +1 -1
- package/react/types/types.js.map +1 -1
- package/testing/core/core.cjs +4 -4
- package/testing/core/core.cjs.map +1 -1
- package/testing/core/core.cjs.native.js +4 -4
- package/testing/core/mocking/mockLink.d.ts +8 -3
- package/testing/core/mocking/mockLink.js +5 -5
- 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/testing/matchers/toBeDisposed.js +4 -13
- package/testing/matchers/toBeDisposed.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/utilities/graphql/DocumentTransform.js +1 -1
- package/utilities/graphql/directives.js +4 -4
- package/utilities/graphql/fragments.js +3 -3
- package/utilities/graphql/getFromAST.js +8 -8
- package/utilities/graphql/storeUtils.js +1 -1
- package/utilities/graphql/transform.js +2 -2
- package/utilities/utilities.cjs +19 -19
- package/utilities/utilities.cjs.map +1 -1
- package/utilities/utilities.cjs.native.js +19 -19
- package/version.js +1 -1
|
@@ -95,15 +95,26 @@ function useDeepMemo(memoFn, deps) {
|
|
|
95
95
|
return ref.current.value;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return (_b = (_a = React__namespace.__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;
|
|
101
|
-
}
|
|
102
|
-
var RenderDispatcher = null;
|
|
98
|
+
var Ctx;
|
|
99
|
+
function noop() { }
|
|
103
100
|
function useRenderGuard() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
if (!Ctx) {
|
|
102
|
+
Ctx = React__namespace.createContext(null);
|
|
103
|
+
}
|
|
104
|
+
return React__namespace.useCallback(
|
|
105
|
+
function () {
|
|
106
|
+
var orig = console.error;
|
|
107
|
+
try {
|
|
108
|
+
console.error = noop;
|
|
109
|
+
React__namespace["useContext" ](Ctx);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
console.error = orig;
|
|
117
|
+
}
|
|
107
118
|
}, []);
|
|
108
119
|
}
|
|
109
120
|
|
|
@@ -148,14 +159,16 @@ function _useQuery(query, options) {
|
|
|
148
159
|
return useInternalState(useApolloClient(options.client), query).useQuery(options);
|
|
149
160
|
}
|
|
150
161
|
function useInternalState(client, query) {
|
|
151
|
-
var
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
162
|
+
var forceUpdateState = React__namespace.useReducer(function (tick) { return tick + 1; }, 0)[1];
|
|
163
|
+
function createInternalState(previous) {
|
|
164
|
+
return Object.assign(new InternalState(client, query, previous), {
|
|
165
|
+
forceUpdateState: forceUpdateState,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
var _a = React__namespace.useState(createInternalState), state = _a[0], updateState = _a[1];
|
|
169
|
+
if (client !== state.client || query !== state.query) {
|
|
170
|
+
updateState((state = createInternalState(state)));
|
|
156
171
|
}
|
|
157
|
-
var state = stateRef.current;
|
|
158
|
-
state.forceUpdateState = React__namespace.useReducer(function (tick) { return tick + 1; }, 0)[1];
|
|
159
172
|
return state;
|
|
160
173
|
}
|
|
161
174
|
var InternalState = (function () {
|
|
@@ -218,7 +231,8 @@ var InternalState = (function () {
|
|
|
218
231
|
this.renderPromises = React__namespace.useContext(context.getApolloContext()).renderPromises;
|
|
219
232
|
this.useOptions(options);
|
|
220
233
|
var obsQuery = this.useObservableQuery();
|
|
221
|
-
var result = useSyncExternalStore(
|
|
234
|
+
var result = useSyncExternalStore(
|
|
235
|
+
React__namespace.useCallback(function (handleStoreChange) {
|
|
222
236
|
if (_this.renderPromises) {
|
|
223
237
|
return function () { };
|
|
224
238
|
}
|
|
@@ -453,17 +467,15 @@ function useLazyQuery(query, options) {
|
|
|
453
467
|
var useQueryResult = internalState.useQuery(tslib.__assign(tslib.__assign({}, merged), { skip: !execOptionsRef.current }));
|
|
454
468
|
var initialFetchPolicy = useQueryResult.observable.options.initialFetchPolicy ||
|
|
455
469
|
internalState.getDefaultFetchPolicy();
|
|
456
|
-
var
|
|
457
|
-
called: !!execOptionsRef.current,
|
|
458
|
-
});
|
|
470
|
+
var forceUpdateState = internalState.forceUpdateState, obsQueryFields = internalState.obsQueryFields;
|
|
459
471
|
var eagerMethods = React__namespace.useMemo(function () {
|
|
460
472
|
var eagerMethods = {};
|
|
461
473
|
var _loop_1 = function (key) {
|
|
462
|
-
var method =
|
|
474
|
+
var method = obsQueryFields[key];
|
|
463
475
|
eagerMethods[key] = function () {
|
|
464
476
|
if (!execOptionsRef.current) {
|
|
465
477
|
execOptionsRef.current = Object.create(null);
|
|
466
|
-
|
|
478
|
+
forceUpdateState();
|
|
467
479
|
}
|
|
468
480
|
return method.apply(this, arguments);
|
|
469
481
|
};
|
|
@@ -473,8 +485,9 @@ function useLazyQuery(query, options) {
|
|
|
473
485
|
_loop_1(key);
|
|
474
486
|
}
|
|
475
487
|
return eagerMethods;
|
|
476
|
-
}, []);
|
|
477
|
-
|
|
488
|
+
}, [forceUpdateState, obsQueryFields]);
|
|
489
|
+
var called = !!execOptionsRef.current;
|
|
490
|
+
var result = React__namespace.useMemo(function () { return (tslib.__assign(tslib.__assign(tslib.__assign({}, useQueryResult), eagerMethods), { called: called })); }, [useQueryResult, eagerMethods, called]);
|
|
478
491
|
var execute = React__namespace.useCallback(function (executeOptions) {
|
|
479
492
|
execOptionsRef.current =
|
|
480
493
|
executeOptions ? tslib.__assign(tslib.__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
|
|
@@ -486,7 +499,7 @@ function useLazyQuery(query, options) {
|
|
|
486
499
|
.then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
|
|
487
500
|
promise.catch(function () { });
|
|
488
501
|
return promise;
|
|
489
|
-
}, []);
|
|
502
|
+
}, [eagerMethods, initialFetchPolicy, internalState]);
|
|
490
503
|
return [execute, result];
|
|
491
504
|
}
|
|
492
505
|
|
|
@@ -506,9 +519,9 @@ function useMutation(mutation, options) {
|
|
|
506
519
|
mutation: mutation,
|
|
507
520
|
options: options,
|
|
508
521
|
});
|
|
509
|
-
{
|
|
522
|
+
React__namespace.useLayoutEffect(function () {
|
|
510
523
|
Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
|
511
|
-
}
|
|
524
|
+
});
|
|
512
525
|
var execute = React__namespace.useCallback(function (executeOptions) {
|
|
513
526
|
if (executeOptions === void 0) { executeOptions = {}; }
|
|
514
527
|
var _a = ref.current, options = _a.options, mutation = _a.mutation;
|
|
@@ -582,15 +595,20 @@ function useMutation(mutation, options) {
|
|
|
582
595
|
}, []);
|
|
583
596
|
var reset = React__namespace.useCallback(function () {
|
|
584
597
|
if (ref.current.isMounted) {
|
|
585
|
-
var result_3 = {
|
|
598
|
+
var result_3 = {
|
|
599
|
+
called: false,
|
|
600
|
+
loading: false,
|
|
601
|
+
client: ref.current.client,
|
|
602
|
+
};
|
|
586
603
|
Object.assign(ref.current, { mutationId: 0, result: result_3 });
|
|
587
604
|
setResult(result_3);
|
|
588
605
|
}
|
|
589
606
|
}, []);
|
|
590
607
|
React__namespace.useEffect(function () {
|
|
591
|
-
|
|
608
|
+
var current = ref.current;
|
|
609
|
+
current.isMounted = true;
|
|
592
610
|
return function () {
|
|
593
|
-
|
|
611
|
+
current.isMounted = false;
|
|
594
612
|
};
|
|
595
613
|
}, []);
|
|
596
614
|
return [execute, tslib.__assign({ reset: reset }, result)];
|
|
@@ -846,11 +864,11 @@ function _useSuspenseQuery(query, options) {
|
|
|
846
864
|
};
|
|
847
865
|
}, [queryRef.result]);
|
|
848
866
|
var result = fetchPolicy === "standby" ? skipResult : __use(promise);
|
|
849
|
-
var fetchMore = React__namespace.useCallback(
|
|
867
|
+
var fetchMore = React__namespace.useCallback(function (options) {
|
|
850
868
|
var promise = queryRef.fetchMore(options);
|
|
851
869
|
setPromise([queryRef.key, queryRef.promise]);
|
|
852
870
|
return promise;
|
|
853
|
-
}
|
|
871
|
+
}, [queryRef]);
|
|
854
872
|
var refetch = React__namespace.useCallback(function (variables) {
|
|
855
873
|
var promise = queryRef.refetch(variables);
|
|
856
874
|
setPromise([queryRef.key, queryRef.promise]);
|
|
@@ -975,6 +993,7 @@ function useLoadableQuery(query, options) {
|
|
|
975
993
|
var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
|
|
976
994
|
var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
|
|
977
995
|
var _b = React__namespace.useState(null), queryRef = _b[0], setQueryRef = _b[1];
|
|
996
|
+
internal.assertWrappedQueryRef(queryRef);
|
|
978
997
|
var internalQueryRef = queryRef && internal.unwrapQueryRef(queryRef);
|
|
979
998
|
if (queryRef && (internalQueryRef === null || internalQueryRef === void 0 ? void 0 : internalQueryRef.didChangeOptions(watchQueryOptions))) {
|
|
980
999
|
var promise = internalQueryRef.applyOptions(watchQueryOptions);
|
|
@@ -1012,10 +1031,17 @@ function useLoadableQuery(query, options) {
|
|
|
1012
1031
|
return client.watchQuery(tslib.__assign(tslib.__assign({}, watchQueryOptions), { variables: variables }));
|
|
1013
1032
|
});
|
|
1014
1033
|
setQueryRef(internal.wrapQueryRef(queryRef));
|
|
1015
|
-
}, [
|
|
1034
|
+
}, [
|
|
1035
|
+
query,
|
|
1036
|
+
queryKey,
|
|
1037
|
+
suspenseCache,
|
|
1038
|
+
watchQueryOptions,
|
|
1039
|
+
calledDuringRender,
|
|
1040
|
+
client,
|
|
1041
|
+
]);
|
|
1016
1042
|
var reset = React__namespace.useCallback(function () {
|
|
1017
1043
|
setQueryRef(null);
|
|
1018
|
-
}, [
|
|
1044
|
+
}, []);
|
|
1019
1045
|
return [loadQuery, queryRef, { fetchMore: fetchMore, refetch: refetch, reset: reset }];
|
|
1020
1046
|
}
|
|
1021
1047
|
|
|
@@ -1026,6 +1052,7 @@ function useQueryRefHandlers(queryRef) {
|
|
|
1026
1052
|
: useApolloClient())(queryRef);
|
|
1027
1053
|
}
|
|
1028
1054
|
function _useQueryRefHandlers(queryRef) {
|
|
1055
|
+
internal.assertWrappedQueryRef(queryRef);
|
|
1029
1056
|
var _a = React__namespace.useState(queryRef), previousQueryRef = _a[0], setPreviousQueryRef = _a[1];
|
|
1030
1057
|
var _b = React__namespace.useState(queryRef), wrappedQueryRef = _b[0], setWrappedQueryRef = _b[1];
|
|
1031
1058
|
var internalQueryRef = internal.unwrapQueryRef(queryRef);
|
|
@@ -1056,6 +1083,7 @@ function useReadQuery(queryRef) {
|
|
|
1056
1083
|
: useApolloClient())(queryRef);
|
|
1057
1084
|
}
|
|
1058
1085
|
function _useReadQuery(queryRef) {
|
|
1086
|
+
internal.assertWrappedQueryRef(queryRef);
|
|
1059
1087
|
var internalQueryRef = React__namespace.useMemo(function () { return internal.unwrapQueryRef(queryRef); }, [queryRef]);
|
|
1060
1088
|
var getPromise = React__namespace.useCallback(function () { return internal.getWrappedPromise(queryRef); }, [queryRef]);
|
|
1061
1089
|
if (internalQueryRef.disposed) {
|
|
@@ -1068,7 +1096,7 @@ function _useReadQuery(queryRef) {
|
|
|
1068
1096
|
internal.updateWrappedQueryRef(queryRef, promise);
|
|
1069
1097
|
forceUpdate();
|
|
1070
1098
|
});
|
|
1071
|
-
}, [internalQueryRef]), getPromise, getPromise);
|
|
1099
|
+
}, [internalQueryRef, queryRef]), getPromise, getPromise);
|
|
1072
1100
|
var result = __use(promise);
|
|
1073
1101
|
return React__namespace.useMemo(function () {
|
|
1074
1102
|
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"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DocumentNode, OperationVariables, TypedDocumentNode } from "../../core/index.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { QueryRef } from "../internal/index.js";
|
|
3
3
|
import type { BackgroundQueryHookOptions, NoInfer } from "../types/types.js";
|
|
4
4
|
import type { FetchMoreFunction, RefetchFunction } from "./useSuspenseQuery.js";
|
|
5
5
|
import type { DeepPartial } from "../../utilities/index.js";
|
|
@@ -10,54 +10,51 @@ export type UseBackgroundQueryResult<TData = unknown, TVariables extends Operati
|
|
|
10
10
|
};
|
|
11
11
|
type BackgroundQueryHookOptionsNoInfer<TData, TVariables extends OperationVariables> = BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>;
|
|
12
12
|
export declare function useBackgroundQuery<TData, TVariables extends OperationVariables, TOptions extends Omit<BackgroundQueryHookOptions<TData>, "variables">>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions): [
|
|
13
|
-
(
|
|
13
|
+
(QueryRef<TOptions["errorPolicy"] extends "ignore" | "all" ? TOptions["returnPartialData"] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions["returnPartialData"] extends true ? DeepPartial<TData> : TData, TVariables> | (TOptions["skip"] extends boolean ? undefined : never)),
|
|
14
14
|
UseBackgroundQueryResult<TData, TVariables>
|
|
15
15
|
];
|
|
16
16
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
17
17
|
returnPartialData: true;
|
|
18
18
|
errorPolicy: "ignore" | "all";
|
|
19
19
|
}): [
|
|
20
|
-
|
|
20
|
+
QueryRef<DeepPartial<TData> | undefined, TVariables>,
|
|
21
21
|
UseBackgroundQueryResult<TData, TVariables>
|
|
22
22
|
];
|
|
23
23
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
24
24
|
errorPolicy: "ignore" | "all";
|
|
25
25
|
}): [
|
|
26
|
-
|
|
26
|
+
QueryRef<TData | undefined, TVariables>,
|
|
27
27
|
UseBackgroundQueryResult<TData, TVariables>
|
|
28
28
|
];
|
|
29
29
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
30
30
|
skip: boolean;
|
|
31
31
|
returnPartialData: true;
|
|
32
32
|
}): [
|
|
33
|
-
|
|
33
|
+
QueryRef<DeepPartial<TData>, TVariables> | undefined,
|
|
34
34
|
UseBackgroundQueryResult<TData, TVariables>
|
|
35
35
|
];
|
|
36
36
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
37
37
|
returnPartialData: true;
|
|
38
38
|
}): [
|
|
39
|
-
|
|
39
|
+
QueryRef<DeepPartial<TData>, TVariables>,
|
|
40
40
|
UseBackgroundQueryResult<TData, TVariables>
|
|
41
41
|
];
|
|
42
42
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
43
43
|
skip: boolean;
|
|
44
44
|
}): [
|
|
45
|
-
|
|
46
|
-
UseBackgroundQueryResult<TData, TVariables>
|
|
47
|
-
];
|
|
48
|
-
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>): [
|
|
49
|
-
QueryReference<TData, TVariables>,
|
|
45
|
+
QueryRef<TData, TVariables> | undefined,
|
|
50
46
|
UseBackgroundQueryResult<TData, TVariables>
|
|
51
47
|
];
|
|
48
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>): [QueryRef<TData, TVariables>, UseBackgroundQueryResult<TData, TVariables>];
|
|
52
49
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken): [undefined, UseBackgroundQueryResult<TData, TVariables>];
|
|
53
50
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken | (BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
54
51
|
returnPartialData: true;
|
|
55
52
|
})): [
|
|
56
|
-
|
|
53
|
+
QueryRef<DeepPartial<TData>, TVariables> | undefined,
|
|
57
54
|
UseBackgroundQueryResult<TData, TVariables>
|
|
58
55
|
];
|
|
59
56
|
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SkipToken | BackgroundQueryHookOptionsNoInfer<TData, TVariables>): [
|
|
60
|
-
|
|
57
|
+
QueryRef<TData, TVariables> | undefined,
|
|
61
58
|
UseBackgroundQueryResult<TData, TVariables>
|
|
62
59
|
];
|
|
63
60
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBackgroundQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAQjC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,YAAY,GACb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAwJ1D,MAAM,UAAU,kBAAkB,CAIhC,KAA0D,EAC1D,OAG8E;IAH9E,wBAAA,EAAA,UAG2D,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IAK9E,OAAO,QAAQ,CACb,oBAAoB,EACpB,mBAAmB,EACnB,eAAe,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAC1E,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACpB,CAAC;AAED,SAAS,mBAAmB,CAI1B,KAA0D,EAC1D,OAGwD;IAKxD,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,KA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TypedDocumentNode,\n WatchQueryOptions,\n} from \"../../core/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport {\n getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryReference } from \"../internal/index.js\";\nimport type { BackgroundQueryHookOptions, NoInfer } from \"../types/types.js\";\nimport { wrapHook } from \"./internal/index.js\";\nimport { useWatchQueryOptions } from \"./useSuspenseQuery.js\";\nimport type { FetchMoreFunction, RefetchFunction } from \"./useSuspenseQuery.js\";\nimport { canonicalStringify } from \"../../cache/index.js\";\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport type { SkipToken } from \"./constants.js\";\n\nexport type UseBackgroundQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n> = {\n fetchMore: FetchMoreFunction<TData, TVariables>;\n refetch: RefetchFunction<TData, TVariables>;\n};\n\ntype BackgroundQueryHookOptionsNoInfer<\n TData,\n TVariables extends OperationVariables,\n> = BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>;\n\nexport function useBackgroundQuery<\n TData,\n TVariables extends OperationVariables,\n TOptions extends Omit<BackgroundQueryHookOptions<TData>, \"variables\">,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions\n): [\n (\n | QueryReference<\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 | (TOptions[\"skip\"] extends boolean ? undefined : never)\n ),\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryReference<DeepPartial<TData> | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryReference<TData | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n returnPartialData: true;\n }\n): [\n QueryReference<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n }\n): [\n QueryReference<DeepPartial<TData>, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n }\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: SkipToken\n): [undefined, UseBackgroundQueryResult<TData, TVariables>];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | SkipToken\n | (BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n })\n): [\n QueryReference<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: SkipToken | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables> = Object.create(null)\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n return wrapHook(\n \"useBackgroundQuery\",\n _useBackgroundQuery,\n useApolloClient(typeof options === \"object\" ? options.client : undefined)\n )(query, options);\n}\n\nfunction _useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryReference<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n const client = useApolloClient(options.client);\n const suspenseCache = getSuspenseCache(client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { fetchPolicy, variables } = watchQueryOptions;\n const { queryKey = [] } = options;\n\n // This ref tracks the first time query execution is enabled to determine\n // whether to return a query ref or `undefined`. When initialized\n // in a skipped state (either via `skip: true` or `skipToken`) we return\n // `undefined` for the `queryRef` until the query has been enabled. Once\n // enabled, a query ref is always returned regardless of whether the query is\n // skipped again later.\n const didFetchResult = React.useRef(fetchPolicy !== \"standby\");\n didFetchResult.current ||= fetchPolicy !== \"standby\";\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(watchQueryOptions as WatchQueryOptions<any, any>)\n );\n\n const [wrappedQueryRef, setWrappedQueryRef] = React.useState(\n wrapQueryRef(queryRef)\n );\n if (unwrapQueryRef(wrappedQueryRef) !== queryRef) {\n setWrappedQueryRef(wrapQueryRef(queryRef));\n }\n if (queryRef.didChangeOptions(watchQueryOptions)) {\n const promise = queryRef.applyOptions(watchQueryOptions);\n updateWrappedQueryRef(wrappedQueryRef, promise);\n }\n\n // This prevents issues where rerendering useBackgroundQuery after the\n // queryRef has been disposed would cause the hook to return a new queryRef\n // instance since disposal also removes it from the suspense cache. We add\n // the queryRef back in the suspense cache so that the next render will reuse\n // this queryRef rather than initializing a new instance.\n React.useEffect(() => {\n // Since the queryRef is disposed async via `setTimeout`, we have to wait a\n // tick before checking it and adding back to the suspense cache.\n const id = setTimeout(() => {\n if (queryRef.disposed) {\n suspenseCache.add(cacheKey, queryRef);\n }\n });\n\n return () => clearTimeout(id);\n // Omitting the deps is intentional. This avoids stale closures and the\n // conditional ensures we aren't running the logic on each render.\n });\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(\n (options) => {\n const promise = queryRef.fetchMore(options as FetchMoreQueryOptions<any>);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = React.useCallback(\n (variables) => {\n const promise = queryRef.refetch(variables);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n React.useEffect(() => queryRef.softRetain(), [queryRef]);\n\n return [\n didFetchResult.current ? wrappedQueryRef : void 0,\n { fetchMore, refetch },\n ];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useBackgroundQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAQjC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,YAAY,GACb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAqJ1D,MAAM,UAAU,kBAAkB,CAIhC,KAA0D,EAC1D,OAG8E;IAH9E,wBAAA,EAAA,UAG2D,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IAK9E,OAAO,QAAQ,CACb,oBAAoB,EACpB,mBAAmB,EACnB,eAAe,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAC1E,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACpB,CAAC;AAED,SAAS,mBAAmB,CAI1B,KAA0D,EAC1D,OAGwD;IAKxD,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAM,aAAa,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/C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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 getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryRef } from \"../internal/index.js\";\nimport type { BackgroundQueryHookOptions, NoInfer } from \"../types/types.js\";\nimport { wrapHook } from \"./internal/index.js\";\nimport { useWatchQueryOptions } from \"./useSuspenseQuery.js\";\nimport type { FetchMoreFunction, RefetchFunction } from \"./useSuspenseQuery.js\";\nimport { canonicalStringify } from \"../../cache/index.js\";\nimport type { DeepPartial } from \"../../utilities/index.js\";\nimport type { SkipToken } from \"./constants.js\";\n\nexport type UseBackgroundQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n> = {\n fetchMore: FetchMoreFunction<TData, TVariables>;\n refetch: RefetchFunction<TData, TVariables>;\n};\n\ntype BackgroundQueryHookOptionsNoInfer<\n TData,\n TVariables extends OperationVariables,\n> = BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>;\n\nexport function useBackgroundQuery<\n TData,\n TVariables extends OperationVariables,\n TOptions extends Omit<BackgroundQueryHookOptions<TData>, \"variables\">,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions\n): [\n (\n | QueryRef<\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 | (TOptions[\"skip\"] extends boolean ? undefined : never)\n ),\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryRef<DeepPartial<TData> | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n errorPolicy: \"ignore\" | \"all\";\n }\n): [\n QueryRef<TData | undefined, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n returnPartialData: true;\n }\n): [\n QueryRef<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n }\n): [\n QueryRef<DeepPartial<TData>, TVariables>,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n skip: boolean;\n }\n): [\n QueryRef<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [QueryRef<TData, TVariables>, UseBackgroundQueryResult<TData, TVariables>];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: SkipToken\n): [undefined, UseBackgroundQueryResult<TData, TVariables>];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | SkipToken\n | (BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {\n returnPartialData: true;\n })\n): [\n QueryRef<DeepPartial<TData>, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: SkipToken | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryRef<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n];\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables> = Object.create(null)\n): [\n QueryRef<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n return wrapHook(\n \"useBackgroundQuery\",\n _useBackgroundQuery,\n useApolloClient(typeof options === \"object\" ? options.client : undefined)\n )(query, options);\n}\n\nfunction _useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options:\n | (SkipToken &\n Partial<BackgroundQueryHookOptionsNoInfer<TData, TVariables>>)\n | BackgroundQueryHookOptionsNoInfer<TData, TVariables>\n): [\n QueryRef<TData, TVariables> | undefined,\n UseBackgroundQueryResult<TData, TVariables>,\n] {\n const client = useApolloClient(options.client);\n const suspenseCache = getSuspenseCache(client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { fetchPolicy, variables } = watchQueryOptions;\n const { queryKey = [] } = options;\n\n // This ref tracks the first time query execution is enabled to determine\n // whether to return a query ref or `undefined`. When initialized\n // in a skipped state (either via `skip: true` or `skipToken`) we return\n // `undefined` for the `queryRef` until the query has been enabled. Once\n // enabled, a query ref is always returned regardless of whether the query is\n // skipped again later.\n const didFetchResult = React.useRef(fetchPolicy !== \"standby\");\n didFetchResult.current ||= fetchPolicy !== \"standby\";\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(watchQueryOptions as WatchQueryOptions<any, any>)\n );\n\n const [wrappedQueryRef, setWrappedQueryRef] = React.useState(\n wrapQueryRef(queryRef)\n );\n if (unwrapQueryRef(wrappedQueryRef) !== queryRef) {\n setWrappedQueryRef(wrapQueryRef(queryRef));\n }\n if (queryRef.didChangeOptions(watchQueryOptions)) {\n const promise = queryRef.applyOptions(watchQueryOptions);\n updateWrappedQueryRef(wrappedQueryRef, promise);\n }\n\n // This prevents issues where rerendering useBackgroundQuery after the\n // queryRef has been disposed would cause the hook to return a new queryRef\n // instance since disposal also removes it from the suspense cache. We add\n // the queryRef back in the suspense cache so that the next render will reuse\n // this queryRef rather than initializing a new instance.\n React.useEffect(() => {\n // Since the queryRef is disposed async via `setTimeout`, we have to wait a\n // tick before checking it and adding back to the suspense cache.\n const id = setTimeout(() => {\n if (queryRef.disposed) {\n suspenseCache.add(cacheKey, queryRef);\n }\n });\n\n return () => clearTimeout(id);\n // Omitting the deps is intentional. This avoids stale closures and the\n // conditional ensures we aren't running the logic on each render.\n });\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = React.useCallback(\n (options) => {\n const promise = queryRef.fetchMore(options as FetchMoreQueryOptions<any>);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = React.useCallback(\n (variables) => {\n const promise = queryRef.refetch(variables);\n\n setWrappedQueryRef(wrapQueryRef(queryRef));\n\n return promise;\n },\n [queryRef]\n );\n\n React.useEffect(() => queryRef.softRetain(), [queryRef]);\n\n return [\n didFetchResult.current ? wrappedQueryRef : void 0,\n { fetchMore, refetch },\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;AAC/D,IAAM,aAAa,GAAG;IACpB,SAAS;IACT,WAAW;IACX,WAAW;IACX,aAAa;IACb,cAAc;IACd,iBAAiB;CACT,CAAC;AAEX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;G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,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"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DocumentNode, OperationVariables, TypedDocumentNode } from "../../core/index.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { QueryRef } from "../internal/index.js";
|
|
3
3
|
import type { LoadableQueryHookOptions } from "../types/types.js";
|
|
4
4
|
import type { FetchMoreFunction, RefetchFunction } from "./useSuspenseQuery.js";
|
|
5
5
|
import type { DeepPartial, OnlyRequiredProperties } from "../../utilities/index.js";
|
|
@@ -7,8 +7,8 @@ export type LoadQueryFunction<TVariables extends OperationVariables> = (...args:
|
|
|
7
7
|
type ResetFunction = () => void;
|
|
8
8
|
export type UseLoadableQueryResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> = [
|
|
9
9
|
loadQuery: LoadQueryFunction<TVariables>,
|
|
10
|
-
queryRef:
|
|
11
|
-
{
|
|
10
|
+
queryRef: QueryRef<TData, TVariables> | null,
|
|
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
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __assign, __spreadArray } from "tslib";
|
|
2
2
|
import * as React from "rehackt";
|
|
3
3
|
import { useApolloClient } from "./useApolloClient.js";
|
|
4
|
-
import { getSuspenseCache, unwrapQueryRef, updateWrappedQueryRef, wrapQueryRef, } from "../internal/index.js";
|
|
4
|
+
import { assertWrappedQueryRef, getSuspenseCache, unwrapQueryRef, updateWrappedQueryRef, wrapQueryRef, } from "../internal/index.js";
|
|
5
5
|
import { useRenderGuard } from "./internal/index.js";
|
|
6
6
|
import { useWatchQueryOptions } from "./useSuspenseQuery.js";
|
|
7
7
|
import { canonicalStringify } from "../../cache/index.js";
|
|
@@ -13,6 +13,7 @@ export function useLoadableQuery(query, options) {
|
|
|
13
13
|
var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
|
|
14
14
|
var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
|
|
15
15
|
var _b = React.useState(null), queryRef = _b[0], setQueryRef = _b[1];
|
|
16
|
+
assertWrappedQueryRef(queryRef);
|
|
16
17
|
var internalQueryRef = queryRef && unwrapQueryRef(queryRef);
|
|
17
18
|
if (queryRef && (internalQueryRef === null || internalQueryRef === void 0 ? void 0 : internalQueryRef.didChangeOptions(watchQueryOptions))) {
|
|
18
19
|
var promise = internalQueryRef.applyOptions(watchQueryOptions);
|
|
@@ -50,10 +51,17 @@ export function useLoadableQuery(query, options) {
|
|
|
50
51
|
return client.watchQuery(__assign(__assign({}, watchQueryOptions), { variables: variables }));
|
|
51
52
|
});
|
|
52
53
|
setQueryRef(wrapQueryRef(queryRef));
|
|
53
|
-
}, [
|
|
54
|
+
}, [
|
|
55
|
+
query,
|
|
56
|
+
queryKey,
|
|
57
|
+
suspenseCache,
|
|
58
|
+
watchQueryOptions,
|
|
59
|
+
calledDuringRender,
|
|
60
|
+
client,
|
|
61
|
+
]);
|
|
54
62
|
var reset = React.useCallback(function () {
|
|
55
63
|
setQueryRef(null);
|
|
56
|
-
}, [
|
|
64
|
+
}, []);
|
|
57
65
|
return [loadQuery, queryRef, { fetchMore: fetchMore, refetch: refetch, reset: reset }];
|
|
58
66
|
}
|
|
59
67
|
//# sourceMappingURL=useLoadableQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLoadableQuery.js","sourceRoot":"","sources":["../../../src/react/h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AAM,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 getSuspenseCache,\n unwrapQueryRef,\n updateWrappedQueryRef,\n wrapQueryRef,\n} from \"../internal/index.js\";\nimport type { CacheKey, QueryReference } 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: QueryReference<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<QueryReference<\n TData,\n TVariables\n > | null>(null);\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;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AC,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"]}
|