1 | import { __assign, __spreadArray } from "tslib";
|
2 | import * as React from "rehackt";
|
3 | import { invariant } from "../../utilities/globals/index.js";
|
4 | import { ApolloError, NetworkStatus } from "../../core/index.js";
|
5 | import { isNonEmptyArray } from "../../utilities/index.js";
|
6 | import { useApolloClient } from "./useApolloClient.js";
|
7 | import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
8 | import { __use, useDeepMemo, wrapHook } from "./internal/index.js";
|
9 | import { getSuspenseCache } from "../internal/index.js";
|
10 | import { canonicalStringify } from "../../cache/index.js";
|
11 | import { skipToken } from "./constants.js";
|
12 | export 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 | }
|
16 | function _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 |
|
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 | }
|
86 | function 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 | }
|
92 | function 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 | }
|
102 | function validatePartialDataReturn(fetchPolicy, returnPartialData) {
|
103 | if (fetchPolicy === "no-cache" && returnPartialData) {
|
104 | globalThis.__DEV__ !== false && invariant.warn(59);
|
105 | }
|
106 | }
|
107 | export function toApolloError(result) {
|
108 | return isNonEmptyArray(result.errors) ?
|
109 | new ApolloError({ graphQLErrors: result.errors })
|
110 | : result.error;
|
111 | }
|
112 | export 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 |
|
127 |
|
128 | if (options.skip) {
|
129 | watchQueryOptions.fetchPolicy = "standby";
|
130 | }
|
131 | return watchQueryOptions;
|
132 | }, [client, options, query]);
|
133 | }
|
134 |
|
\ | No newline at end of file |