@apollo/client 3.7.2 → 3.8.0-alpha.0
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/apollo-client.cjs +250 -30
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/config/jest/setup.d.ts +1 -1
- package/config/jest/setup.d.ts.map +1 -1
- package/config/jest/setup.js +1 -0
- package/config/jest/setup.js.map +1 -1
- package/core/ObservableQuery.d.ts.map +1 -1
- package/core/ObservableQuery.js +3 -2
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.d.ts.map +1 -1
- package/core/QueryInfo.js +6 -2
- package/core/QueryInfo.js.map +1 -1
- package/core/core.cjs +10 -5
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +10 -5
- package/core/watchQueryOptions.d.ts +1 -0
- package/core/watchQueryOptions.d.ts.map +1 -1
- package/core/watchQueryOptions.js.map +1 -1
- package/invariantErrorCodes.js +35 -23
- package/package.json +17 -3
- package/react/cache/SuspenseCache.d.ts +18 -0
- package/react/cache/SuspenseCache.d.ts.map +1 -0
- package/react/cache/SuspenseCache.js +49 -0
- package/react/cache/SuspenseCache.js.map +1 -0
- package/react/cache/index.d.ts +2 -0
- package/react/cache/index.d.ts.map +1 -0
- package/react/cache/index.js +2 -0
- package/react/cache/index.js.map +1 -0
- package/react/context/ApolloContext.d.ts +2 -0
- package/react/context/ApolloContext.d.ts.map +1 -1
- package/react/context/ApolloContext.js.map +1 -1
- package/react/context/ApolloProvider.d.ts +2 -0
- package/react/context/ApolloProvider.d.ts.map +1 -1
- package/react/context/ApolloProvider.js +4 -1
- package/react/context/ApolloProvider.js.map +1 -1
- package/react/context/context.cjs +4 -1
- package/react/context/context.cjs.map +1 -1
- package/react/context/context.cjs.native.js +4 -1
- package/react/hooks/compareResults.d.ts +3 -0
- package/react/hooks/compareResults.d.ts.map +1 -0
- package/react/hooks/compareResults.js +63 -0
- package/react/hooks/compareResults.js.map +1 -0
- package/react/hooks/hooks.cjs +167 -3
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +167 -3
- package/react/hooks/index.d.ts +1 -0
- package/react/hooks/index.d.ts.map +1 -1
- package/react/hooks/index.js +1 -0
- package/react/hooks/index.js.map +1 -1
- package/react/hooks/internal/index.d.ts +2 -0
- package/react/hooks/internal/index.d.ts.map +1 -0
- package/react/hooks/internal/index.js +2 -0
- package/react/hooks/internal/index.js.map +1 -0
- package/react/hooks/internal/useDeepMemo.d.ts +3 -0
- package/react/hooks/internal/useDeepMemo.d.ts.map +1 -0
- package/react/hooks/internal/useDeepMemo.js +10 -0
- package/react/hooks/internal/useDeepMemo.js.map +1 -0
- package/react/hooks/useQuery.d.ts.map +1 -1
- package/react/hooks/useQuery.js +5 -3
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useSuspenseCache.d.ts +2 -0
- package/react/hooks/useSuspenseCache.d.ts.map +1 -0
- package/react/hooks/useSuspenseCache.js +10 -0
- package/react/hooks/useSuspenseCache.js.map +1 -0
- package/react/hooks/useSuspenseQuery.d.ts +10 -0
- package/react/hooks/useSuspenseQuery.d.ts.map +1 -0
- package/react/hooks/useSuspenseQuery.js +157 -0
- package/react/hooks/useSuspenseQuery.js.map +1 -0
- package/react/index.d.ts +1 -0
- package/react/index.d.ts.map +1 -1
- package/react/index.js +1 -0
- package/react/index.js.map +1 -1
- 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/react.cjs +49 -2
- package/react/react.cjs.map +1 -1
- package/react/react.cjs.native.js +49 -2
- package/react/types/types.d.ts +9 -1
- package/react/types/types.d.ts.map +1 -1
- package/react/types/types.js.map +1 -1
- package/testing/core/core.cjs +1 -1
- package/testing/core/core.cjs.map +1 -1
- package/testing/core/core.cjs.native.js +1 -1
- package/testing/core/mocking/mockLink.js +1 -1
- package/testing/react/MockedProvider.d.ts +3 -0
- package/testing/react/MockedProvider.d.ts.map +1 -1
- package/testing/react/MockedProvider.js +8 -3
- package/testing/react/MockedProvider.js.map +1 -1
- package/testing/testing.cjs +54 -3
- package/testing/testing.cjs.map +1 -1
- package/testing/testing.cjs.native.js +54 -3
- 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/globals/index.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/utilities.cjs +16 -16
- package/utilities/utilities.cjs.map +1 -1
- package/utilities/utilities.cjs.native.js +16 -16
- package/version.js +1 -1
|
@@ -47,13 +47,16 @@ var ApolloConsumer = function (props) {
|
|
|
47
47
|
};
|
|
48
48
|
|
|
49
49
|
var ApolloProvider = function (_a) {
|
|
50
|
-
var client = _a.client, children = _a.children;
|
|
50
|
+
var client = _a.client, suspenseCache = _a.suspenseCache, children = _a.children;
|
|
51
51
|
var ApolloContext = getApolloContext();
|
|
52
52
|
return (React__namespace.createElement(ApolloContext.Consumer, null, function (context) {
|
|
53
53
|
if (context === void 0) { context = {}; }
|
|
54
54
|
if (client && context.client !== client) {
|
|
55
55
|
context = Object.assign({}, context, { client: client });
|
|
56
56
|
}
|
|
57
|
+
if (suspenseCache) {
|
|
58
|
+
context = Object.assign({}, context, { suspenseCache: suspenseCache });
|
|
59
|
+
}
|
|
57
60
|
__DEV__ ? globals.invariant(context.client, 'ApolloProvider was not passed a client instance. Make ' +
|
|
58
61
|
'sure you pass in your client via the "client" prop.') : globals.invariant(context.client, 28);
|
|
59
62
|
return (React__namespace.createElement(ApolloContext.Provider, { value: context }, children));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compareResults.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/compareResults.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,YAAY,EAOb,MAAM,SAAS,CAAC;AAejB,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,YAAY,EACnB,OAAO,EAAE,GAAG,EACZ,OAAO,EAAE,GAAG,EACZ,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,GAC1C,OAAO,CAWT"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import equal from "@wry/equality";
|
|
2
|
+
import { createFragmentMap, getFragmentDefinitions, getFragmentFromSelection, getMainDefinition, isField, resultKeyNameFromField, shouldInclude, } from "../../utilities/index.js";
|
|
3
|
+
export function compareResultsUsingQuery(query, aResult, bResult, variables) {
|
|
4
|
+
if (aResult === bResult)
|
|
5
|
+
return true;
|
|
6
|
+
return compareResultsUsingSelectionSet(getMainDefinition(query).selectionSet, aResult, bResult, {
|
|
7
|
+
fragmentMap: createFragmentMap(getFragmentDefinitions(query)),
|
|
8
|
+
variables: variables,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function compareResultsUsingSelectionSet(selectionSet, aResult, bResult, context) {
|
|
12
|
+
var seenSelections = new Set();
|
|
13
|
+
return selectionSet.selections.every(function (selection) {
|
|
14
|
+
if (seenSelections.has(selection))
|
|
15
|
+
return true;
|
|
16
|
+
seenSelections.add(selection);
|
|
17
|
+
if (!shouldInclude(selection, context.variables))
|
|
18
|
+
return true;
|
|
19
|
+
if (selectionHasNonreactiveDirective(selection))
|
|
20
|
+
return true;
|
|
21
|
+
if (isField(selection)) {
|
|
22
|
+
var resultKey = resultKeyNameFromField(selection);
|
|
23
|
+
var aResultChild = aResult && aResult[resultKey];
|
|
24
|
+
var bResultChild = bResult && bResult[resultKey];
|
|
25
|
+
var childSelectionSet = selection.selectionSet;
|
|
26
|
+
if (!childSelectionSet) {
|
|
27
|
+
return equal(aResultChild, bResultChild);
|
|
28
|
+
}
|
|
29
|
+
var aChildIsArray = Array.isArray(aResultChild);
|
|
30
|
+
var bChildIsArray = Array.isArray(bResultChild);
|
|
31
|
+
if (aChildIsArray !== bChildIsArray)
|
|
32
|
+
return false;
|
|
33
|
+
if (aChildIsArray && bChildIsArray) {
|
|
34
|
+
var length_1 = aResultChild.length;
|
|
35
|
+
if (bResultChild.length !== length_1) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
for (var i = 0; i < length_1; ++i) {
|
|
39
|
+
if (!compareResultsUsingSelectionSet(childSelectionSet, aResultChild[i], bResultChild[i], context)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return compareResultsUsingSelectionSet(childSelectionSet, aResultChild, bResultChild, context);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
var fragment = getFragmentFromSelection(selection, context.fragmentMap);
|
|
49
|
+
if (fragment) {
|
|
50
|
+
if (selectionHasNonreactiveDirective(fragment))
|
|
51
|
+
return true;
|
|
52
|
+
return compareResultsUsingSelectionSet(fragment.selectionSet, aResult, bResult, context);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function selectionHasNonreactiveDirective(selection) {
|
|
58
|
+
return !!selection.directives && selection.directives.some(directiveIsNonreactive);
|
|
59
|
+
}
|
|
60
|
+
function directiveIsNonreactive(dir) {
|
|
61
|
+
return dir.name.value === "nonreactive";
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=compareResults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compareResults.js","sourceRoot":"","sources":["../../../src/react/hooks/compareResults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,eAAe,CAAC;AAalC,OAAO,EACL,iBAAiB,EAEjB,sBAAsB,EACtB,wBAAwB,EACxB,iBAAiB,EACjB,OAAO,EACP,sBAAsB,EACtB,aAAa,GACd,MAAM,iBAAiB,CAAC;AAIzB,MAAM,UAAU,wB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ality\";\n\nimport {\n DirectiveNode,\n DocumentNode,\n FieldNode,\n FragmentDefinitionNode,\n FragmentSpreadNode,\n InlineFragmentNode,\n SelectionNode,\n SelectionSetNode,\n} from \"graphql\";\n\nimport {\n createFragmentMap,\n FragmentMap,\n getFragmentDefinitions,\n getFragmentFromSelection,\n getMainDefinition,\n isField,\n resultKeyNameFromField,\n shouldInclude,\n} from \"../../utilities\";\n\n// Returns true if aResult and bResult are deeply equal according to the fields\n// selected by the given query, ignoring any fields marked as @nonreactive.\nexport function compareResultsUsingQuery(\n query: DocumentNode,\n aResult: any,\n bResult: any,\n variables?: Record<string, any> | undefined,\n): boolean {\n if (aResult === bResult) return true;\n return compareResultsUsingSelectionSet(\n getMainDefinition(query).selectionSet,\n aResult,\n bResult,\n {\n fragmentMap: createFragmentMap(getFragmentDefinitions(query)),\n variables,\n },\n );\n}\n\n// Encapsulates the information used by compareResultsUsingSelectionSet that\n// does not change during the recursion.\ninterface CompareContext {\n fragmentMap: FragmentMap;\n variables: Record<string, any> | undefined;\n}\n\nfunction compareResultsUsingSelectionSet(\n selectionSet: SelectionSetNode,\n aResult: any,\n bResult: any,\n context: CompareContext,\n): boolean {\n const seenSelections = new Set<SelectionNode>();\n\n // Returning true from this Array.prototype.every callback function skips the\n // current field/subtree. Returning false aborts the entire traversal\n // immediately, causing compareResultsUsingSelectionSet to return false.\n return selectionSet.selections.every(selection => {\n // Avoid re-processing the same selection at the same level of recursion, in\n // case the same field gets included via multiple indirect fragment spreads.\n if (seenSelections.has(selection)) return true;\n seenSelections.add(selection);\n\n // Ignore @skip(if: true) and @include(if: false) fields.\n if (!shouldInclude(selection, context.variables)) return true;\n\n // If the field or (named) fragment spread has a @nonreactive directive on\n // it, we don't care if it's different, so we pretend it's the same.\n if (selectionHasNonreactiveDirective(selection)) return true;\n\n if (isField(selection)) {\n const resultKey = resultKeyNameFromField(selection);\n const aResultChild = aResult && aResult[resultKey];\n const bResultChild = bResult && bResult[resultKey];\n const childSelectionSet = selection.selectionSet;\n\n if (!childSelectionSet) {\n // These are scalar values, so we can compare them with deep equal\n // without redoing the main recursive work.\n return equal(aResultChild, bResultChild);\n }\n\n const aChildIsArray = Array.isArray(aResultChild);\n const bChildIsArray = Array.isArray(bResultChild);\n if (aChildIsArray !== bChildIsArray) return false;\n if (aChildIsArray && bChildIsArray) {\n const length = aResultChild.length;\n if (bResultChild.length !== length) {\n return false;\n }\n for (let i = 0; i < length; ++i) {\n if (!compareResultsUsingSelectionSet(\n childSelectionSet,\n aResultChild[i],\n bResultChild[i],\n context,\n )) {\n return false;\n }\n }\n return true;\n }\n\n return compareResultsUsingSelectionSet(\n childSelectionSet,\n aResultChild,\n bResultChild,\n context,\n );\n\n } else {\n const fragment = getFragmentFromSelection(selection, context.fragmentMap);\n if (fragment) {\n // The fragment might === selection if it's an inline fragment, but\n // could be !== if it's a named fragment ...spread.\n if (selectionHasNonreactiveDirective(fragment)) return true;\n\n return compareResultsUsingSelectionSet(\n fragment.selectionSet,\n // Notice that we reuse the same aResult and bResult values here,\n // since the fragment ...spread does not specify a field name, but\n // consists of multiple fields (within the fragment's selection set)\n // that should be applied to the current result value(s).\n aResult,\n bResult,\n context,\n );\n }\n }\n });\n}\n\nfunction selectionHasNonreactiveDirective(selection:\n | FieldNode\n | InlineFragmentNode\n | FragmentSpreadNode\n | FragmentDefinitionNode,\n): boolean {\n return !!selection.directives && selection.directives.some(directiveIsNonreactive);\n}\n\nfunction directiveIsNonreactive(dir: DirectiveNode): boolean {\n return dir.name.value === \"nonreactive\";\n}\n"]}
|
package/react/hooks/hooks.cjs
CHANGED
|
@@ -304,16 +304,18 @@ var InternalState = (function () {
|
|
|
304
304
|
}
|
|
305
305
|
this.result = nextResult;
|
|
306
306
|
this.forceUpdate();
|
|
307
|
-
this.handleErrorOrCompleted(nextResult);
|
|
307
|
+
this.handleErrorOrCompleted(nextResult, previousResult);
|
|
308
308
|
};
|
|
309
|
-
InternalState.prototype.handleErrorOrCompleted = function (result) {
|
|
309
|
+
InternalState.prototype.handleErrorOrCompleted = function (result, previousResult) {
|
|
310
310
|
var _this = this;
|
|
311
311
|
if (!result.loading) {
|
|
312
312
|
Promise.resolve().then(function () {
|
|
313
313
|
if (result.error) {
|
|
314
314
|
_this.onError(result.error);
|
|
315
315
|
}
|
|
316
|
-
else if (result.data
|
|
316
|
+
else if (result.data &&
|
|
317
|
+
(previousResult === null || previousResult === void 0 ? void 0 : previousResult.networkStatus) !== result.networkStatus &&
|
|
318
|
+
result.networkStatus === core.NetworkStatus.ready) {
|
|
317
319
|
_this.onCompleted(result.data);
|
|
318
320
|
}
|
|
319
321
|
}).catch(function (error) {
|
|
@@ -682,6 +684,167 @@ function diffToResult(diff) {
|
|
|
682
684
|
return result;
|
|
683
685
|
}
|
|
684
686
|
|
|
687
|
+
function useDeepMemo(memoFn, deps) {
|
|
688
|
+
var ref = React.useRef();
|
|
689
|
+
if (!ref.current || !equality.equal(ref.current.deps, deps)) {
|
|
690
|
+
ref.current = { value: memoFn(), deps: deps };
|
|
691
|
+
}
|
|
692
|
+
return ref.current.value;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function useSuspenseCache() {
|
|
696
|
+
var suspenseCache = React.useContext(context.getApolloContext()).suspenseCache;
|
|
697
|
+
__DEV__ ? globals.invariant(suspenseCache, 'Could not find a "suspenseCache" in the context. Wrap the root component ' +
|
|
698
|
+
'in an <ApolloProvider> and provide a suspenseCache.') : globals.invariant(suspenseCache, 32);
|
|
699
|
+
return suspenseCache;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
var SUPPORTED_FETCH_POLICIES = [
|
|
703
|
+
'cache-first',
|
|
704
|
+
'network-only',
|
|
705
|
+
'no-cache',
|
|
706
|
+
'cache-and-network',
|
|
707
|
+
];
|
|
708
|
+
var DEFAULT_FETCH_POLICY = 'cache-first';
|
|
709
|
+
var DEFAULT_SUSPENSE_POLICY = 'always';
|
|
710
|
+
var DEFAULT_ERROR_POLICY = 'none';
|
|
711
|
+
function useSuspenseQuery_experimental(query, options) {
|
|
712
|
+
if (options === void 0) { options = Object.create(null); }
|
|
713
|
+
var suspenseCache = useSuspenseCache();
|
|
714
|
+
var client = useApolloClient(options.client);
|
|
715
|
+
var watchQueryOptions = useWatchQueryOptions({ query: query, options: options, client: client });
|
|
716
|
+
var previousWatchQueryOptionsRef = React.useRef(watchQueryOptions);
|
|
717
|
+
var fetchPolicy = watchQueryOptions.fetchPolicy, errorPolicy = watchQueryOptions.errorPolicy, returnPartialData = watchQueryOptions.returnPartialData, variables = watchQueryOptions.variables;
|
|
718
|
+
var cacheEntry = suspenseCache.lookup(query, variables);
|
|
719
|
+
var observable = React.useState(function () {
|
|
720
|
+
return (cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.observable) || client.watchQuery(watchQueryOptions);
|
|
721
|
+
})[0];
|
|
722
|
+
var result = useObservableQueryResult(observable);
|
|
723
|
+
if (result.error && errorPolicy === 'none') {
|
|
724
|
+
throw result.error;
|
|
725
|
+
}
|
|
726
|
+
if (result.loading) {
|
|
727
|
+
if (!cacheEntry) {
|
|
728
|
+
cacheEntry = suspenseCache.add(query, variables, {
|
|
729
|
+
promise: observable.reobserve(watchQueryOptions),
|
|
730
|
+
observable: observable,
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
var hasFullResult = result.data && !result.partial;
|
|
734
|
+
var usePartialResult = returnPartialData && result.partial && result.data;
|
|
735
|
+
var hasUsableResult = usePartialResult ||
|
|
736
|
+
(fetchPolicy === 'cache-and-network' && hasFullResult);
|
|
737
|
+
if (!hasUsableResult && !cacheEntry.fulfilled) {
|
|
738
|
+
throw cacheEntry.promise;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
React.useEffect(function () {
|
|
742
|
+
var variables = watchQueryOptions.variables, query = watchQueryOptions.query;
|
|
743
|
+
var previousOpts = previousWatchQueryOptionsRef.current;
|
|
744
|
+
if (variables !== previousOpts.variables || query !== previousOpts.query) {
|
|
745
|
+
suspenseCache.remove(previousOpts.query, previousOpts.variables);
|
|
746
|
+
suspenseCache.add(query, variables, {
|
|
747
|
+
promise: observable.reobserve({ query: query, variables: variables }),
|
|
748
|
+
observable: observable,
|
|
749
|
+
});
|
|
750
|
+
previousWatchQueryOptionsRef.current = watchQueryOptions;
|
|
751
|
+
}
|
|
752
|
+
}, [watchQueryOptions]);
|
|
753
|
+
React.useEffect(function () {
|
|
754
|
+
return function () {
|
|
755
|
+
suspenseCache.remove(query, variables);
|
|
756
|
+
};
|
|
757
|
+
}, []);
|
|
758
|
+
return React.useMemo(function () {
|
|
759
|
+
return {
|
|
760
|
+
data: result.data,
|
|
761
|
+
error: errorPolicy === 'all' ? toApolloError(result) : void 0,
|
|
762
|
+
fetchMore: function (options) {
|
|
763
|
+
var promise = observable.fetchMore(options);
|
|
764
|
+
suspenseCache.add(query, watchQueryOptions.variables, {
|
|
765
|
+
promise: promise,
|
|
766
|
+
observable: observable,
|
|
767
|
+
});
|
|
768
|
+
return promise;
|
|
769
|
+
},
|
|
770
|
+
refetch: function (variables) {
|
|
771
|
+
var promise = observable.refetch(variables);
|
|
772
|
+
suspenseCache.add(query, watchQueryOptions.variables, {
|
|
773
|
+
promise: promise,
|
|
774
|
+
observable: observable,
|
|
775
|
+
});
|
|
776
|
+
return promise;
|
|
777
|
+
},
|
|
778
|
+
};
|
|
779
|
+
}, [result, observable, errorPolicy]);
|
|
780
|
+
}
|
|
781
|
+
function validateOptions(options) {
|
|
782
|
+
var query = options.query, _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? DEFAULT_FETCH_POLICY : _a, returnPartialData = options.returnPartialData;
|
|
783
|
+
parser.verifyDocumentType(query, parser.DocumentType.Query);
|
|
784
|
+
validateFetchPolicy(fetchPolicy);
|
|
785
|
+
validatePartialDataReturn(fetchPolicy, returnPartialData);
|
|
786
|
+
}
|
|
787
|
+
function validateFetchPolicy(fetchPolicy) {
|
|
788
|
+
__DEV__ ? globals.invariant(SUPPORTED_FETCH_POLICIES.includes(fetchPolicy), "The fetch policy `".concat(fetchPolicy, "` is not supported with suspense.")) : globals.invariant(SUPPORTED_FETCH_POLICIES.includes(fetchPolicy), 33);
|
|
789
|
+
}
|
|
790
|
+
function validatePartialDataReturn(fetchPolicy, returnPartialData) {
|
|
791
|
+
if (fetchPolicy === 'no-cache' && returnPartialData) {
|
|
792
|
+
__DEV__ && globals.invariant.warn('Using `returnPartialData` with a `no-cache` fetch policy has no effect. To read partial data from the cache, consider using an alternate fetch policy.');
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
function toApolloError(result) {
|
|
796
|
+
return utilities.isNonEmptyArray(result.errors)
|
|
797
|
+
? new core.ApolloError({ graphQLErrors: result.errors })
|
|
798
|
+
: result.error;
|
|
799
|
+
}
|
|
800
|
+
function useWatchQueryOptions(_a) {
|
|
801
|
+
var query = _a.query, options = _a.options, client = _a.client;
|
|
802
|
+
var defaultOptions = client.defaultOptions.watchQuery;
|
|
803
|
+
var watchQueryOptions = useDeepMemo(function () {
|
|
804
|
+
var errorPolicy = options.errorPolicy, fetchPolicy = options.fetchPolicy, _a = options.suspensePolicy, suspensePolicy = _a === void 0 ? DEFAULT_SUSPENSE_POLICY : _a, variables = options.variables, watchQueryOptions = tslib.__rest(options, ["errorPolicy", "fetchPolicy", "suspensePolicy", "variables"]);
|
|
805
|
+
return tslib.__assign(tslib.__assign({}, watchQueryOptions), { query: query, errorPolicy: errorPolicy || (defaultOptions === null || defaultOptions === void 0 ? void 0 : defaultOptions.errorPolicy) || DEFAULT_ERROR_POLICY, fetchPolicy: fetchPolicy || (defaultOptions === null || defaultOptions === void 0 ? void 0 : defaultOptions.fetchPolicy) || DEFAULT_FETCH_POLICY, notifyOnNetworkStatusChange: suspensePolicy === 'always', fetchOnFirstSubscribe: false, variables: utilities.compact(tslib.__assign(tslib.__assign({}, defaultOptions === null || defaultOptions === void 0 ? void 0 : defaultOptions.variables), variables)) });
|
|
806
|
+
}, [options, query, defaultOptions]);
|
|
807
|
+
if (__DEV__) {
|
|
808
|
+
validateOptions(watchQueryOptions);
|
|
809
|
+
}
|
|
810
|
+
return watchQueryOptions;
|
|
811
|
+
}
|
|
812
|
+
function useObservableQueryResult(observable) {
|
|
813
|
+
var resultRef = React.useRef();
|
|
814
|
+
var isMountedRef = React.useRef(false);
|
|
815
|
+
if (!resultRef.current) {
|
|
816
|
+
resultRef.current = observable.getCurrentResult();
|
|
817
|
+
}
|
|
818
|
+
React.useLayoutEffect(function () {
|
|
819
|
+
isMountedRef.current = true;
|
|
820
|
+
return function () {
|
|
821
|
+
isMountedRef.current = false;
|
|
822
|
+
};
|
|
823
|
+
}, []);
|
|
824
|
+
return useSyncExternalStore(React.useCallback(function (forceUpdate) {
|
|
825
|
+
function handleUpdate() {
|
|
826
|
+
var previousResult = resultRef.current;
|
|
827
|
+
var result = observable.getCurrentResult();
|
|
828
|
+
if (previousResult.loading === result.loading &&
|
|
829
|
+
previousResult.networkStatus === result.networkStatus &&
|
|
830
|
+
equality.equal(previousResult.data, result.data)) {
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
resultRef.current = result;
|
|
834
|
+
if (isMountedRef.current) {
|
|
835
|
+
forceUpdate();
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
var subscription = observable.subscribe({
|
|
839
|
+
next: handleUpdate,
|
|
840
|
+
error: handleUpdate,
|
|
841
|
+
});
|
|
842
|
+
return function () {
|
|
843
|
+
subscription.unsubscribe();
|
|
844
|
+
};
|
|
845
|
+
}, [observable]), function () { return resultRef.current; }, function () { return resultRef.current; });
|
|
846
|
+
}
|
|
847
|
+
|
|
685
848
|
exports.useApolloClient = useApolloClient;
|
|
686
849
|
exports.useFragment_experimental = useFragment_experimental;
|
|
687
850
|
exports.useLazyQuery = useLazyQuery;
|
|
@@ -689,4 +852,5 @@ exports.useMutation = useMutation;
|
|
|
689
852
|
exports.useQuery = useQuery;
|
|
690
853
|
exports.useReactiveVar = useReactiveVar;
|
|
691
854
|
exports.useSubscription = useSubscription;
|
|
855
|
+
exports.useSuspenseQuery_experimental = useSuspenseQuery_experimental;
|
|
692
856
|
//# sourceMappingURL=hooks.cjs.map
|