@apollo/client 3.6.0-beta.1 → 3.6.0-beta.10
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 +574 -421
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/cache/cache.cjs +36 -35
- package/cache/cache.cjs.map +1 -1
- package/cache/inmemory/helpers.d.ts +1 -0
- package/cache/inmemory/helpers.d.ts.map +1 -1
- package/cache/inmemory/helpers.js +4 -3
- package/cache/inmemory/helpers.js.map +1 -1
- package/cache/inmemory/key-extractor.js +7 -7
- package/cache/inmemory/key-extractor.js.map +1 -1
- package/cache/inmemory/object-canon.d.ts.map +1 -1
- package/cache/inmemory/object-canon.js +2 -1
- package/cache/inmemory/object-canon.js.map +1 -1
- package/cache/inmemory/policies.d.ts +1 -1
- package/cache/inmemory/policies.d.ts.map +1 -1
- package/cache/inmemory/policies.js +9 -9
- package/cache/inmemory/policies.js.map +1 -1
- package/cache/inmemory/readFromStore.js +6 -6
- package/cache/inmemory/readFromStore.js.map +1 -1
- package/cache/inmemory/writeToStore.js +13 -13
- package/cache/inmemory/writeToStore.js.map +1 -1
- package/core/ApolloClient.d.ts.map +1 -1
- package/core/ApolloClient.js +3 -3
- package/core/ApolloClient.js.map +1 -1
- package/core/LocalState.js +1 -1
- package/core/LocalState.js.map +1 -1
- package/core/ObservableQuery.d.ts +8 -2
- package/core/ObservableQuery.d.ts.map +1 -1
- package/core/ObservableQuery.js +83 -37
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.d.ts.map +1 -1
- package/core/QueryInfo.js +4 -2
- package/core/QueryInfo.js.map +1 -1
- package/core/QueryManager.d.ts +5 -2
- package/core/QueryManager.d.ts.map +1 -1
- package/core/QueryManager.js +19 -12
- package/core/QueryManager.js.map +1 -1
- package/core/core.cjs +113 -60
- package/core/core.cjs.map +1 -1
- package/core/index.d.ts +1 -1
- package/core/index.d.ts.map +1 -1
- package/core/index.js +1 -1
- package/core/index.js.map +1 -1
- package/core/watchQueryOptions.d.ts +8 -1
- package/core/watchQueryOptions.d.ts.map +1 -1
- package/core/watchQueryOptions.js.map +1 -1
- package/errors/errors.cjs +2 -2
- package/errors/errors.cjs.map +1 -1
- package/errors/index.js +2 -2
- package/errors/index.js.map +1 -1
- package/invariantErrorCodes.js +27 -27
- package/link/batch/batch.cjs +48 -38
- package/link/batch/batch.cjs.map +1 -1
- package/link/batch/batching.d.ts +2 -6
- package/link/batch/batching.d.ts.map +1 -1
- package/link/batch/batching.js +48 -38
- package/link/batch/batching.js.map +1 -1
- package/link/batch-http/batch-http.cjs +1 -1
- package/link/batch-http/batch-http.cjs.map +1 -1
- package/link/batch-http/batchHttpLink.js +2 -2
- package/link/batch-http/batchHttpLink.js.map +1 -1
- package/link/http/createHttpLink.js +2 -2
- package/link/http/createHttpLink.js.map +1 -1
- package/link/http/http.cjs +24 -13
- package/link/http/http.cjs.map +1 -1
- package/link/http/index.d.ts +1 -1
- package/link/http/index.d.ts.map +1 -1
- package/link/http/index.js +1 -1
- package/link/http/index.js.map +1 -1
- package/link/http/parseAndCheckHttpResponse.js +3 -3
- package/link/http/parseAndCheckHttpResponse.js.map +1 -1
- package/link/http/rewriteURIForGET.js +1 -1
- package/link/http/rewriteURIForGET.js.map +1 -1
- package/link/http/selectHttpOptionsAndBody.d.ts +5 -1
- package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
- package/link/http/selectHttpOptionsAndBody.js +19 -8
- package/link/http/selectHttpOptionsAndBody.js.map +1 -1
- package/link/http/serializeFetchParameter.js +1 -1
- package/link/http/serializeFetchParameter.js.map +1 -1
- package/link/subscriptions/index.d.ts +9 -0
- package/link/subscriptions/index.d.ts.map +1 -0
- package/link/subscriptions/index.js +39 -0
- package/link/subscriptions/index.js.map +1 -0
- package/link/subscriptions/package.json +8 -0
- package/link/subscriptions/subscriptions.cjs +45 -0
- package/link/subscriptions/subscriptions.cjs.map +1 -0
- package/link/utils/utils.cjs +1 -1
- package/link/utils/utils.cjs.map +1 -1
- package/link/utils/validateOperation.js +1 -1
- package/link/utils/validateOperation.js.map +1 -1
- package/main.cjs +4 -4
- package/package.json +27 -26
- package/react/components/components.cjs +3 -3
- package/react/context/context.cjs +3 -3
- package/react/hoc/hoc.cjs +9 -9
- package/react/hoc/hoc.cjs.map +1 -1
- package/react/hoc/mutation-hoc.js +2 -2
- package/react/hoc/mutation-hoc.js.map +1 -1
- package/react/hoc/query-hoc.js +1 -1
- package/react/hoc/query-hoc.js.map +1 -1
- package/react/hoc/subscription-hoc.js +1 -1
- package/react/hoc/subscription-hoc.js.map +1 -1
- package/react/hoc/withApollo.js +1 -1
- package/react/hoc/withApollo.js.map +1 -1
- package/react/hooks/hooks.cjs +345 -255
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/options.d.ts +3 -0
- package/react/hooks/options.d.ts.map +1 -0
- package/react/hooks/options.js +20 -0
- package/react/hooks/options.js.map +1 -0
- package/react/hooks/useApolloClient.js +2 -2
- package/react/hooks/useApolloClient.js.map +1 -1
- package/react/hooks/useLazyQuery.d.ts +2 -2
- package/react/hooks/useLazyQuery.d.ts.map +1 -1
- package/react/hooks/useLazyQuery.js +23 -28
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useMutation.d.ts.map +1 -1
- package/react/hooks/useMutation.js +59 -65
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts +2 -2
- package/react/hooks/useQuery.d.ts.map +1 -1
- package/react/hooks/useQuery.js +235 -154
- package/react/hooks/useQuery.js.map +1 -1
- package/react/hooks/useSubscription.d.ts.map +1 -1
- package/react/hooks/useSubscription.js +10 -8
- package/react/hooks/useSubscription.js.map +1 -1
- package/react/parser/index.js +7 -7
- package/react/parser/index.js.map +1 -1
- package/react/parser/parser.cjs +7 -7
- package/react/parser/parser.cjs.map +1 -1
- package/react/react.cjs +2 -2
- package/react/ssr/RenderPromises.d.ts +2 -2
- package/react/ssr/RenderPromises.d.ts.map +1 -1
- package/react/ssr/RenderPromises.js +7 -5
- package/react/ssr/RenderPromises.js.map +1 -1
- package/react/ssr/ssr.cjs +10 -8
- package/react/ssr/ssr.cjs.map +1 -1
- package/react/types/types.d.ts +4 -1
- package/react/types/types.d.ts.map +1 -1
- package/react/types/types.js.map +1 -1
- package/testing/core/core.cjs +2 -2
- package/testing/core/core.cjs.map +1 -1
- package/testing/core/mocking/mockFetch.js +2 -2
- package/testing/core/mocking/mockFetch.js.map +1 -1
- package/testing/core/mocking/mockLink.js +2 -2
- package/testing/core/mocking/mockLink.js.map +1 -1
- package/testing/core/observableToPromise.js +1 -1
- package/testing/core/observableToPromise.js.map +1 -1
- package/testing/testing.cjs +5 -5
- package/utilities/common/makeUniqueId.js +1 -1
- package/utilities/common/makeUniqueId.js.map +1 -1
- package/utilities/globals/fix-graphql.js +1 -1
- package/utilities/globals/global.d.ts +2 -8
- package/utilities/globals/global.d.ts.map +1 -1
- package/utilities/globals/global.js +1 -1
- package/utilities/globals/global.js.map +1 -1
- package/utilities/globals/globals.cjs +1 -1
- package/utilities/globals/globals.cjs.map +1 -1
- package/utilities/graphql/directives.js +4 -4
- package/utilities/graphql/directives.js.map +1 -1
- package/utilities/graphql/fragments.js +3 -3
- package/utilities/graphql/fragments.js.map +1 -1
- package/utilities/graphql/getFromAST.js +2 -2
- package/utilities/graphql/getFromAST.js.map +1 -1
- package/utilities/graphql/storeUtils.js +5 -5
- package/utilities/graphql/storeUtils.js.map +1 -1
- package/utilities/utilities.cjs +15 -15
- package/utilities/utilities.cjs.map +1 -1
- package/version.js +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE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,CAEf,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,aAAI,KAAK,OAAA,IAAK,MAAM,EAAG,CAAC;AACzC,CAAC","sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport {\n MutationFunctionOptions,\n MutationHookOptions,\n MutationResult,\n MutationTuple,\n} from '../types/types';\n\nimport {\n ApolloCache,\n DefaultContext,\n mergeOptions,\n OperationVariables,\n} from '../../core';\nimport { equal } from '@wry/equality';\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { ApolloError } from '../../errors';\nimport { useApolloClient } from './useApolloClient';\n\nexport function useMutation<\n TData = any,\n TVariables = OperationVariables,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n>(\n mutation: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: MutationHookOptions<TData, TVariables, TContext>,\n): MutationTuple<TData, TVariables, TContext, TCache> {\n const client = useApolloClient(options?.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n const [result, setResult] = useState<Omit<MutationResult, 'reset'>>({\n called: false,\n loading: false,\n client,\n });\n\n const ref = useRef({\n result,\n mutationId: 0,\n isMounted: true,\n client,\n mutation,\n options,\n });\n\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client, options, mutation });\n }\n\n const execute = useCallback((\n executeOptions: MutationFunctionOptions<\n TData,\n TVariables,\n TContext,\n TCache\n > = {}\n ) => {\n const {client, options, mutation} = ref.current;\n const baseOptions = { ...options, mutation };\n if (!ref.current.result.loading && !baseOptions.ignoreResults) {\n setResult(ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client,\n });\n }\n\n const mutationId = ++ref.current.mutationId;\n const clientOptions = mergeOptions(\n baseOptions,\n executeOptions as any,\n );\n\n return client.mutate(clientOptions).then((response) => {\n const { data, errors } = response;\n const error =\n errors && errors.length > 0\n ? new ApolloError({ graphQLErrors: errors })\n : void 0;\n\n if (\n mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults\n ) {\n const result = {\n called: true,\n loading: false,\n data,\n error,\n client,\n };\n\n if (ref.current.isMounted && !equal(ref.current.result, result)) {\n setResult(ref.current.result = result);\n }\n }\n\n baseOptions.onCompleted?.(response.data!);\n executeOptions.onCompleted?.(response.data!);\n return response;\n }).catch((error) => {\n if (\n mutationId === ref.current.mutationId &&\n ref.current.isMounted\n ) {\n const result = {\n loading: false,\n error,\n data: void 0,\n called: true,\n client,\n };\n\n if (!equal(ref.current.result, result)) {\n setResult(ref.current.result = result);\n }\n }\n\n if (baseOptions.onError || clientOptions.onError) {\n baseOptions.onError?.(error);\n executeOptions.onError?.(error);\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n\n throw error;\n });\n }, []);\n\n const reset = useCallback(() => {\n setResult({ called: false, loading: false, client });\n }, []);\n\n useEffect(() => () => {\n ref.current.isMounted = false;\n }, []);\n\n return [execute, { reset, ...result }];\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OperationVariables } from '../../core';
|
|
2
2
|
import { DocumentNode, TypedDocumentNode } from '../../core';
|
|
3
|
-
import { QueryHookOptions, QueryResult } from '../types/types';
|
|
4
|
-
export declare function useQuery<TData = any, TVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>,
|
|
3
|
+
import { QueryHookOptions, QueryResult, QueryHookOptionsFunction } from '../types/types';
|
|
4
|
+
export declare function useQuery<TData = any, TVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, optionsOrFunction?: QueryHookOptions<TData, TVariables> | QueryHookOptionsFunction<TData, TVariables>): QueryResult<TData, TVariables>;
|
|
5
5
|
//# sourceMappingURL=useQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGhD,OAAO,EAKL,YAAY,EACZ,iBAAiB,EAElB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gBAAgB,EAChB,WAAW,EAEX,wBAAwB,EACzB,MAAM,gBAAgB,CAAC;AAaxB,wBAAgB,QAAQ,CACtB,KAAK,GAAG,GAAG,EACX,UAAU,GAAG,kBAAkB,EAE/B,KAAK,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC1D,iBAAiB,CAAC,EACd,gBAAgB,CAAC,KAAK,EAAE,UAAU,CAAC,GACnC,wBAAwB,CAAC,KAAK,EAAE,UAAU,CAAC,GAC9C,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,CAMhC"}
|
package/react/hooks/useQuery.js
CHANGED
|
@@ -1,182 +1,263 @@
|
|
|
1
1
|
import { __assign, __rest } from "tslib";
|
|
2
|
-
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
-
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
2
|
+
import { useContext, useEffect, useMemo, useRef, useState, } from 'react';
|
|
4
3
|
import { equal } from '@wry/equality';
|
|
5
4
|
import { getApolloContext } from "../context/index.js";
|
|
6
5
|
import { ApolloError } from "../../errors/index.js";
|
|
7
6
|
import { NetworkStatus, } from "../../core/index.js";
|
|
8
7
|
import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
|
9
8
|
import { useApolloClient } from "./useApolloClient.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
9
|
+
import { canUseWeakMap, isNonEmptyArray } from "../../utilities/index.js";
|
|
10
|
+
import { useNormalizedOptions } from "./options.js";
|
|
11
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
12
|
+
export function useQuery(query, optionsOrFunction) {
|
|
13
|
+
var options = useNormalizedOptions(optionsOrFunction);
|
|
14
|
+
return useInternalState(useApolloClient(options.client), query).useQuery(options);
|
|
15
|
+
}
|
|
16
|
+
function useInternalState(client, query) {
|
|
17
|
+
var stateRef = useRef();
|
|
18
|
+
if (!stateRef.current ||
|
|
19
|
+
client !== stateRef.current.client ||
|
|
20
|
+
query !== stateRef.current.query) {
|
|
21
|
+
stateRef.current = new InternalState(client, query);
|
|
22
|
+
}
|
|
23
|
+
var state = stateRef.current;
|
|
24
|
+
var _a = useState(0), _tick = _a[0], setTick = _a[1];
|
|
25
|
+
state.forceUpdate = function () {
|
|
26
|
+
setTick(function (tick) { return tick + 1; });
|
|
27
|
+
};
|
|
28
|
+
return state;
|
|
29
|
+
}
|
|
30
|
+
var InternalState = (function () {
|
|
31
|
+
function InternalState(client, query) {
|
|
32
|
+
this.client = client;
|
|
33
|
+
this.query = query;
|
|
34
|
+
this.toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)();
|
|
35
|
+
verifyDocumentType(query, DocumentType.Query);
|
|
36
|
+
}
|
|
37
|
+
InternalState.prototype.forceUpdate = function () {
|
|
38
|
+
};
|
|
39
|
+
InternalState.prototype.useQuery = function (options) {
|
|
40
|
+
this.useOptions(options);
|
|
41
|
+
var obsQuery = this.useObservableQuery();
|
|
42
|
+
this.useSubscriptionEffect(obsQuery);
|
|
43
|
+
var result = this.getCurrentResult();
|
|
44
|
+
this.unsafeHandlePartialRefetch(result);
|
|
45
|
+
return this.toQueryResult(result);
|
|
46
|
+
};
|
|
47
|
+
InternalState.prototype.useOptions = function (options) {
|
|
48
|
+
this.renderPromises = useContext(getApolloContext()).renderPromises;
|
|
49
|
+
var watchQueryOptions = this.createWatchQueryOptions(this.queryHookOptions = options);
|
|
50
|
+
if (!equal(watchQueryOptions, this.watchQueryOptions)) {
|
|
51
|
+
this.watchQueryOptions = watchQueryOptions;
|
|
26
52
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
53
|
+
this.ssrDisabled = !!(options && (options.ssr === false ||
|
|
54
|
+
options.skip));
|
|
55
|
+
this.onCompleted = options
|
|
56
|
+
&& options.onCompleted
|
|
57
|
+
|| InternalState.prototype.onCompleted;
|
|
58
|
+
this.onError = options
|
|
59
|
+
&& options.onError
|
|
60
|
+
|| InternalState.prototype.onError;
|
|
61
|
+
};
|
|
62
|
+
InternalState.prototype.createWatchQueryOptions = function (_a) {
|
|
63
|
+
var _b;
|
|
64
|
+
if (_a === void 0) { _a = {}; }
|
|
65
|
+
var skip = _a.skip, ssr = _a.ssr, onCompleted = _a.onCompleted, onError = _a.onError, displayName = _a.displayName, otherOptions = __rest(_a, ["skip", "ssr", "onCompleted", "onError", "displayName"]);
|
|
66
|
+
var watchQueryOptions = Object.assign(otherOptions, { query: this.query });
|
|
67
|
+
if (skip) {
|
|
68
|
+
watchQueryOptions.fetchPolicy = 'standby';
|
|
32
69
|
}
|
|
33
|
-
if (context.renderPromises &&
|
|
34
|
-
(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
context.renderPromises.addQueryPromise({
|
|
38
|
-
getOptions: function () { return createWatchQueryOptions(query, options); },
|
|
39
|
-
fetchData: function () { return new Promise(function (resolve) {
|
|
40
|
-
var sub = obsQuery.subscribe({
|
|
41
|
-
next: function (result) {
|
|
42
|
-
if (!result.loading) {
|
|
43
|
-
resolve();
|
|
44
|
-
sub.unsubscribe();
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
error: function () {
|
|
48
|
-
resolve();
|
|
49
|
-
sub.unsubscribe();
|
|
50
|
-
},
|
|
51
|
-
complete: function () {
|
|
52
|
-
resolve();
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}); },
|
|
56
|
-
}, function () { return null; });
|
|
70
|
+
else if (((_b = watchQueryOptions.context) === null || _b === void 0 ? void 0 : _b.renderPromises) &&
|
|
71
|
+
(watchQueryOptions.fetchPolicy === 'network-only' ||
|
|
72
|
+
watchQueryOptions.fetchPolicy === 'cache-and-network')) {
|
|
73
|
+
watchQueryOptions.fetchPolicy = 'cache-first';
|
|
57
74
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (ref.current.client !== client || !equal(ref.current.query, query)) {
|
|
63
|
-
var obsQuery_1 = client.watchQuery(watchQueryOptions);
|
|
64
|
-
setObsQuery(obsQuery_1);
|
|
75
|
+
else if (!watchQueryOptions.fetchPolicy) {
|
|
76
|
+
var defaultOptions = this.client.defaultOptions.watchQuery;
|
|
77
|
+
watchQueryOptions.fetchPolicy =
|
|
78
|
+
defaultOptions && defaultOptions.fetchPolicy || 'cache-first';
|
|
65
79
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
setObsQuery(obsQuery);
|
|
80
|
+
if (!watchQueryOptions.variables) {
|
|
81
|
+
watchQueryOptions.variables = {};
|
|
69
82
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
83
|
+
return watchQueryOptions;
|
|
84
|
+
};
|
|
85
|
+
InternalState.prototype.onCompleted = function (data) { };
|
|
86
|
+
InternalState.prototype.onError = function (error) { };
|
|
87
|
+
InternalState.prototype.useObservableQuery = function () {
|
|
88
|
+
var _this = this;
|
|
89
|
+
var obsQuery = this.observable =
|
|
90
|
+
this.renderPromises
|
|
91
|
+
&& this.renderPromises.getSSRObservable(this.watchQueryOptions)
|
|
92
|
+
|| this.observable
|
|
93
|
+
|| this.client.watchQuery(this.watchQueryOptions);
|
|
94
|
+
this.obsQueryFields = useMemo(function () { return ({
|
|
95
|
+
refetch: obsQuery.refetch.bind(obsQuery),
|
|
96
|
+
fetchMore: obsQuery.fetchMore.bind(obsQuery),
|
|
97
|
+
updateQuery: obsQuery.updateQuery.bind(obsQuery),
|
|
98
|
+
startPolling: obsQuery.startPolling.bind(obsQuery),
|
|
99
|
+
stopPolling: obsQuery.stopPolling.bind(obsQuery),
|
|
100
|
+
subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
|
|
101
|
+
}); }, [obsQuery]);
|
|
102
|
+
if (this.renderPromises) {
|
|
103
|
+
this.renderPromises.registerSSRObservable(obsQuery);
|
|
104
|
+
if (!this.ssrDisabled && obsQuery.getCurrentResult().loading) {
|
|
105
|
+
this.renderPromises.addQueryPromise({
|
|
106
|
+
getOptions: function () { return obsQuery.options; },
|
|
107
|
+
fetchData: function () { return new Promise(function (resolve) {
|
|
108
|
+
var sub = obsQuery.subscribe({
|
|
109
|
+
next: function (result) {
|
|
110
|
+
if (!result.loading) {
|
|
111
|
+
resolve();
|
|
112
|
+
sub.unsubscribe();
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
error: function () {
|
|
116
|
+
resolve();
|
|
117
|
+
sub.unsubscribe();
|
|
118
|
+
},
|
|
119
|
+
complete: function () {
|
|
120
|
+
resolve();
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}); },
|
|
124
|
+
}, function () { return null; });
|
|
125
|
+
obsQuery.setOptions(this.watchQueryOptions).catch(function () { });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
var prevOptionsRef = useRef({
|
|
129
|
+
watchQueryOptions: this.watchQueryOptions,
|
|
75
130
|
});
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
131
|
+
useEffect(function () {
|
|
132
|
+
if (_this.renderPromises) {
|
|
133
|
+
}
|
|
134
|
+
else if (_this.watchQueryOptions !== prevOptionsRef.current.watchQueryOptions) {
|
|
135
|
+
obsQuery.setOptions(_this.watchQueryOptions).catch(function () { });
|
|
136
|
+
prevOptionsRef.current.watchQueryOptions = _this.watchQueryOptions;
|
|
137
|
+
_this.setResult(obsQuery.getCurrentResult());
|
|
138
|
+
}
|
|
139
|
+
}, [obsQuery, this.watchQueryOptions]);
|
|
140
|
+
return obsQuery;
|
|
141
|
+
};
|
|
142
|
+
InternalState.prototype.useSubscriptionEffect = function (obsQuery) {
|
|
143
|
+
var _this = this;
|
|
144
|
+
useEffect(function () {
|
|
145
|
+
if (_this.renderPromises) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
var onNext = function () {
|
|
149
|
+
var previousResult = _this.result;
|
|
150
|
+
var result = obsQuery.getCurrentResult();
|
|
151
|
+
if (previousResult &&
|
|
152
|
+
previousResult.loading === result.loading &&
|
|
153
|
+
previousResult.networkStatus === result.networkStatus &&
|
|
154
|
+
equal(previousResult.data, result.data)) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
_this.setResult(result);
|
|
158
|
+
};
|
|
159
|
+
var onError = function (error) {
|
|
84
160
|
var last = obsQuery["last"];
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
161
|
+
subscription.unsubscribe();
|
|
162
|
+
try {
|
|
163
|
+
obsQuery.resetLastResults();
|
|
164
|
+
subscription = obsQuery.subscribe(onNext, onError);
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
obsQuery["last"] = last;
|
|
168
|
+
}
|
|
169
|
+
if (!hasOwnProperty.call(error, 'graphQLErrors')) {
|
|
89
170
|
throw error;
|
|
90
171
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (!previousResult ||
|
|
102
|
-
previousResult.loading !== result.loading ||
|
|
103
|
-
previousResult.networkStatus !== result.networkStatus ||
|
|
104
|
-
!equal(previousResult.data, result.data) ||
|
|
105
|
-
!equal(previousResult.error, result.error)) {
|
|
106
|
-
if (previousResult) {
|
|
107
|
-
result = __assign(__assign({}, result), { previousData: previousResult.data || previousResult.previousData });
|
|
172
|
+
var previousResult = _this.result;
|
|
173
|
+
if (!previousResult ||
|
|
174
|
+
(previousResult && previousResult.loading) ||
|
|
175
|
+
!equal(error, previousResult.error)) {
|
|
176
|
+
_this.setResult({
|
|
177
|
+
data: (previousResult && previousResult.data),
|
|
178
|
+
error: error,
|
|
179
|
+
loading: false,
|
|
180
|
+
networkStatus: NetworkStatus.error,
|
|
181
|
+
});
|
|
108
182
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}); }, [obsQuery]);
|
|
123
|
-
var result = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
124
|
-
useEffect(function () {
|
|
125
|
-
var _a, _b, _c, _d, _e, _f;
|
|
126
|
-
if (((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) ||
|
|
127
|
-
((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) === 'standby') {
|
|
128
|
-
return;
|
|
183
|
+
};
|
|
184
|
+
var subscription = obsQuery.subscribe(onNext, onError);
|
|
185
|
+
return function () { return subscription.unsubscribe(); };
|
|
186
|
+
}, [
|
|
187
|
+
obsQuery,
|
|
188
|
+
this.renderPromises,
|
|
189
|
+
this.client.disableNetworkFetches,
|
|
190
|
+
]);
|
|
191
|
+
};
|
|
192
|
+
InternalState.prototype.setResult = function (nextResult) {
|
|
193
|
+
var previousResult = this.result;
|
|
194
|
+
if (previousResult && previousResult.data) {
|
|
195
|
+
this.previousData = previousResult.data;
|
|
129
196
|
}
|
|
197
|
+
this.result = nextResult;
|
|
198
|
+
this.forceUpdate();
|
|
199
|
+
this.handleErrorOrCompleted(nextResult);
|
|
200
|
+
};
|
|
201
|
+
InternalState.prototype.handleErrorOrCompleted = function (result) {
|
|
130
202
|
if (!result.loading) {
|
|
131
203
|
if (result.error) {
|
|
132
|
-
|
|
204
|
+
this.onError(result.error);
|
|
133
205
|
}
|
|
134
206
|
else if (result.data) {
|
|
135
|
-
|
|
207
|
+
this.onCompleted(result.data);
|
|
136
208
|
}
|
|
137
209
|
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
210
|
+
};
|
|
211
|
+
InternalState.prototype.getCurrentResult = function () {
|
|
212
|
+
var result = this.result;
|
|
213
|
+
if (!result) {
|
|
214
|
+
result = this.result = this.observable.getCurrentResult();
|
|
215
|
+
this.handleErrorOrCompleted(result);
|
|
216
|
+
}
|
|
217
|
+
if ((this.renderPromises || this.client.disableNetworkFetches) &&
|
|
218
|
+
this.queryHookOptions.ssr === false) {
|
|
219
|
+
result = {
|
|
220
|
+
loading: true,
|
|
221
|
+
data: void 0,
|
|
222
|
+
error: void 0,
|
|
223
|
+
networkStatus: NetworkStatus.loading,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
else if (this.queryHookOptions.skip ||
|
|
227
|
+
this.queryHookOptions.fetchPolicy === 'standby') {
|
|
228
|
+
result = {
|
|
229
|
+
loading: false,
|
|
230
|
+
data: void 0,
|
|
231
|
+
error: void 0,
|
|
232
|
+
networkStatus: NetworkStatus.ready,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return result;
|
|
236
|
+
};
|
|
237
|
+
InternalState.prototype.toQueryResult = function (result) {
|
|
238
|
+
var queryResult = this.toQueryResultCache.get(result);
|
|
239
|
+
if (queryResult)
|
|
240
|
+
return queryResult;
|
|
241
|
+
var data = result.data, partial = result.partial, resultWithoutPartial = __rest(result, ["data", "partial"]);
|
|
242
|
+
this.toQueryResultCache.set(result, queryResult = __assign(__assign(__assign({ data: data }, resultWithoutPartial), this.obsQueryFields), { client: this.client, observable: this.observable, variables: this.observable.variables, called: true, previousData: this.previousData }));
|
|
243
|
+
if (!queryResult.error && isNonEmptyArray(result.errors)) {
|
|
244
|
+
queryResult.error = new ApolloError({ graphQLErrors: result.errors });
|
|
245
|
+
}
|
|
246
|
+
return queryResult;
|
|
247
|
+
};
|
|
248
|
+
InternalState.prototype.unsafeHandlePartialRefetch = function (result) {
|
|
249
|
+
if (result.partial &&
|
|
250
|
+
this.queryHookOptions.partialRefetch &&
|
|
152
251
|
!result.loading &&
|
|
153
252
|
(!result.data || Object.keys(result.data).length === 0) &&
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
.catch(function () { });
|
|
253
|
+
this.observable.options.fetchPolicy !== 'cache-only') {
|
|
254
|
+
Object.assign(result, {
|
|
255
|
+
loading: true,
|
|
256
|
+
networkStatus: NetworkStatus.refetch,
|
|
257
|
+
});
|
|
258
|
+
this.observable.refetch();
|
|
161
259
|
}
|
|
162
|
-
}
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
function createWatchQueryOptions(query, options) {
|
|
166
|
-
var _a;
|
|
167
|
-
if (options === void 0) { options = {}; }
|
|
168
|
-
var skip = options.skip, ssr = options.ssr, onCompleted = options.onCompleted, onError = options.onError, displayName = options.displayName, watchQueryOptions = __rest(options, ["skip", "ssr", "onCompleted", "onError", "displayName"]);
|
|
169
|
-
if (skip) {
|
|
170
|
-
watchQueryOptions.fetchPolicy = 'standby';
|
|
171
|
-
}
|
|
172
|
-
else if (((_a = watchQueryOptions.context) === null || _a === void 0 ? void 0 : _a.renderPromises) &&
|
|
173
|
-
(watchQueryOptions.fetchPolicy === 'network-only' ||
|
|
174
|
-
watchQueryOptions.fetchPolicy === 'cache-and-network')) {
|
|
175
|
-
watchQueryOptions.fetchPolicy = 'cache-first';
|
|
176
|
-
}
|
|
177
|
-
else if (!watchQueryOptions.fetchPolicy) {
|
|
178
|
-
watchQueryOptions.fetchPolicy = 'cache-first';
|
|
179
|
-
}
|
|
180
|
-
return __assign({ query: query }, watchQueryOptions);
|
|
181
|
-
}
|
|
260
|
+
};
|
|
261
|
+
return InternalState;
|
|
262
|
+
}());
|
|
182
263
|
//# sourceMappingURL=useQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAEL,aAAa,GAKd,MAAM,YAAY,CAAC;AAMpB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,MAAM,UAAU,QAAQ,CAItB,KAA0D,EAC1D,OAA6C;;IAE7C,IAAM,OAAO,GAAG,UAAU,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAC/C,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,KAAK,EAAE,YAAY,CAAC,KAAK,CAAC,CAAC;IAC9C,IAAM,GAAG,GAAG,MAAM,CAAC;QACjB,MAAM,QAAA;QACN,KAAK,OAAA;QACL,OAAO,SAAA;QACP,iB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MAAM,EAAE,IAAI,KACT,MAAM,EACT;AACJ,CAAC;AAED,SAAS,uBAAuB,CAC9B,KAA0D,EAC1D,OAAiD;;IAAjD,wBAAA,EAAA,YAAiD;IAI/C,IAAA,IAAI,GAMF,OAAO,KANL,EACJ,GAAG,GAKD,OAAO,IALN,EACH,WAAW,GAIT,OAAO,YAJE,EACX,OAAO,GAGL,OAAO,QAHF,EACP,WAAW,GAET,OAAO,YAFE,EACR,iBAAiB,UAClB,OAAO,EAPL,wDAOL,CADqB,CACV;IAIZ,IAAI,IAAI,EAAE;QACR,iBAAiB,CAAC,WAAW,GAAG,SAAS,CAAC;KAC3C;SAAM,IACL,CAAA,MAAA,iBAAiB,CAAC,OAAO,0CAAE,cAAc;QACzC,CACE,iBAAiB,CAAC,WAAW,KAAK,cAAc;YAChD,iBAAiB,CAAC,WAAW,KAAK,mBAAmB,CACtD,EACD;QAGA,iBAAiB,CAAC,WAAW,GAAG,aAAa,CAAC;KAC/C;SAAM,IAAI,CAAC,iBAAiB,CAAC,WAAW,EAAE;QAGzC,iBAAiB,CAAC,WAAW,GAAG,aAAa,CAAC;KAC/C;IAED,kBAAS,KAAK,OAAA,IAAK,iBAAiB,EAAG;AACzC,CAAC","sourcesContent":["import { useContext, useEffect, useMemo, useRef, useState } from 'react';\nimport { useSyncExternalStore } from 'use-sync-external-store/shim';\nimport { equal } from '@wry/equality';\nimport { OperationVariables } from '../../core';\nimport { getApolloContext } from '../context';\nimport { ApolloError } from '../../errors';\nimport {\n ApolloQueryResult,\n NetworkStatus,\n ObservableQuery,\n DocumentNode,\n TypedDocumentNode,\n WatchQueryOptions,\n} from '../../core';\nimport {\n QueryHookOptions,\n QueryResult,\n} from '../types/types';\n\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { useApolloClient } from './useApolloClient';\n\nexport function useQuery<\n TData = any,\n TVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: QueryHookOptions<TData, TVariables>,\n): QueryResult<TData, TVariables> {\n const context = useContext(getApolloContext());\n const client = useApolloClient(options?.client);\n verifyDocumentType(query, DocumentType.Query);\n const ref = useRef({\n client,\n query,\n options,\n watchQueryOptions: createWatchQueryOptions(query, options),\n });\n\n const [obsQuery, setObsQuery] = useState(() => {\n const watchQueryOptions = createWatchQueryOptions(query, options);\n // See if there is an existing observable that was used to fetch the same\n // data and if so, use it instead since it will contain the proper queryId\n // to fetch the result set. This is used during SSR.\n let obsQuery: ObservableQuery<TData, TVariables> | null = null;\n if (context.renderPromises) {\n obsQuery = context.renderPromises.getSSRObservable(watchQueryOptions);\n }\n\n if (!obsQuery) {\n // Is it safe (StrictMode/memory-wise) to call client.watchQuery here?\n obsQuery = client.watchQuery(watchQueryOptions);\n if (context.renderPromises) {\n context.renderPromises.registerSSRObservable(\n obsQuery,\n watchQueryOptions,\n );\n }\n }\n\n if (\n context.renderPromises &&\n options?.ssr !== false &&\n !options?.skip &&\n obsQuery.getCurrentResult().loading\n ) {\n // TODO: This is a legacy API which could probably be cleaned up\n context.renderPromises.addQueryPromise(\n {\n // The only options which seem to actually be used by the\n // RenderPromises class are query and variables.\n getOptions: () => createWatchQueryOptions(query, options),\n fetchData: () => new Promise<void>((resolve) => {\n const sub = obsQuery!.subscribe({\n next(result) {\n if (!result.loading) {\n resolve()\n sub.unsubscribe();\n }\n },\n error() {\n resolve();\n sub.unsubscribe();\n },\n complete() {\n // TODO: Does this branch ever get called before next() and error()?\n resolve();\n },\n });\n }),\n },\n // This callback never seemed to do anything\n () => null,\n );\n }\n\n return obsQuery;\n });\n\n // An effect to recreate the obsQuery whenever the client or query changes.\n // This effect is also responsible for updating the obsQuery options whenever\n // they change.\n useEffect(() => {\n const watchQueryOptions = createWatchQueryOptions(query, options);\n if (ref.current.client !== client || !equal(ref.current.query, query)) {\n const obsQuery = client.watchQuery(watchQueryOptions);\n setObsQuery(obsQuery);\n } else if (!equal(ref.current.watchQueryOptions, watchQueryOptions)) {\n obsQuery.setOptions(watchQueryOptions);\n // We call setObsQuery to rerender the hook.\n setObsQuery(obsQuery);\n }\n\n Object.assign(ref.current, {\n client,\n query,\n options,\n watchQueryOptions,\n });\n }, [obsQuery, client, query, options]);\n\n const [subscribe, getSnapshot] = useMemo(() => {\n let previousResult: ApolloQueryResult<TData> | undefined;\n const subscribe = (forceUpdate: () => void) => {\n let subscription = obsQuery.subscribe(forceUpdate, onError);\n function onError(error: Error) {\n forceUpdate();\n subscription.unsubscribe();\n const last = obsQuery[\"last\"];\n obsQuery.resetLastResults();\n obsQuery.subscribe(forceUpdate, onError);\n obsQuery[\"last\"] = last;\n if (!error.hasOwnProperty('graphQLErrors')) {\n // The error is not a GraphQL error\n throw error;\n }\n }\n\n return () => {\n subscription.unsubscribe();\n };\n };\n\n const getSnapshot = () => {\n let result = obsQuery.getCurrentResult();\n if (result.errors && result.errors.length) {\n // Until a set naming convention for networkError and graphQLErrors is\n // decided upon, we map errors (graphQLErrors) to the error options.\n // TODO: Is it possible for both result.error and result.errors to be\n // defined here?\n result = {\n ...result,\n error:\n result.error || new ApolloError({ graphQLErrors: result.errors }),\n };\n }\n\n if (\n !previousResult ||\n previousResult.loading !== result.loading ||\n previousResult.networkStatus !== result.networkStatus ||\n !equal(previousResult.data, result.data) ||\n !equal(previousResult.error, result.error)\n ) {\n if (previousResult) {\n result = {\n ...result,\n previousData: previousResult.data || (previousResult as any).previousData,\n } as ApolloQueryResult<TData>;\n }\n\n previousResult = result;\n }\n\n return previousResult;\n };\n\n return [subscribe, getSnapshot];\n }, [obsQuery]);\n\n const obsQueryMethods = useMemo(() => ({\n refetch: obsQuery.refetch.bind(obsQuery),\n fetchMore: obsQuery.fetchMore.bind(obsQuery),\n updateQuery: obsQuery.updateQuery.bind(obsQuery),\n startPolling: obsQuery.startPolling.bind(obsQuery),\n stopPolling: obsQuery.stopPolling.bind(obsQuery),\n subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),\n }), [obsQuery]);\n\n let result = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n useEffect(() => {\n if (\n ref.current.options?.skip ||\n ref.current.options?.fetchPolicy === 'standby'\n ) {\n return;\n }\n\n if (!result.loading) {\n if (result.error) {\n ref.current.options?.onError?.(result.error);\n } else if (result.data) {\n ref.current.options?.onCompleted?.(result.data);\n }\n }\n }, [result]);\n\n let partial: boolean | undefined;\n ({ partial, ...result } = result);\n if (options?.skip || options?.fetchPolicy === 'standby') {\n // When skipping a query (ie. we're not querying for data but still want to\n // render children), make sure the `data` is cleared out and `loading` is\n // set to `false` (since we aren't loading anything).\n //\n // NOTE: We no longer think this is the correct behavior. Skipping should\n // not automatically set `data` to `undefined`, but instead leave the\n // previous data in place. In other words, skipping should not mandate that\n // previously received data is all of a sudden removed. Unfortunately,\n // changing this is breaking, so we'll have to wait until Apollo Client 4.0\n // to address this.\n result = {\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.ready,\n };\n } else {\n // BAD BOY CODE BLOCK WHERE WE PUT SIDE-EFFECTS IN THE RENDER FUNCTION\n // TODO: This code should be removed when the partialRefetch option is\n // removed. I was unable to get this hook to behave reasonably in certain\n // edge cases when this block was put in an effect.\n if (\n partial &&\n options?.partialRefetch &&\n !result.loading &&\n (!result.data || Object.keys(result.data).length === 0) &&\n obsQuery.options.fetchPolicy !== 'cache-only'\n ) {\n result = {\n ...result,\n loading: true,\n networkStatus: NetworkStatus.refetch,\n };\n\n obsQuery.refetch();\n }\n\n // TODO: This is a hack to make sure useLazyQuery executions update the\n // obsevable query options for ssr.\n if (context.renderPromises && options?.ssr !== false && result.loading) {\n obsQuery.setOptions(createWatchQueryOptions(query, options))\n .catch(() => {});\n }\n }\n\n return {\n ...obsQueryMethods,\n variables: obsQuery.variables,\n client,\n called: true,\n ...result,\n };\n}\n\nfunction createWatchQueryOptions<TData, TVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: QueryHookOptions<TData, TVariables> = {},\n): WatchQueryOptions<TVariables, TData> {\n // Using destructuring to remove hook specific options.\n const {\n skip,\n ssr,\n onCompleted,\n onError,\n displayName,\n ...watchQueryOptions\n } = options;\n // TODO: For some reason, we pass context, which is the React Apollo Context,\n // into observable queries, and test for that.\n\n if (skip) {\n watchQueryOptions.fetchPolicy = 'standby';\n } else if (\n watchQueryOptions.context?.renderPromises &&\n (\n watchQueryOptions.fetchPolicy === 'network-only' ||\n watchQueryOptions.fetchPolicy === 'cache-and-network'\n )\n ) {\n // this behavior was added to react-apollo without explanation in this PR\n // https://github.com/apollographql/react-apollo/pull/1579\n watchQueryOptions.fetchPolicy = 'cache-first';\n } else if (!watchQueryOptions.fetchPolicy) {\n // cache-first is the default policy, but we explicitly assign it here so\n // the cache policies computed based on options can be cleared\n watchQueryOptions.fetchPolicy = 'cache-first';\n }\n\n return { query, ...watchQueryOptions };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,GACjD,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAsB,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAGL,aAAa,GAKd,MAAM,YAAY,CAAC;AAQpB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAI7C,IAAA,cAAc,GAEd,MAAM,yBAFQ,CAEP;AAEX,MAAM,UAAU,QAAQ,CAItB,KAA0D,EAC1D,iBAE+C;IAE/C,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OAAO,EAAE,IAAI;gBACb,aAAa,EAAE,aAAa,CAAC,OAAO;aACrC,CAAC,CAAC;YACH,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;SAC3B;IACH,CAAC;IACH,oBAAC;AAAD,CAAC,AAlaD,IAkaC","sourcesContent":["import {\n useContext, useEffect, useMemo, useRef, useState,\n} from 'react';\nimport { equal } from '@wry/equality';\nimport { OperationVariables } from '../../core';\nimport { ApolloContextValue, getApolloContext } from '../context';\nimport { ApolloError } from '../../errors';\nimport {\n ApolloClient,\n ApolloQueryResult,\n NetworkStatus,\n ObservableQuery,\n DocumentNode,\n TypedDocumentNode,\n WatchQueryOptions,\n} from '../../core';\nimport {\n QueryHookOptions,\n QueryResult,\n ObservableQueryFields,\n QueryHookOptionsFunction,\n} from '../types/types';\n\nimport { DocumentType, verifyDocumentType } from '../parser';\nimport { useApolloClient } from './useApolloClient';\nimport { canUseWeakMap, isNonEmptyArray } from '../../utilities';\nimport { useNormalizedOptions } from './options';\n\nconst {\n prototype: {\n hasOwnProperty,\n },\n} = Object;\n\nexport function useQuery<\n TData = any,\n TVariables = OperationVariables,\n>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n optionsOrFunction?:\n | QueryHookOptions<TData, TVariables>\n | QueryHookOptionsFunction<TData, TVariables>\n): QueryResult<TData, TVariables> {\n const options = useNormalizedOptions(optionsOrFunction);\n return useInternalState(\n useApolloClient(options.client),\n query,\n ).useQuery(options);\n}\n\nfunction useInternalState<TData, TVariables>(\n client: ApolloClient<any>,\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n) {\n const stateRef = useRef<InternalState<TData, TVariables>>();\n if (\n !stateRef.current ||\n client !== stateRef.current.client ||\n query !== stateRef.current.query\n ) {\n stateRef.current = new InternalState(client, query);\n }\n const state = stateRef.current;\n\n // By default, InternalState.prototype.forceUpdate is an empty function, but\n // we replace it here (before anyone has had a chance to see this state yet)\n // with a function that unconditionally forces an update, using the latest\n // setTick function. Updating this state by calling state.forceUpdate is the\n // only way we trigger React component updates (no other useState calls within\n // the InternalState class).\n const [_tick, setTick] = useState(0);\n state.forceUpdate = () => {\n setTick(tick => tick + 1);\n };\n\n return state;\n}\n\nclass InternalState<TData, TVariables> {\n constructor(\n public readonly client: ReturnType<typeof useApolloClient>,\n public readonly query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n ) {\n verifyDocumentType(query, DocumentType.Query);\n }\n\n public forceUpdate() {\n // Replaced (in useInternalState) with a method that triggers an update.\n }\n\n // Methods beginning with use- should be called according to the standard\n // rules of React hooks: only at the top level of the calling function, and\n // without any dynamic conditional logic.\n public useQuery(options: QueryHookOptions<TData, TVariables>) {\n this.useOptions(options);\n\n const obsQuery = this.useObservableQuery();\n this.useSubscriptionEffect(obsQuery);\n\n const result = this.getCurrentResult();\n\n // TODO Remove this method when we remove support for options.partialRefetch.\n this.unsafeHandlePartialRefetch(result);\n\n return this.toQueryResult(result);\n }\n\n // These members are all populated by the useOptions method, which is called\n // unconditionally at the beginning of the useQuery method, so we can safely\n // use these members in other/later methods without worrying about the\n // possibility they might be undefined.\n private renderPromises: ApolloContextValue[\"renderPromises\"];\n private queryHookOptions: QueryHookOptions<TData, TVariables>;\n private watchQueryOptions: WatchQueryOptions<TVariables, TData>;\n private ssrDisabled: boolean;\n\n private useOptions(\n options: QueryHookOptions<TData, TVariables>,\n ) {\n this.renderPromises = useContext(getApolloContext()).renderPromises;\n\n const watchQueryOptions = this.createWatchQueryOptions(\n this.queryHookOptions = options,\n );\n // Update this.watchQueryOptions, but only when they have changed, which\n // allows us to depend on the referential stability of\n // this.watchQueryOptions elsewhere.\n if (!equal(watchQueryOptions, this.watchQueryOptions)) {\n this.watchQueryOptions = watchQueryOptions;\n }\n\n this.ssrDisabled = !!(options && (\n options.ssr === false ||\n options.skip\n ));\n\n // Make sure state.onCompleted and state.onError always reflect the latest\n // options.onCompleted and options.onError callbacks provided to useQuery,\n // since those functions are often recreated every time useQuery is called.\n // Like the forceUpdate method, the versions of these methods inherited from\n // InternalState.prototype are empty no-ops, but we can override them on the\n // base state object (without modifying the prototype).\n\n this.onCompleted = options\n && options.onCompleted\n || InternalState.prototype.onCompleted;\n\n this.onError = options\n && options.onError\n || InternalState.prototype.onError;\n }\n\n // A function to massage options before passing them to ObservableQuery.\n private createWatchQueryOptions<TData, TVariables>({\n skip,\n ssr,\n onCompleted,\n onError,\n displayName,\n // The above options are useQuery-specific, so this ...otherOptions spread\n // makes otherOptions almost a WatchQueryOptions object, except for the\n // query property that we add below.\n ...otherOptions\n }: QueryHookOptions<TData, TVariables> = {}): WatchQueryOptions<TVariables, TData> {\n // TODO: For some reason, we pass context, which is the React Apollo Context,\n // into observable queries, and test for that.\n const watchQueryOptions: WatchQueryOptions<TVariables, TData> =\n Object.assign(otherOptions, { query: this.query });\n\n if (skip) {\n watchQueryOptions.fetchPolicy = 'standby';\n } else if (\n watchQueryOptions.context?.renderPromises &&\n (\n watchQueryOptions.fetchPolicy === 'network-only' ||\n watchQueryOptions.fetchPolicy === 'cache-and-network'\n )\n ) {\n // this behavior was added to react-apollo without explanation in this PR\n // https://github.com/apollographql/react-apollo/pull/1579\n watchQueryOptions.fetchPolicy = 'cache-first';\n } else if (!watchQueryOptions.fetchPolicy) {\n const defaultOptions = this.client.defaultOptions.watchQuery;\n // cache-first is the default policy, but we explicitly assign it here so\n // the cache policies computed based on options can be cleared\n watchQueryOptions.fetchPolicy =\n defaultOptions && defaultOptions.fetchPolicy || 'cache-first';\n }\n\n if (!watchQueryOptions.variables) {\n watchQueryOptions.variables = {} as TVariables;\n }\n\n return watchQueryOptions;\n }\n\n // Defining these methods as no-ops on the prototype allows us to call\n // state.onCompleted and/or state.onError without worrying about whether a\n // callback was provided.\n private onCompleted(data: TData) {}\n private onError(error: ApolloError) {}\n\n private observable: ObservableQuery<TData, TVariables>;\n private obsQueryFields: Omit<\n ObservableQueryFields<TData, TVariables>,\n \"variables\"\n >;\n\n private useObservableQuery() {\n // See if there is an existing observable that was used to fetch the same\n // data and if so, use it instead since it will contain the proper queryId\n // to fetch the result set. This is used during SSR.\n const obsQuery = this.observable =\n this.renderPromises\n && this.renderPromises.getSSRObservable(this.watchQueryOptions)\n || this.observable // Reuse this.observable if possible (and not SSR)\n || this.client.watchQuery(this.watchQueryOptions);\n\n this.obsQueryFields = useMemo(() => ({\n refetch: obsQuery.refetch.bind(obsQuery),\n fetchMore: obsQuery.fetchMore.bind(obsQuery),\n updateQuery: obsQuery.updateQuery.bind(obsQuery),\n startPolling: obsQuery.startPolling.bind(obsQuery),\n stopPolling: obsQuery.stopPolling.bind(obsQuery),\n subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),\n }), [obsQuery]);\n\n if (this.renderPromises) {\n this.renderPromises.registerSSRObservable(obsQuery);\n\n if (!this.ssrDisabled && obsQuery.getCurrentResult().loading) {\n // TODO: This is a legacy API which could probably be cleaned up\n this.renderPromises.addQueryPromise({\n // The only options which seem to actually be used by the\n // RenderPromises class are query and variables.\n getOptions: () => obsQuery.options,\n fetchData: () => new Promise<void>((resolve) => {\n const sub = obsQuery.subscribe({\n next(result) {\n if (!result.loading) {\n resolve()\n sub.unsubscribe();\n }\n },\n error() {\n resolve();\n sub.unsubscribe();\n },\n complete() {\n resolve();\n },\n });\n }),\n },\n // This callback never seemed to do anything\n () => null);\n\n // TODO: This is a hack to make sure useLazyQuery executions update the\n // obsevable query options for ssr.\n obsQuery.setOptions(this.watchQueryOptions).catch(() => {});\n }\n }\n\n const prevOptionsRef = useRef({\n watchQueryOptions: this.watchQueryOptions,\n });\n\n // An effect to keep obsQuery.options up to date in case\n // state.watchQueryOptions changes.\n useEffect(() => {\n if (this.renderPromises) {\n // Do nothing during server rendering.\n } else if (\n // The useOptions method only updates this.watchQueryOptions if new new\n // watchQueryOptions are not deep-equal to the previous options, so we\n // only need a reference check (!==) here.\n this.watchQueryOptions !== prevOptionsRef.current.watchQueryOptions\n ) {\n obsQuery.setOptions(this.watchQueryOptions).catch(() => {});\n prevOptionsRef.current.watchQueryOptions = this.watchQueryOptions;\n this.setResult(obsQuery.getCurrentResult());\n }\n }, [obsQuery, this.watchQueryOptions]);\n\n return obsQuery;\n }\n\n private useSubscriptionEffect(\n // We could use this.observable and not pass this obsQuery parameter, but I\n // like the guarantee that obsQuery won't change, whereas this.observable\n // could change without warning (in theory).\n obsQuery: ObservableQuery<TData, TVariables>,\n ) {\n // An effect to subscribe to the current observable query\n useEffect(() => {\n if (this.renderPromises) {\n return;\n }\n\n const onNext = () => {\n const previousResult = this.result;\n // We use `getCurrentResult()` instead of the onNext argument because\n // the values differ slightly. Specifically, loading results will have\n // an empty object for data instead of `undefined` for some reason.\n const result = obsQuery.getCurrentResult();\n // Make sure we're not attempting to re-render similar results\n if (\n previousResult &&\n previousResult.loading === result.loading &&\n previousResult.networkStatus === result.networkStatus &&\n equal(previousResult.data, result.data)\n ) {\n return;\n }\n\n this.setResult(result);\n };\n\n const onError = (error: Error) => {\n const last = obsQuery[\"last\"];\n subscription.unsubscribe();\n // Unfortunately, if `lastError` is set in the current\n // `observableQuery` when the subscription is re-created,\n // the subscription will immediately receive the error, which will\n // cause it to terminate again. To avoid this, we first clear\n // the last error/result from the `observableQuery` before re-starting\n // the subscription, and restore it afterwards (so the subscription\n // has a chance to stay open).\n try {\n obsQuery.resetLastResults();\n subscription = obsQuery.subscribe(onNext, onError);\n } finally {\n obsQuery[\"last\"] = last;\n }\n\n if (!hasOwnProperty.call(error, 'graphQLErrors')) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = this.result;\n if (\n !previousResult ||\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n this.setResult({\n data: (previousResult && previousResult.data) as TData,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n }\n };\n\n let subscription = obsQuery.subscribe(onNext, onError);\n\n return () => subscription.unsubscribe();\n }, [\n obsQuery,\n this.renderPromises,\n this.client.disableNetworkFetches,\n ]);\n }\n\n // These members are populated by getCurrentResult and setResult, and it's\n // okay/normal for them to be initially undefined.\n private result: undefined | ApolloQueryResult<TData>;\n private previousData: undefined | TData;\n\n private setResult(nextResult: ApolloQueryResult<TData>) {\n const previousResult = this.result;\n if (previousResult && previousResult.data) {\n this.previousData = previousResult.data;\n }\n this.result = nextResult;\n // Calling state.setResult always triggers an update, though some call sites\n // perform additional equality checks before committing to an update.\n this.forceUpdate();\n this.handleErrorOrCompleted(nextResult);\n }\n\n private handleErrorOrCompleted(result: ApolloQueryResult<TData>) {\n if (!result.loading) {\n if (result.error) {\n this.onError(result.error);\n } else if (result.data) {\n this.onCompleted(result.data);\n }\n }\n }\n\n private getCurrentResult(): ApolloQueryResult<TData> {\n let { result } = this;\n // Using this.result as a cache ensures getCurrentResult continues returning\n // the same (===) result object, unless state.setResult has been called, or\n // we're doing server rendering and therefore override the result below.\n if (!result) {\n result = this.result = this.observable.getCurrentResult();\n this.handleErrorOrCompleted(result);\n }\n\n if (\n (this.renderPromises || this.client.disableNetworkFetches) &&\n this.queryHookOptions.ssr === false\n ) {\n // If SSR has been explicitly disabled, and this function has been called\n // on the server side, return the default loading state.\n result = {\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n };\n } else if (\n this.queryHookOptions.skip ||\n this.queryHookOptions.fetchPolicy === 'standby'\n ) {\n // When skipping a query (ie. we're not querying for data but still want to\n // render children), make sure the `data` is cleared out and `loading` is\n // set to `false` (since we aren't loading anything).\n //\n // NOTE: We no longer think this is the correct behavior. Skipping should\n // not automatically set `data` to `undefined`, but instead leave the\n // previous data in place. In other words, skipping should not mandate that\n // previously received data is all of a sudden removed. Unfortunately,\n // changing this is breaking, so we'll have to wait until Apollo Client 4.0\n // to address this.\n result = {\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.ready,\n };\n }\n\n return result;\n }\n\n // This cache allows the referential stability of this.result (as returned by\n // getCurrentResult) to translate into referential stability of the resulting\n // QueryResult object returned by toQueryResult.\n private toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)<\n ApolloQueryResult<TData>,\n QueryResult<TData, TVariables>\n >();\n\n private toQueryResult(\n result: ApolloQueryResult<TData>,\n ): QueryResult<TData, TVariables> {\n let queryResult = this.toQueryResultCache.get(result);\n if (queryResult) return queryResult;\n\n const { data, partial, ...resultWithoutPartial } = result;\n this.toQueryResultCache.set(result, queryResult = {\n data, // Ensure always defined, even if result.data is missing.\n ...resultWithoutPartial,\n ...this.obsQueryFields,\n client: this.client,\n observable: this.observable,\n variables: this.observable.variables,\n called: true,\n previousData: this.previousData,\n });\n\n if (!queryResult.error && isNonEmptyArray(result.errors)) {\n // Until a set naming convention for networkError and graphQLErrors is\n // decided upon, we map errors (graphQLErrors) to the error options.\n // TODO: Is it possible for both result.error and result.errors to be\n // defined here?\n queryResult.error = new ApolloError({ graphQLErrors: result.errors });\n }\n\n return queryResult;\n }\n\n private unsafeHandlePartialRefetch(result: ApolloQueryResult<TData>) {\n // WARNING: SIDE-EFFECTS IN THE RENDER FUNCTION\n //\n // TODO: This code should be removed when the partialRefetch option is\n // removed. I was unable to get this hook to behave reasonably in certain\n // edge cases when this block was put in an effect.\n if (\n result.partial &&\n this.queryHookOptions.partialRefetch &&\n !result.loading &&\n (!result.data || Object.keys(result.data).length === 0) &&\n this.observable.options.fetchPolicy !== 'cache-only'\n ) {\n Object.assign(result, {\n loading: true,\n networkStatus: NetworkStatus.refetch,\n });\n this.observable.refetch();\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscription.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAItE,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGhD,wBAAgB,eAAe,CAAC,KAAK,GAAG,GAAG,EAAE,UAAU,GAAG,kBAAkB,EAC1E,YAAY,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EACjE,OAAO,CAAC,EAAE,uBAAuB,CAAC,KAAK,EAAE,UAAU,CAAC,
|
|
1
|
+
{"version":3,"file":"useSubscription.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAItE,OAAO,EACL,uBAAuB,EACvB,kBAAkB,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGhD,wBAAgB,eAAe,CAAC,KAAK,GAAG,GAAG,EAAE,UAAU,GAAG,kBAAkB,EAC1E,YAAY,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EACjE,OAAO,CAAC,EAAE,uBAAuB,CAAC,KAAK,EAAE,UAAU,CAAC,kCA4GrD"}
|
|
@@ -30,14 +30,16 @@ export function useSubscription(subscription, options) {
|
|
|
30
30
|
if (typeof shouldResubscribe === 'function') {
|
|
31
31
|
shouldResubscribe = !!shouldResubscribe(options);
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
if (options === null || options === void 0 ? void 0 : options.skip) {
|
|
34
|
+
if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip)) {
|
|
35
|
+
setResult({
|
|
36
|
+
loading: false,
|
|
37
|
+
data: void 0,
|
|
38
|
+
error: void 0,
|
|
39
|
+
variables: options === null || options === void 0 ? void 0 : options.variables,
|
|
40
|
+
});
|
|
41
|
+
setObservable(null);
|
|
42
|
+
}
|
|
41
43
|
}
|
|
42
44
|
else if (shouldResubscribe !== false && (client !== ref.current.client ||
|
|
43
45
|
subscription !== ref.current.subscription ||
|