UNPKG

3.33 kBJavaScriptView Raw
1import { __assign, __spreadArray } from "tslib";
2import * as React from "rehackt";
3import { useApolloClient } from "./useApolloClient.js";
4import { assertWrappedQueryRef, getSuspenseCache, unwrapQueryRef, updateWrappedQueryRef, wrapQueryRef, } from "../internal/index.js";
5import { useRenderGuard } from "./internal/index.js";
6import { useWatchQueryOptions } from "./useSuspenseQuery.js";
7import { canonicalStringify } from "../../cache/index.js";
8import { invariant } from "../../utilities/globals/index.js";
9export function useLoadableQuery(query, options) {
10 if (options === void 0) { options = Object.create(null); }
11 var client = useApolloClient(options.client);
12 var suspenseCache = getSuspenseCache(client);
13 var watchQueryOptions = useWatchQueryOptions({ client: client, query: query, options: options });
14 var _a = options.queryKey, queryKey = _a === void 0 ? [] : _a;
15 var _b = React.useState(null), queryRef = _b[0], setQueryRef = _b[1];
16 assertWrappedQueryRef(queryRef);
17 var internalQueryRef = queryRef && unwrapQueryRef(queryRef);
18 if (queryRef && (internalQueryRef === null || internalQueryRef === void 0 ? void 0 : internalQueryRef.didChangeOptions(watchQueryOptions))) {
19 var promise = internalQueryRef.applyOptions(watchQueryOptions);
20 updateWrappedQueryRef(queryRef, promise);
21 }
22 var calledDuringRender = useRenderGuard();
23 var fetchMore = React.useCallback(function (options) {
24 if (!internalQueryRef) {
25 throw new Error("The query has not been loaded. Please load the query.");
26 }
27 var promise = internalQueryRef.fetchMore(options);
28 setQueryRef(wrapQueryRef(internalQueryRef));
29 return promise;
30 }, [internalQueryRef]);
31 var refetch = React.useCallback(function (options) {
32 if (!internalQueryRef) {
33 throw new Error("The query has not been loaded. Please load the query.");
34 }
35 var promise = internalQueryRef.refetch(options);
36 setQueryRef(wrapQueryRef(internalQueryRef));
37 return promise;
38 }, [internalQueryRef]);
39 var loadQuery = React.useCallback(function () {
40 var args = [];
41 for (var _i = 0; _i < arguments.length; _i++) {
42 args[_i] = arguments[_i];
43 }
44 invariant(!calledDuringRender(), 51);
45 var variables = args[0];
46 var cacheKey = __spreadArray([
47 query,
48 canonicalStringify(variables)
49 ], [].concat(queryKey), true);
50 var queryRef = suspenseCache.getQueryRef(cacheKey, function () {
51 return client.watchQuery(__assign(__assign({}, watchQueryOptions), { variables: variables }));
52 });
53 setQueryRef(wrapQueryRef(queryRef));
54 }, [
55 query,
56 queryKey,
57 suspenseCache,
58 watchQueryOptions,
59 calledDuringRender,
60 client,
61 ]);
62 var subscribeToMore = React.useCallback(function (options) {
63 invariant(internalQueryRef, 52);
64 return internalQueryRef.observable.subscribeToMore(options);
65 }, [internalQueryRef]);
66 var reset = React.useCallback(function () {
67 setQueryRef(null);
68 }, []);
69 return [loadQuery, queryRef, { fetchMore: fetchMore, refetch: refetch, reset: reset, subscribeToMore: subscribeToMore }];
70}
71//# sourceMappingURL=useLoadableQuery.js.map
\No newline at end of file