UNPKG

5.73 kBJavaScriptView Raw
1import { __assign, __spreadArray } from "tslib";
2import * as React from "rehackt";
3import { invariant } from "../../utilities/globals/index.js";
4import { ApolloError, NetworkStatus } from "../../core/index.js";
5import { isNonEmptyArray } from "../../utilities/index.js";
6import { useApolloClient } from "./useApolloClient.js";
7import { DocumentType, verifyDocumentType } from "../parser/index.js";
8import { __use, useDeepMemo, wrapHook } from "./internal/index.js";
9import { getSuspenseCache } from "../internal/index.js";
10import { canonicalStringify } from "../../cache/index.js";
11import { skipToken } from "./constants.js";
12export function useSuspenseQuery(query, options) {
13 if (options === void 0) { options = Object.create(null); }
14 return wrapHook("useSuspenseQuery", _useSuspenseQuery, useApolloClient(typeof options === "object" ? options.client : undefined))(query, options);
15}
16function _useSuspenseQuery(query, options) {
17 var client = useApolloClient(options.client);
18 var suspenseCache = getSuspenseCache(client);
19 var watchQueryOptions = useWatchQueryOptions({
20 client: client,
21 query: query,
22 options: options,
23 });
24 var fetchPolicy = watchQueryOptions.fetchPolicy, variables = watchQueryOptions.variables;
25 var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
26 var cacheKey = __spreadArray([
27 query,
28 canonicalStringify(variables)
29 ], [].concat(queryKey), true);
30 var queryRef = suspenseCache.getQueryRef(cacheKey, function () {
31 return client.watchQuery(watchQueryOptions);
32 });
33 var _b = React.useState([queryRef.key, queryRef.promise]), current = _b[0], setPromise = _b[1];
34 // This saves us a re-execution of the render function when a variable changed.
35 if (current[0] !== queryRef.key) {
36 current[0] = queryRef.key;
37 current[1] = queryRef.promise;
38 }
39 var promise = current[1];
40 if (queryRef.didChangeOptions(watchQueryOptions)) {
41 current[1] = promise = queryRef.applyOptions(watchQueryOptions);
42 }
43 React.useEffect(function () {
44 var dispose = queryRef.retain();
45 var removeListener = queryRef.listen(function (promise) {
46 setPromise([queryRef.key, promise]);
47 });
48 return function () {
49 removeListener();
50 dispose();
51 };
52 }, [queryRef]);
53 var skipResult = React.useMemo(function () {
54 var error = toApolloError(queryRef.result);
55 return {
56 loading: false,
57 data: queryRef.result.data,
58 networkStatus: error ? NetworkStatus.error : NetworkStatus.ready,
59 error: error,
60 };
61 }, [queryRef.result]);
62 var result = fetchPolicy === "standby" ? skipResult : __use(promise);
63 var fetchMore = React.useCallback(function (options) {
64 var promise = queryRef.fetchMore(options);
65 setPromise([queryRef.key, queryRef.promise]);
66 return promise;
67 }, [queryRef]);
68 var refetch = React.useCallback(function (variables) {
69 var promise = queryRef.refetch(variables);
70 setPromise([queryRef.key, queryRef.promise]);
71 return promise;
72 }, [queryRef]);
73 var subscribeToMore = queryRef.observable.subscribeToMore;
74 return React.useMemo(function () {
75 return {
76 client: client,
77 data: result.data,
78 error: toApolloError(result),
79 networkStatus: result.networkStatus,
80 fetchMore: fetchMore,
81 refetch: refetch,
82 subscribeToMore: subscribeToMore,
83 };
84 }, [client, fetchMore, refetch, result, subscribeToMore]);
85}
86function validateOptions(options) {
87 var query = options.query, fetchPolicy = options.fetchPolicy, returnPartialData = options.returnPartialData;
88 verifyDocumentType(query, DocumentType.Query);
89 validateFetchPolicy(fetchPolicy);
90 validatePartialDataReturn(fetchPolicy, returnPartialData);
91}
92function validateFetchPolicy(fetchPolicy) {
93 if (fetchPolicy === void 0) { fetchPolicy = "cache-first"; }
94 var supportedFetchPolicies = [
95 "cache-first",
96 "network-only",
97 "no-cache",
98 "cache-and-network",
99 ];
100 invariant(supportedFetchPolicies.includes(fetchPolicy), 58, fetchPolicy);
101}
102function validatePartialDataReturn(fetchPolicy, returnPartialData) {
103 if (fetchPolicy === "no-cache" && returnPartialData) {
104 globalThis.__DEV__ !== false && invariant.warn(59);
105 }
106}
107export function toApolloError(result) {
108 return isNonEmptyArray(result.errors) ?
109 new ApolloError({ graphQLErrors: result.errors })
110 : result.error;
111}
112export function useWatchQueryOptions(_a) {
113 var client = _a.client, query = _a.query, options = _a.options;
114 return useDeepMemo(function () {
115 var _a;
116 if (options === skipToken) {
117 return { query: query, fetchPolicy: "standby" };
118 }
119 var fetchPolicy = options.fetchPolicy ||
120 ((_a = client.defaultOptions.watchQuery) === null || _a === void 0 ? void 0 : _a.fetchPolicy) ||
121 "cache-first";
122 var watchQueryOptions = __assign(__assign({}, options), { fetchPolicy: fetchPolicy, query: query, notifyOnNetworkStatusChange: false, nextFetchPolicy: void 0 });
123 if (globalThis.__DEV__ !== false) {
124 validateOptions(watchQueryOptions);
125 }
126 // Assign the updated fetch policy after our validation since `standby` is
127 // not a supported fetch policy on its own without the use of `skip`.
128 if (options.skip) {
129 watchQueryOptions.fetchPolicy = "standby";
130 }
131 return watchQueryOptions;
132 }, [client, options, query]);
133}
134//# sourceMappingURL=useSuspenseQuery.js.map
\No newline at end of file