@apollo/client 3.8.0-beta.3 → 3.8.0-beta.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/apollo-client.cjs +172 -99
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/core/ObservableQuery.d.ts +1 -0
- package/core/ObservableQuery.d.ts.map +1 -1
- package/core/ObservableQuery.js +17 -4
- package/core/ObservableQuery.js.map +1 -1
- package/core/core.cjs +18 -5
- package/core/core.cjs.map +1 -1
- package/core/core.cjs.native.js +18 -5
- package/dev/dev.cjs +5 -5
- package/dev/dev.cjs.map +1 -1
- package/dev/dev.cjs.native.js +5 -5
- package/invariantErrorCodes.js +4 -4
- package/link/batch-http/batchHttpLink.d.ts +1 -1
- package/link/batch-http/batchHttpLink.d.ts.map +1 -1
- package/link/batch-http/batchHttpLink.js.map +1 -1
- package/link/persisted-queries/index.d.ts +10 -4
- package/link/persisted-queries/index.d.ts.map +1 -1
- package/link/persisted-queries/index.js +25 -28
- package/link/persisted-queries/index.js.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs +24 -27
- package/link/persisted-queries/persisted-queries.cjs.map +1 -1
- package/link/persisted-queries/persisted-queries.cjs.native.js +24 -27
- package/package.json +9 -9
- package/react/cache/QueryReference.d.ts +10 -4
- package/react/cache/QueryReference.d.ts.map +1 -1
- package/react/cache/QueryReference.js +31 -14
- package/react/cache/QueryReference.js.map +1 -1
- package/react/cache/SuspenseCache.d.ts +2 -2
- package/react/cache/SuspenseCache.d.ts.map +1 -1
- package/react/cache/SuspenseCache.js +2 -2
- package/react/cache/SuspenseCache.js.map +1 -1
- package/react/hooks/hooks.cjs +184 -20
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/hooks.cjs.native.js +184 -20
- package/react/hooks/index.d.ts +2 -1
- package/react/hooks/index.d.ts.map +1 -1
- package/react/hooks/index.js +2 -1
- package/react/hooks/index.js.map +1 -1
- package/react/hooks/useBackgroundQuery.d.ts +18 -11
- package/react/hooks/useBackgroundQuery.d.ts.map +1 -1
- package/react/hooks/useBackgroundQuery.js +12 -30
- package/react/hooks/useBackgroundQuery.js.map +1 -1
- package/react/hooks/useQuery.js +1 -1
- package/react/hooks/useReadQuery.d.ts +8 -0
- package/react/hooks/useReadQuery.d.ts.map +1 -0
- package/react/hooks/useReadQuery.js +42 -0
- package/react/hooks/useReadQuery.js.map +1 -0
- package/react/hooks/useSuspenseQuery.d.ts +14 -6
- package/react/hooks/useSuspenseQuery.d.ts.map +1 -1
- package/react/hooks/useSuspenseQuery.js +33 -10
- package/react/hooks/useSuspenseQuery.js.map +1 -1
- package/react/react.cjs +30 -14
- package/react/react.cjs.map +1 -1
- package/react/react.cjs.native.js +30 -14
- package/react/types/types.d.ts +7 -1
- package/react/types/types.d.ts.map +1 -1
- package/react/types/types.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/index.js.map +1 -1
- package/utilities/policies/pagination.d.ts.map +1 -1
- package/utilities/policies/pagination.js +3 -0
- package/utilities/policies/pagination.js.map +1 -1
- package/utilities/utilities.cjs +3 -0
- package/utilities/utilities.cjs.map +1 -1
- package/utilities/utilities.cjs.native.js +3 -0
- package/version.js +1 -1
|
@@ -12,6 +12,7 @@ var errors = require('../../errors');
|
|
|
12
12
|
var core = require('../../core');
|
|
13
13
|
var parser = require('../parser');
|
|
14
14
|
var cache = require('../../cache');
|
|
15
|
+
var invariant = require('ts-invariant');
|
|
15
16
|
|
|
16
17
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
17
18
|
|
|
@@ -29,6 +30,7 @@ function _interopNamespace(e) {
|
|
|
29
30
|
|
|
30
31
|
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
31
32
|
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
33
|
+
var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
|
|
32
34
|
|
|
33
35
|
function useApolloClient(override) {
|
|
34
36
|
var context$1 = React.useContext(context.getApolloContext());
|
|
@@ -126,7 +128,7 @@ var InternalState = (function () {
|
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
InternalState.prototype.forceUpdate = function () {
|
|
129
|
-
globals.__DEV__ && globals.invariant.warn(
|
|
131
|
+
globals.__DEV__ && globals.invariant.warn(49);
|
|
130
132
|
};
|
|
131
133
|
InternalState.prototype.executeQuery = function (options) {
|
|
132
134
|
var _this = this;
|
|
@@ -759,15 +761,20 @@ function useSuspenseQuery(query, options) {
|
|
|
759
761
|
if (options === void 0) { options = Object.create(null); }
|
|
760
762
|
var client = useApolloClient(options.client);
|
|
761
763
|
var suspenseCache = useSuspenseCache(options.suspenseCache);
|
|
762
|
-
var watchQueryOptions = useWatchQueryOptions({ query: query, options: options });
|
|
763
|
-
var variables = watchQueryOptions.variables;
|
|
764
|
+
var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
|
|
765
|
+
var fetchPolicy = watchQueryOptions.fetchPolicy, variables = watchQueryOptions.variables;
|
|
764
766
|
var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
|
|
765
767
|
var cacheKey = [client, query, cache.canonicalStringify(variables)].concat(queryKey);
|
|
766
768
|
var queryRef = suspenseCache.getQueryRef(cacheKey, function () {
|
|
767
769
|
return client.watchQuery(watchQueryOptions);
|
|
768
770
|
});
|
|
771
|
+
var currentFetchPolicy = queryRef.watchQueryOptions.fetchPolicy;
|
|
769
772
|
var _b = React.useState(function () { return new Map([[queryRef.key, queryRef.promise]]); }), promiseCache = _b[0], setPromiseCache = _b[1];
|
|
770
773
|
var promise = promiseCache.get(queryRef.key);
|
|
774
|
+
if (currentFetchPolicy === 'standby' && fetchPolicy !== currentFetchPolicy) {
|
|
775
|
+
promise = queryRef.reobserve({ fetchPolicy: fetchPolicy });
|
|
776
|
+
promiseCache.set(queryRef.key, promise);
|
|
777
|
+
}
|
|
771
778
|
if (!promise) {
|
|
772
779
|
promise = queryRef.promise;
|
|
773
780
|
promiseCache.set(queryRef.key, promise);
|
|
@@ -780,7 +787,16 @@ function useSuspenseQuery(query, options) {
|
|
|
780
787
|
});
|
|
781
788
|
});
|
|
782
789
|
}, [queryRef]);
|
|
783
|
-
var
|
|
790
|
+
var skipResult = React.useMemo(function () {
|
|
791
|
+
var error = toApolloError(queryRef.result);
|
|
792
|
+
return {
|
|
793
|
+
loading: false,
|
|
794
|
+
data: queryRef.result.data,
|
|
795
|
+
networkStatus: error ? core.NetworkStatus.error : core.NetworkStatus.ready,
|
|
796
|
+
error: error,
|
|
797
|
+
};
|
|
798
|
+
}, [queryRef.result]);
|
|
799
|
+
var result = watchQueryOptions.fetchPolicy === 'standby' ? skipResult : __use(promise);
|
|
784
800
|
var fetchMore = React.useCallback(function (options) {
|
|
785
801
|
var promise = queryRef.fetchMore(options);
|
|
786
802
|
setPromiseCache(function (previousPromiseCache) {
|
|
@@ -842,26 +858,160 @@ function useTrackedQueryRefs(queryRef) {
|
|
|
842
858
|
});
|
|
843
859
|
}
|
|
844
860
|
function useWatchQueryOptions(_a) {
|
|
845
|
-
var query = _a.query, options = _a.options;
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
861
|
+
var client = _a.client, query = _a.query, options = _a.options;
|
|
862
|
+
return useDeepMemo(function () {
|
|
863
|
+
var _a;
|
|
864
|
+
var fetchPolicy = options.fetchPolicy ||
|
|
865
|
+
((_a = client.defaultOptions.watchQuery) === null || _a === void 0 ? void 0 : _a.fetchPolicy) ||
|
|
866
|
+
'cache-first';
|
|
867
|
+
var watchQueryOptions = tslib.__assign(tslib.__assign({}, options), { fetchPolicy: fetchPolicy, query: query, notifyOnNetworkStatusChange: false, nextFetchPolicy: void 0 });
|
|
868
|
+
if (globals.__DEV__) {
|
|
869
|
+
validateOptions(watchQueryOptions);
|
|
870
|
+
}
|
|
871
|
+
if (options.skip) {
|
|
872
|
+
watchQueryOptions.fetchPolicy = 'standby';
|
|
873
|
+
}
|
|
874
|
+
return watchQueryOptions;
|
|
875
|
+
}, [client, options, query]);
|
|
851
876
|
}
|
|
852
877
|
|
|
878
|
+
var QUERY_REFERENCE_SYMBOL = Symbol();
|
|
879
|
+
((function () {
|
|
880
|
+
function InternalQueryReference(observable, options) {
|
|
881
|
+
var _this = this;
|
|
882
|
+
var _a;
|
|
883
|
+
this.listeners = new Set();
|
|
884
|
+
this.initialized = false;
|
|
885
|
+
this.refetching = false;
|
|
886
|
+
this.listen = this.listen.bind(this);
|
|
887
|
+
this.handleNext = this.handleNext.bind(this);
|
|
888
|
+
this.handleError = this.handleError.bind(this);
|
|
889
|
+
this.dispose = this.dispose.bind(this);
|
|
890
|
+
this.observable = observable;
|
|
891
|
+
this.result = observable.getCurrentResult(false);
|
|
892
|
+
this.key = options.key;
|
|
893
|
+
if (options.onDispose) {
|
|
894
|
+
this.onDispose = options.onDispose;
|
|
895
|
+
}
|
|
896
|
+
if (core.isNetworkRequestSettled(this.result.networkStatus) ||
|
|
897
|
+
(this.result.data &&
|
|
898
|
+
(!this.result.partial || this.observable.options.returnPartialData))) {
|
|
899
|
+
this.promise = utilities.createFulfilledPromise(this.result);
|
|
900
|
+
this.initialized = true;
|
|
901
|
+
this.refetching = false;
|
|
902
|
+
}
|
|
903
|
+
this.subscription = observable.subscribe({
|
|
904
|
+
next: this.handleNext,
|
|
905
|
+
error: this.handleError,
|
|
906
|
+
});
|
|
907
|
+
if (!this.promise) {
|
|
908
|
+
this.promise = new Promise(function (resolve, reject) {
|
|
909
|
+
_this.resolve = resolve;
|
|
910
|
+
_this.reject = reject;
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
this.autoDisposeTimeoutId = setTimeout(this.dispose, (_a = options.autoDisposeTimeoutMs) !== null && _a !== void 0 ? _a : 30000);
|
|
914
|
+
}
|
|
915
|
+
Object.defineProperty(InternalQueryReference.prototype, "watchQueryOptions", {
|
|
916
|
+
get: function () {
|
|
917
|
+
return this.observable.options;
|
|
918
|
+
},
|
|
919
|
+
enumerable: false,
|
|
920
|
+
configurable: true
|
|
921
|
+
});
|
|
922
|
+
InternalQueryReference.prototype.listen = function (listener) {
|
|
923
|
+
var _this = this;
|
|
924
|
+
clearTimeout(this.autoDisposeTimeoutId);
|
|
925
|
+
this.listeners.add(listener);
|
|
926
|
+
return function () {
|
|
927
|
+
_this.listeners.delete(listener);
|
|
928
|
+
};
|
|
929
|
+
};
|
|
930
|
+
InternalQueryReference.prototype.refetch = function (variables) {
|
|
931
|
+
this.refetching = true;
|
|
932
|
+
var promise = this.observable.refetch(variables);
|
|
933
|
+
this.promise = promise;
|
|
934
|
+
return promise;
|
|
935
|
+
};
|
|
936
|
+
InternalQueryReference.prototype.fetchMore = function (options) {
|
|
937
|
+
var promise = this.observable.fetchMore(options);
|
|
938
|
+
this.promise = promise;
|
|
939
|
+
return promise;
|
|
940
|
+
};
|
|
941
|
+
InternalQueryReference.prototype.reobserve = function (watchQueryOptions) {
|
|
942
|
+
var promise = this.observable.reobserve(watchQueryOptions);
|
|
943
|
+
this.promise = promise;
|
|
944
|
+
return promise;
|
|
945
|
+
};
|
|
946
|
+
InternalQueryReference.prototype.dispose = function () {
|
|
947
|
+
this.subscription.unsubscribe();
|
|
948
|
+
this.onDispose();
|
|
949
|
+
};
|
|
950
|
+
InternalQueryReference.prototype.onDispose = function () {
|
|
951
|
+
};
|
|
952
|
+
InternalQueryReference.prototype.handleNext = function (result) {
|
|
953
|
+
if (!this.initialized || this.refetching) {
|
|
954
|
+
if (!core.isNetworkRequestSettled(result.networkStatus)) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (this.result.data && result.data === void 0) {
|
|
958
|
+
result.data = this.result.data;
|
|
959
|
+
}
|
|
960
|
+
this.initialized = true;
|
|
961
|
+
this.refetching = false;
|
|
962
|
+
this.result = result;
|
|
963
|
+
if (this.resolve) {
|
|
964
|
+
this.resolve(result);
|
|
965
|
+
}
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
if (result.data === this.result.data) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
this.result = result;
|
|
972
|
+
this.promise = utilities.createFulfilledPromise(result);
|
|
973
|
+
this.deliver(this.promise);
|
|
974
|
+
};
|
|
975
|
+
InternalQueryReference.prototype.handleError = function (error) {
|
|
976
|
+
var result = tslib.__assign(tslib.__assign({}, this.result), { error: error, networkStatus: core.NetworkStatus.error });
|
|
977
|
+
this.result = result;
|
|
978
|
+
if (!this.initialized || this.refetching) {
|
|
979
|
+
this.initialized = true;
|
|
980
|
+
this.refetching = false;
|
|
981
|
+
if (this.reject) {
|
|
982
|
+
this.reject(error);
|
|
983
|
+
}
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
this.result = result;
|
|
987
|
+
this.promise = result.data
|
|
988
|
+
? utilities.createFulfilledPromise(result)
|
|
989
|
+
: utilities.createRejectedPromise(result);
|
|
990
|
+
this.deliver(this.promise);
|
|
991
|
+
};
|
|
992
|
+
InternalQueryReference.prototype.deliver = function (promise) {
|
|
993
|
+
this.listeners.forEach(function (listener) { return listener(promise); });
|
|
994
|
+
};
|
|
995
|
+
return InternalQueryReference;
|
|
996
|
+
})());
|
|
997
|
+
|
|
853
998
|
function useBackgroundQuery(query, options) {
|
|
854
999
|
if (options === void 0) { options = Object.create(null); }
|
|
855
1000
|
var suspenseCache = useSuspenseCache(options.suspenseCache);
|
|
856
1001
|
var client = useApolloClient(options.client);
|
|
857
|
-
var watchQueryOptions = useWatchQueryOptions({ query: query, options: options });
|
|
858
|
-
var variables = watchQueryOptions.variables;
|
|
1002
|
+
var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
|
|
1003
|
+
var fetchPolicy = watchQueryOptions.fetchPolicy, variables = watchQueryOptions.variables;
|
|
859
1004
|
var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
|
|
860
1005
|
var cacheKey = [client, query, cache.canonicalStringify(variables)].concat(queryKey);
|
|
861
1006
|
var queryRef = suspenseCache.getQueryRef(cacheKey, function () {
|
|
862
1007
|
return client.watchQuery(watchQueryOptions);
|
|
863
1008
|
});
|
|
1009
|
+
var currentFetchPolicy = queryRef.watchQueryOptions.fetchPolicy;
|
|
864
1010
|
var _b = React.useState(function () { return new Map([[queryRef.key, queryRef.promise]]); }), promiseCache = _b[0], setPromiseCache = _b[1];
|
|
1011
|
+
if (currentFetchPolicy === 'standby' && fetchPolicy !== currentFetchPolicy) {
|
|
1012
|
+
var promise = queryRef.reobserve({ fetchPolicy: fetchPolicy });
|
|
1013
|
+
promiseCache.set(queryRef.key, promise);
|
|
1014
|
+
}
|
|
865
1015
|
useTrackedQueryRefs(queryRef);
|
|
866
1016
|
var fetchMore = React.useCallback(function (options) {
|
|
867
1017
|
var promise = queryRef.fetchMore(options);
|
|
@@ -879,8 +1029,9 @@ function useBackgroundQuery(query, options) {
|
|
|
879
1029
|
}, [queryRef]);
|
|
880
1030
|
queryRef.promiseCache = promiseCache;
|
|
881
1031
|
return React.useMemo(function () {
|
|
1032
|
+
var _a;
|
|
882
1033
|
return [
|
|
883
|
-
queryRef,
|
|
1034
|
+
(_a = {}, _a[QUERY_REFERENCE_SYMBOL] = queryRef, _a),
|
|
884
1035
|
{
|
|
885
1036
|
fetchMore: fetchMore,
|
|
886
1037
|
refetch: refetch,
|
|
@@ -888,21 +1039,34 @@ function useBackgroundQuery(query, options) {
|
|
|
888
1039
|
];
|
|
889
1040
|
}, [queryRef, fetchMore, refetch]);
|
|
890
1041
|
}
|
|
1042
|
+
|
|
891
1043
|
function useReadQuery(queryRef) {
|
|
892
1044
|
var _a = React.useState(0), forceUpdate = _a[1];
|
|
893
|
-
|
|
894
|
-
|
|
1045
|
+
var internalQueryRef = queryRef[QUERY_REFERENCE_SYMBOL];
|
|
1046
|
+
invariant__default(internalQueryRef.promiseCache, 50);
|
|
1047
|
+
var skipResult = React.useMemo(function () {
|
|
1048
|
+
var error = toApolloError(internalQueryRef.result);
|
|
1049
|
+
return {
|
|
1050
|
+
loading: false,
|
|
1051
|
+
data: internalQueryRef.result.data,
|
|
1052
|
+
networkStatus: error ? core.NetworkStatus.error : core.NetworkStatus.ready,
|
|
1053
|
+
error: error,
|
|
1054
|
+
};
|
|
1055
|
+
}, [internalQueryRef.result]);
|
|
1056
|
+
var promise = internalQueryRef.promiseCache.get(internalQueryRef.key);
|
|
895
1057
|
if (!promise) {
|
|
896
|
-
promise =
|
|
897
|
-
|
|
1058
|
+
promise = internalQueryRef.promise;
|
|
1059
|
+
internalQueryRef.promiseCache.set(internalQueryRef.key, promise);
|
|
898
1060
|
}
|
|
899
1061
|
React.useEffect(function () {
|
|
900
|
-
return
|
|
901
|
-
|
|
1062
|
+
return internalQueryRef.listen(function (promise) {
|
|
1063
|
+
internalQueryRef.promiseCache.set(internalQueryRef.key, promise);
|
|
902
1064
|
forceUpdate(function (prevState) { return prevState + 1; });
|
|
903
1065
|
});
|
|
904
1066
|
}, [queryRef]);
|
|
905
|
-
var result =
|
|
1067
|
+
var result = internalQueryRef.watchQueryOptions.fetchPolicy === 'standby'
|
|
1068
|
+
? skipResult
|
|
1069
|
+
: __use(promise);
|
|
906
1070
|
return React.useMemo(function () {
|
|
907
1071
|
return {
|
|
908
1072
|
data: result.data,
|
package/react/hooks/index.d.ts
CHANGED
|
@@ -7,5 +7,6 @@ export * from './useSubscription';
|
|
|
7
7
|
export * from './useReactiveVar';
|
|
8
8
|
export * from './useFragment';
|
|
9
9
|
export { useSuspenseQuery, UseSuspenseQueryResult, FetchMoreFunction, RefetchFunction, SubscribeToMoreFunction, } from './useSuspenseQuery';
|
|
10
|
-
export { useBackgroundQuery,
|
|
10
|
+
export { useBackgroundQuery, UseBackgroundQueryResult, } from './useBackgroundQuery';
|
|
11
|
+
export { useReadQuery } from './useReadQuery';
|
|
11
12
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,iBAAiB,EACjB,eAAe,EACf,uBAAuB,GACxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,kBAAkB,EAClB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,iBAAiB,EACjB,eAAe,EACf,uBAAuB,GACxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,kBAAkB,EAClB,wBAAwB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
package/react/hooks/index.js
CHANGED
|
@@ -7,5 +7,6 @@ export * from "./useSubscription.js";
|
|
|
7
7
|
export * from "./useReactiveVar.js";
|
|
8
8
|
export * from "./useFragment.js";
|
|
9
9
|
export { useSuspenseQuery, } from "./useSuspenseQuery.js";
|
|
10
|
-
export { useBackgroundQuery,
|
|
10
|
+
export { useBackgroundQuery, } from "./useBackgroundQuery.js";
|
|
11
|
+
export { useReadQuery } from "./useReadQuery.js";
|
|
11
12
|
//# sourceMappingURL=index.js.map
|
package/react/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,OAAO,EACL,gBAAgB,GAKjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,kBAAkB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,OAAO,EACL,gBAAgB,GAKjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,kBAAkB,GAEnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["import '../../utilities/globals';\n\nexport * from './useApolloClient';\nexport * from './useLazyQuery';\nexport * from './useMutation';\nexport { useQuery } from './useQuery';\nexport * from './useSubscription';\nexport * from './useReactiveVar';\nexport * from './useFragment';\nexport {\n useSuspenseQuery,\n UseSuspenseQueryResult,\n FetchMoreFunction,\n RefetchFunction,\n SubscribeToMoreFunction,\n} from './useSuspenseQuery';\nexport {\n useBackgroundQuery,\n UseBackgroundQueryResult,\n} from './useBackgroundQuery';\nexport { useReadQuery } from './useReadQuery';\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { DocumentNode, OperationVariables, TypedDocumentNode } from '../../core';
|
|
2
|
-
import type
|
|
3
|
-
import type {
|
|
2
|
+
import { type QueryReference } from '../cache/QueryReference';
|
|
3
|
+
import type { BackgroundQueryHookOptions, NoInfer } from '../types/types';
|
|
4
4
|
import type { FetchMoreFunction, RefetchFunction } from './useSuspenseQuery';
|
|
5
5
|
import type { DeepPartial } from '../../utilities';
|
|
6
6
|
export type UseBackgroundQueryResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> = [
|
|
@@ -10,18 +10,25 @@ export type UseBackgroundQueryResult<TData = unknown, TVariables extends Operati
|
|
|
10
10
|
refetch: RefetchFunction<TData, TVariables>;
|
|
11
11
|
}
|
|
12
12
|
];
|
|
13
|
-
|
|
14
|
-
export declare function useBackgroundQuery<TData
|
|
13
|
+
type BackgroundQueryHookOptionsNoInfer<TData, TVariables extends OperationVariables> = BackgroundQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>;
|
|
14
|
+
export declare function useBackgroundQuery<TData, TVariables extends OperationVariables, TOptions extends Omit<BackgroundQueryHookOptions<TData>, 'variables'>>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions): UseBackgroundQueryResult<TOptions['errorPolicy'] extends 'ignore' | 'all' ? TOptions['returnPartialData'] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions['returnPartialData'] extends true ? TOptions['skip'] extends boolean ? DeepPartial<TData> | undefined : DeepPartial<TData> : TOptions['skip'] extends boolean ? TData | undefined : TData, TVariables>;
|
|
15
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
15
16
|
returnPartialData: true;
|
|
16
17
|
errorPolicy: 'ignore' | 'all';
|
|
17
18
|
}): UseBackgroundQueryResult<DeepPartial<TData> | undefined, TVariables>;
|
|
18
|
-
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options:
|
|
19
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
19
20
|
errorPolicy: 'ignore' | 'all';
|
|
20
21
|
}): UseBackgroundQueryResult<TData | undefined, TVariables>;
|
|
21
|
-
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
23
|
+
skip: boolean;
|
|
24
|
+
returnPartialData: true;
|
|
25
|
+
}): UseBackgroundQueryResult<DeepPartial<TData> | undefined, TVariables>;
|
|
26
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
27
|
+
returnPartialData: true;
|
|
28
|
+
}): UseBackgroundQueryResult<DeepPartial<TData>, TVariables>;
|
|
29
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
|
|
30
|
+
skip: boolean;
|
|
31
|
+
}): UseBackgroundQueryResult<TData | undefined, TVariables>;
|
|
32
|
+
export declare function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>): UseBackgroundQueryResult<TData, TVariables>;
|
|
33
|
+
export {};
|
|
27
34
|
//# sourceMappingURL=useBackgroundQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBackgroundQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"useBackgroundQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAEL,KAAK,cAAc,EACpB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,0BAA0B,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAI1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE7E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,MAAM,MAAM,wBAAwB,CAClC,KAAK,GAAG,OAAO,EACf,UAAU,SAAS,kBAAkB,GAAG,kBAAkB,IACxD;IACF,cAAc,CAAC,KAAK,CAAC;IACrB;QACE,SAAS,EAAE,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QAChD,OAAO,EAAE,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAC7C;CACF,CAAC;AAEF,KAAK,iCAAiC,CACpC,KAAK,EACL,UAAU,SAAS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;IAC9D,IAAI,EAAE,OAAO,CAAC;CACf,GACA,wBAAwB,CAAC,KAAK,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC;AAE3D,wBAAgB,kBAAkB,CAChC,KAAK,GAAG,OAAO,EACf,UAAU,SAAS,kBAAkB,GAAG,kBAAkB,EAE1D,KAAK,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC1D,OAAO,CAAC,EAAE,iCAAiC,CAAC,KAAK,EAAE,UAAU,CAAC,GAC7D,wBAAwB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC"}
|
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useState, useMemo, useCallback } from 'react';
|
|
2
2
|
import { useApolloClient } from "./useApolloClient.js";
|
|
3
|
-
import {
|
|
3
|
+
import { QUERY_REFERENCE_SYMBOL, } from "../cache/QueryReference.js";
|
|
4
4
|
import { useSuspenseCache } from "./useSuspenseCache.js";
|
|
5
|
-
import {
|
|
5
|
+
import { useTrackedQueryRefs, useWatchQueryOptions } from "./useSuspenseQuery.js";
|
|
6
6
|
import { canonicalStringify } from "../../cache/index.js";
|
|
7
|
-
import { invariant } from "../../utilities/globals/index.js";
|
|
8
7
|
export function useBackgroundQuery(query, options) {
|
|
9
8
|
if (options === void 0) { options = Object.create(null); }
|
|
10
9
|
var suspenseCache = useSuspenseCache(options.suspenseCache);
|
|
11
10
|
var client = useApolloClient(options.client);
|
|
12
|
-
var watchQueryOptions = useWatchQueryOptions({ query: query, options: options });
|
|
13
|
-
var variables = watchQueryOptions.variables;
|
|
11
|
+
var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
|
|
12
|
+
var fetchPolicy = watchQueryOptions.fetchPolicy, variables = watchQueryOptions.variables;
|
|
14
13
|
var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
|
|
15
14
|
var cacheKey = [client, query, canonicalStringify(variables)].concat(queryKey);
|
|
16
15
|
var queryRef = suspenseCache.getQueryRef(cacheKey, function () {
|
|
17
16
|
return client.watchQuery(watchQueryOptions);
|
|
18
17
|
});
|
|
18
|
+
var currentFetchPolicy = queryRef.watchQueryOptions.fetchPolicy;
|
|
19
19
|
var _b = useState(function () { return new Map([[queryRef.key, queryRef.promise]]); }), promiseCache = _b[0], setPromiseCache = _b[1];
|
|
20
|
+
if (currentFetchPolicy === 'standby' && fetchPolicy !== currentFetchPolicy) {
|
|
21
|
+
var promise = queryRef.reobserve({ fetchPolicy: fetchPolicy });
|
|
22
|
+
promiseCache.set(queryRef.key, promise);
|
|
23
|
+
}
|
|
20
24
|
useTrackedQueryRefs(queryRef);
|
|
21
25
|
var fetchMore = useCallback(function (options) {
|
|
22
26
|
var promise = queryRef.fetchMore(options);
|
|
@@ -34,8 +38,9 @@ export function useBackgroundQuery(query, options) {
|
|
|
34
38
|
}, [queryRef]);
|
|
35
39
|
queryRef.promiseCache = promiseCache;
|
|
36
40
|
return useMemo(function () {
|
|
41
|
+
var _a;
|
|
37
42
|
return [
|
|
38
|
-
queryRef,
|
|
43
|
+
(_a = {}, _a[QUERY_REFERENCE_SYMBOL] = queryRef, _a),
|
|
39
44
|
{
|
|
40
45
|
fetchMore: fetchMore,
|
|
41
46
|
refetch: refetch,
|
|
@@ -43,27 +48,4 @@ export function useBackgroundQuery(query, options) {
|
|
|
43
48
|
];
|
|
44
49
|
}, [queryRef, fetchMore, refetch]);
|
|
45
50
|
}
|
|
46
|
-
export function useReadQuery(queryRef) {
|
|
47
|
-
var _a = useState(0), forceUpdate = _a[1];
|
|
48
|
-
invariant(queryRef.promiseCache, 49);
|
|
49
|
-
var promise = queryRef.promiseCache.get(queryRef.key);
|
|
50
|
-
if (!promise) {
|
|
51
|
-
promise = queryRef.promise;
|
|
52
|
-
queryRef.promiseCache.set(queryRef.key, promise);
|
|
53
|
-
}
|
|
54
|
-
useEffect(function () {
|
|
55
|
-
return queryRef.listen(function (promise) {
|
|
56
|
-
queryRef.promiseCache.set(queryRef.key, promise);
|
|
57
|
-
forceUpdate(function (prevState) { return prevState + 1; });
|
|
58
|
-
});
|
|
59
|
-
}, [queryRef]);
|
|
60
|
-
var result = __use(promise);
|
|
61
|
-
return useMemo(function () {
|
|
62
|
-
return {
|
|
63
|
-
data: result.data,
|
|
64
|
-
networkStatus: result.networkStatus,
|
|
65
|
-
error: toApolloError(result),
|
|
66
|
-
};
|
|
67
|
-
}, [result]);
|
|
68
|
-
}
|
|
69
51
|
//# sourceMappingURL=useBackgroundQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBackgroundQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useBackgroundQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useBackgroundQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAMvD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EACL,sBAAsB,GAEvB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAqGjD,MAAM,UAAU,kBAAkB,CAIhC,KAA0D,EAC1D,OAEC;IAFD,wBAAA,EAAA,UAAgE,MAAM,CAAC,MAAM,CAC3E,IAAI,CACL;IAED,IAAM,aAAa,GAAG,gBAAgB,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAC9D,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,IAAM,iBAAiB,GAAG,oBAAoB,CAAC,EAAE,MAAM,QAAA,EAAE,KAAK,OAAA,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CAAC;QAAhD,CAAgD,CACjD,CAAC;QAEF,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,IAAM,OAAO,GAAuC,WAAW,CAC7D,UAAC,SAAS;QACR,IAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAE5C,eAAe,CAAC,UAAC,YAAY;YAC3B,OAAA,IAAI,GAAG,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;QAAhD,CAAgD,CACjD,CAAC;QAEF,OAAO,OAAO,CAAC;IACjB,CAAC,EACD,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,QAAQ,CAAC,YAAY,GAAG,YAAY,CAAC;IAErC,OAAO,OAAO,CAAC;;QACb,OAAO;sBACH,GAAC,sBAAsB,IAAG,QAAQ;YACpC;gBACE,SAAS,WAAA;gBACT,OAAO,SAAA;aACR;SACF,CAAC;IACJ,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import { useState, useMemo, useCallback } from 'react';\nimport type {\n DocumentNode,\n OperationVariables,\n TypedDocumentNode,\n} from '../../core';\nimport { useApolloClient } from './useApolloClient';\nimport {\n QUERY_REFERENCE_SYMBOL,\n type QueryReference,\n} from '../cache/QueryReference';\nimport type { BackgroundQueryHookOptions, NoInfer } from '../types/types';\nimport { __use } from './internal';\nimport { useSuspenseCache } from './useSuspenseCache';\nimport { useTrackedQueryRefs, useWatchQueryOptions } from './useSuspenseQuery';\nimport type { FetchMoreFunction, RefetchFunction } from './useSuspenseQuery';\nimport { canonicalStringify } from '../../cache';\nimport type { DeepPartial } from '../../utilities';\n\nexport type UseBackgroundQueryResult<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables\n> = [\n QueryReference<TData>,\n {\n fetchMore: FetchMoreFunction<TData, TVariables>;\n refetch: RefetchFunction<TData, TVariables>;\n }\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): UseBackgroundQueryResult<\n TOptions['errorPolicy'] extends 'ignore' | 'all'\n ? TOptions['returnPartialData'] extends true\n ? DeepPartial<TData> | undefined\n : TData | undefined\n : TOptions['returnPartialData'] extends true\n ? TOptions['skip'] extends boolean\n ? DeepPartial<TData> | undefined\n : DeepPartial<TData>\n : TOptions['skip'] extends boolean\n ? TData | undefined\n : TData,\n 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): UseBackgroundQueryResult<DeepPartial<TData> | undefined, TVariables>;\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): UseBackgroundQueryResult<TData | undefined, TVariables>;\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): UseBackgroundQueryResult<DeepPartial<TData> | undefined, TVariables>;\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): UseBackgroundQueryResult<DeepPartial<TData>, TVariables>;\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): UseBackgroundQueryResult<TData | undefined, TVariables>;\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): UseBackgroundQueryResult<TData, TVariables>;\n\nexport function useBackgroundQuery<\n TData = unknown,\n TVariables extends OperationVariables = OperationVariables\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> = Object.create(\n null\n )\n): UseBackgroundQueryResult<TData> {\n const suspenseCache = useSuspenseCache(options.suspenseCache);\n const client = useApolloClient(options.client);\n const watchQueryOptions = useWatchQueryOptions({ client, query, options });\n const { fetchPolicy, variables } = watchQueryOptions;\n const { queryKey = [] } = options;\n\n const cacheKey = (\n [client, query, canonicalStringify(variables)] as any[]\n ).concat(queryKey);\n\n const queryRef = suspenseCache.getQueryRef(cacheKey, () =>\n client.watchQuery(watchQueryOptions)\n );\n\n const { fetchPolicy: currentFetchPolicy } = queryRef.watchQueryOptions;\n\n const [promiseCache, setPromiseCache] = useState(\n () => new Map([[queryRef.key, queryRef.promise]])\n );\n\n if (currentFetchPolicy === 'standby' && fetchPolicy !== currentFetchPolicy) {\n const promise = queryRef.reobserve({ fetchPolicy });\n promiseCache.set(queryRef.key, promise);\n }\n\n useTrackedQueryRefs(queryRef);\n\n const fetchMore: FetchMoreFunction<TData, TVariables> = useCallback(\n (options) => {\n const promise = queryRef.fetchMore(options);\n\n setPromiseCache((promiseCache) =>\n new Map(promiseCache).set(queryRef.key, promise)\n );\n\n return promise;\n },\n [queryRef]\n );\n\n const refetch: RefetchFunction<TData, TVariables> = useCallback(\n (variables) => {\n const promise = queryRef.refetch(variables);\n\n setPromiseCache((promiseCache) =>\n new Map(promiseCache).set(queryRef.key, promise)\n );\n\n return promise;\n },\n [queryRef]\n );\n\n queryRef.promiseCache = promiseCache;\n\n return useMemo(() => {\n return [\n { [QUERY_REFERENCE_SYMBOL]: queryRef },\n {\n fetchMore,\n refetch,\n },\n ];\n }, [queryRef, fetchMore, refetch]);\n}\n"]}
|
package/react/hooks/useQuery.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NetworkStatus } from "../../core";
|
|
2
|
+
import { type QueryReference } from "../cache/QueryReference";
|
|
3
|
+
export declare function useReadQuery<TData>(queryRef: QueryReference<TData>): {
|
|
4
|
+
data: TData;
|
|
5
|
+
networkStatus: NetworkStatus;
|
|
6
|
+
error: import("../../errors").ApolloError | undefined;
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=useReadQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useReadQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useReadQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAA0B,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAItF,wBAAgB,YAAY,CAAC,KAAK,EAAE,QAAQ,EAAE,cAAc,CAAC,KAAK,CAAC;;;;EA+ClE"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useState, useMemo, useEffect } from "react";
|
|
2
|
+
import invariant from "ts-invariant";
|
|
3
|
+
import { NetworkStatus } from "../../core/index.js";
|
|
4
|
+
import { QUERY_REFERENCE_SYMBOL } from "../cache/QueryReference.js";
|
|
5
|
+
import { __use } from "./internal/index.js";
|
|
6
|
+
import { toApolloError } from "./useSuspenseQuery.js";
|
|
7
|
+
export function useReadQuery(queryRef) {
|
|
8
|
+
var _a = useState(0), forceUpdate = _a[1];
|
|
9
|
+
var internalQueryRef = queryRef[QUERY_REFERENCE_SYMBOL];
|
|
10
|
+
invariant(internalQueryRef.promiseCache, 50);
|
|
11
|
+
var skipResult = useMemo(function () {
|
|
12
|
+
var error = toApolloError(internalQueryRef.result);
|
|
13
|
+
return {
|
|
14
|
+
loading: false,
|
|
15
|
+
data: internalQueryRef.result.data,
|
|
16
|
+
networkStatus: error ? NetworkStatus.error : NetworkStatus.ready,
|
|
17
|
+
error: error,
|
|
18
|
+
};
|
|
19
|
+
}, [internalQueryRef.result]);
|
|
20
|
+
var promise = internalQueryRef.promiseCache.get(internalQueryRef.key);
|
|
21
|
+
if (!promise) {
|
|
22
|
+
promise = internalQueryRef.promise;
|
|
23
|
+
internalQueryRef.promiseCache.set(internalQueryRef.key, promise);
|
|
24
|
+
}
|
|
25
|
+
useEffect(function () {
|
|
26
|
+
return internalQueryRef.listen(function (promise) {
|
|
27
|
+
internalQueryRef.promiseCache.set(internalQueryRef.key, promise);
|
|
28
|
+
forceUpdate(function (prevState) { return prevState + 1; });
|
|
29
|
+
});
|
|
30
|
+
}, [queryRef]);
|
|
31
|
+
var result = internalQueryRef.watchQueryOptions.fetchPolicy === 'standby'
|
|
32
|
+
? skipResult
|
|
33
|
+
: __use(promise);
|
|
34
|
+
return useMemo(function () {
|
|
35
|
+
return {
|
|
36
|
+
data: result.data,
|
|
37
|
+
networkStatus: result.networkStatus,
|
|
38
|
+
error: toApolloError(result),
|
|
39
|
+
};
|
|
40
|
+
}, [result]);
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=useReadQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useReadQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useReadQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE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gBAAgB,CAAC,MAAM,CAAC,UAAC,OAAO;YACrC,gBAAgB,CAAC,YAAa,CAAC,GAAG,CAAC,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAClE,WAAW,CAAC,UAAC,SAAS,IAAK,OAAA,SAAS,GAAG,CAAC,EAAb,CAAa,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,IAAM,MAAM,GACV,gBAAgB,CAAC,iBAAiB,CAAC,WAAW,KAAK,SAAS;QAC1D,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAErB,OAAO,OAAO,CAAC;QACb,OAAO;YACL,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,aAAa,EAAE,MAAM,CAAC,aAAa;YACnC,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC;SAC7B,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;AACf,CAAC","sourcesContent":["import { useState, useMemo, useEffect } from \"react\";\nimport invariant from \"ts-invariant\";\nimport { NetworkStatus } from \"../../core\";\nimport { QUERY_REFERENCE_SYMBOL, type QueryReference } from \"../cache/QueryReference\";\nimport { __use } from \"./internal\";\nimport { toApolloError } from \"./useSuspenseQuery\";\n\nexport function useReadQuery<TData>(queryRef: QueryReference<TData>) {\n const [, forceUpdate] = useState(0);\n const internalQueryRef = queryRef[QUERY_REFERENCE_SYMBOL];\n invariant(\n internalQueryRef.promiseCache,\n 'It appears that `useReadQuery` was used outside of `useBackgroundQuery`. ' +\n '`useReadQuery` is only supported for use with `useBackgroundQuery`. ' +\n 'Please ensure you are passing the `queryRef` returned from `useBackgroundQuery`.'\n );\n\n const skipResult = useMemo(() => {\n const error = toApolloError(internalQueryRef.result);\n\n return {\n loading: false,\n data: internalQueryRef.result.data,\n networkStatus: error ? NetworkStatus.error : NetworkStatus.ready,\n error,\n };\n }, [internalQueryRef.result]);\n\n let promise = internalQueryRef.promiseCache.get(internalQueryRef.key);\n\n if (!promise) {\n promise = internalQueryRef.promise;\n internalQueryRef.promiseCache.set(internalQueryRef.key, promise);\n }\n\n useEffect(() => {\n return internalQueryRef.listen((promise) => {\n internalQueryRef.promiseCache!.set(internalQueryRef.key, promise);\n forceUpdate((prevState) => prevState + 1);\n });\n }, [queryRef]);\n\n const result =\n internalQueryRef.watchQueryOptions.fetchPolicy === 'standby'\n ? skipResult\n : __use(promise);\n\n return useMemo(() => {\n return {\n data: result.data,\n networkStatus: result.networkStatus,\n error: toApolloError(result),\n };\n }, [result]);\n}\n"]}
|