@apollo/client 3.5.4 → 3.5.8
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 +175 -175
- package/apollo-client.cjs.map +1 -1
- package/apollo-client.min.cjs +1 -1
- package/cache/cache.cjs +15 -15
- package/cache/cache.cjs.map +1 -1
- package/cache/inmemory/helpers.js +1 -1
- package/cache/inmemory/helpers.js.map +1 -1
- package/cache/inmemory/key-extractor.js +2 -2
- package/cache/inmemory/key-extractor.js.map +1 -1
- package/cache/inmemory/policies.js +3 -3
- package/cache/inmemory/policies.js.map +1 -1
- package/cache/inmemory/readFromStore.js +3 -3
- package/cache/inmemory/readFromStore.js.map +1 -1
- package/cache/inmemory/writeToStore.js +6 -6
- package/cache/inmemory/writeToStore.js.map +1 -1
- package/core/ApolloClient.js +1 -1
- package/core/ApolloClient.js.map +1 -1
- package/core/LocalState.js +1 -1
- package/core/LocalState.js.map +1 -1
- package/core/ObservableQuery.js +3 -3
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryManager.js +1 -1
- package/core/QueryManager.js.map +1 -1
- package/core/core.cjs +11 -11
- package/core/core.cjs.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 +1 -1
- package/link/batch/batch.cjs.map +1 -1
- package/link/batch/batching.js +1 -1
- 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/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 +15 -15
- 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 +101 -112
- package/react/hooks/hooks.cjs.map +1 -1
- package/react/hooks/useApolloClient.js +2 -2
- package/react/hooks/useApolloClient.js.map +1 -1
- package/react/hooks/useLazyQuery.d.ts.map +1 -1
- package/react/hooks/useLazyQuery.js +18 -27
- package/react/hooks/useLazyQuery.js.map +1 -1
- package/react/hooks/useMutation.d.ts.map +1 -1
- package/react/hooks/useMutation.js +59 -68
- package/react/hooks/useMutation.js.map +1 -1
- package/react/hooks/useQuery.d.ts.map +1 -1
- package/react/hooks/useQuery.js +25 -17
- package/react/hooks/useQuery.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/ssr.cjs +3 -3
- package/react/types/types.d.ts +1 -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 +1 -1
- 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/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,5 +1,5 @@
|
|
|
1
|
-
import { __assign
|
|
2
|
-
import { useCallback,
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
3
3
|
import { useQuery } from "./useQuery.js";
|
|
4
4
|
var EAGER_METHODS = [
|
|
5
5
|
'refetch',
|
|
@@ -11,36 +11,16 @@ var EAGER_METHODS = [
|
|
|
11
11
|
export function useLazyQuery(query, options) {
|
|
12
12
|
var _a = useState({
|
|
13
13
|
called: false,
|
|
14
|
-
resolves: [],
|
|
15
14
|
}), execution = _a[0], setExecution = _a[1];
|
|
16
|
-
var execute = useCallback(function (executeOptions) {
|
|
17
|
-
var resolve;
|
|
18
|
-
var promise = new Promise(function (resolve1) { return (resolve = resolve1); });
|
|
19
|
-
setExecution(function (execution) {
|
|
20
|
-
if (execution.called) {
|
|
21
|
-
result && result.refetch(executeOptions === null || executeOptions === void 0 ? void 0 : executeOptions.variables);
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
called: true,
|
|
25
|
-
resolves: __spreadArray(__spreadArray([], execution.resolves, true), [resolve], false),
|
|
26
|
-
options: executeOptions,
|
|
27
|
-
};
|
|
28
|
-
});
|
|
29
|
-
return promise;
|
|
30
|
-
}, []);
|
|
31
15
|
var result = useQuery(query, __assign(__assign(__assign({}, options), execution.options), { fetchPolicy: execution.called ? options === null || options === void 0 ? void 0 : options.fetchPolicy : 'standby', skip: undefined }));
|
|
32
|
-
useEffect(function () {
|
|
33
|
-
var resolves = execution.resolves;
|
|
34
|
-
if (!result.loading && resolves.length) {
|
|
35
|
-
setExecution(function (execution) { return (__assign(__assign({}, execution), { resolves: [] })); });
|
|
36
|
-
resolves.forEach(function (resolve) { return resolve(result); });
|
|
37
|
-
}
|
|
38
|
-
}, [result, execution]);
|
|
39
16
|
if (!execution.called) {
|
|
40
17
|
result = __assign(__assign({}, result), { loading: false, data: void 0, error: void 0, called: false });
|
|
18
|
+
}
|
|
19
|
+
var eagerMethods = useMemo(function () {
|
|
20
|
+
var eagerMethods = {};
|
|
41
21
|
var _loop_1 = function (key) {
|
|
42
22
|
var method = result[key];
|
|
43
|
-
|
|
23
|
+
eagerMethods[key] = function () {
|
|
44
24
|
var args = [];
|
|
45
25
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
46
26
|
args[_i] = arguments[_i];
|
|
@@ -53,7 +33,18 @@ export function useLazyQuery(query, options) {
|
|
|
53
33
|
var key = EAGER_METHODS_1[_i];
|
|
54
34
|
_loop_1(key);
|
|
55
35
|
}
|
|
56
|
-
|
|
36
|
+
return eagerMethods;
|
|
37
|
+
}, []);
|
|
38
|
+
result.error = result.error || void 0;
|
|
39
|
+
Object.assign(result, eagerMethods);
|
|
40
|
+
var execute = useCallback(function (executeOptions) {
|
|
41
|
+
setExecution({ called: true, options: executeOptions });
|
|
42
|
+
return result.refetch(executeOptions === null || executeOptions === void 0 ? void 0 : executeOptions.variables).then(function (result1) {
|
|
43
|
+
var result2 = __assign(__assign({}, result), { data: result1.data, error: result1.error, called: true, loading: false });
|
|
44
|
+
Object.assign(result2, eagerMethods);
|
|
45
|
+
return result2;
|
|
46
|
+
});
|
|
47
|
+
}, []);
|
|
57
48
|
return [execute, result];
|
|
58
49
|
}
|
|
59
50
|
//# sourceMappingURL=useLazyQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLazyQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"useLazyQuery.js","sourceRoot":"","sources":["../../../src/react/hooks/useLazyQuery.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAOvD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAKtC,IAAM,aAAa,GAAG;IACpB,SAAS;IACT,WAAW;IACX,aAAa;IACb,cAAc;IACd,iBAAiB;CACT,CAAC;AAEX,MAAM,UAAU,YAAY,CAC1B,KAA0D,EAC1D,OAAiD;IAE3C,IAAA,KAA4B,QAAQ,CAGvC;QACD,MAAM,EAAE,KAAK;KACd,CAAC,EALK,SAAS,QAAA,EAAE,YAAY,QAK5B,CAAC;IAEH,IAAI,MAAM,GAAG,QAAQ,CAAoB,KAAK,iCACzC,OAAO,GACP,SAAS,CAAC,OAAO,KAGpB,WAAW,EAAE,SAAS,CAAC,MAAM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,CAAE,SAAS,CAAC,CAAC,IAAI,CAAC,UAAC,OAAO;YAC5D,IAAM,OAAO,yBACR,MAAM,KACT,IAAI,EAAE,OAAO,CAAC,IAAI,EAClB,KAAK,EAAE,OAAO,CAAC,KAAK,EACpB,MAAM,EAAE,IAAI,EACZ,OAAO,EAAE,KAAK,GACf,CAAC;YAEF,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;YACrC,OAAO,OAAO,CAAC;QACjB,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { DocumentNode } from 'graphql';\nimport { TypedDocumentNode } from '@graphql-typed-document-node/core';\nimport { useCallback, useMemo, useState } from 'react';\n\nimport {\n LazyQueryHookOptions,\n QueryLazyOptions,\n QueryTuple,\n} from '../types/types';\nimport { useQuery } from './useQuery';\nimport { OperationVariables } from '../../core';\n\n// The following methods, when called will execute the query, regardless of\n// whether the useLazyQuery execute function was called before.\nconst EAGER_METHODS = [\n 'refetch',\n 'fetchMore',\n 'updateQuery',\n 'startPolling',\n 'subscribeToMore',\n] as const;\n\nexport function useLazyQuery<TData = any, TVariables = OperationVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options?: LazyQueryHookOptions<TData, TVariables>\n): QueryTuple<TData, TVariables> {\n const [execution, setExecution] = useState<{\n called: boolean,\n options?: QueryLazyOptions<TVariables>,\n }>({\n called: false,\n });\n\n let result = useQuery<TData, TVariables>(query, {\n ...options,\n ...execution.options,\n // We don’t set skip to execution.called, because some useQuery SSR code\n // checks skip for some reason.\n fetchPolicy: execution.called ? options?.fetchPolicy : 'standby',\n skip: undefined,\n });\n\n if (!execution.called) {\n result = {\n ...result,\n loading: false,\n data: void 0 as unknown as TData,\n error: void 0,\n called: false,\n };\n }\n\n // We use useMemo here to make sure the eager methods have a stable identity.\n const eagerMethods = useMemo(() => {\n const eagerMethods: Record<string, any> = {};\n for (const key of EAGER_METHODS) {\n const method = result[key];\n eagerMethods[key] = (...args: any) => {\n setExecution((execution) => ({ ...execution, called: true }));\n return (method as any)(...args);\n };\n }\n\n return eagerMethods;\n }, []);\n\n result.error = result.error || void 0;\n Object.assign(result, eagerMethods);\n\n const execute = useCallback<\n QueryTuple<TData, TVariables>[0]\n >((executeOptions?: QueryLazyOptions<TVariables>) => {\n setExecution({ called: true, options: executeOptions });\n return result.refetch(executeOptions?.variables).then((result1) => {\n const result2 = {\n ...result,\n data: result1.data,\n error: result1.error,\n called: true,\n loading: false,\n };\n\n Object.assign(result2, eagerMethods);\n return result2;\n });\n }, []);\n\n return [execute, result];\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAEL,mBAAmB,EAEnB,aAAa,EACd,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,WAAW,EACX,cAAc,EAEd,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAMpB,wBAAgB,WAAW,CACzB,KAAK,GAAG,GAAG,EACX,UAAU,GAAG,kBAAkB,EAC/B,QAAQ,GAAG,cAAc,EACzB,MAAM,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,EAElD,QAAQ,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC7D,OAAO,CAAC,EAAE,mBAAmB,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,GACzD,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"useMutation.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useMutation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAEL,mBAAmB,EAEnB,aAAa,EACd,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,WAAW,EACX,cAAc,EAEd,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAMpB,wBAAgB,WAAW,CACzB,KAAK,GAAG,GAAG,EACX,UAAU,GAAG,kBAAkB,EAC/B,QAAQ,GAAG,cAAc,EACzB,MAAM,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,EAElD,QAAQ,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC7D,OAAO,CAAC,EAAE,mBAAmB,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,GACzD,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,CAAC,CAmHpD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
|
-
import { useCallback,
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
3
|
import { mergeOptions, } from "../../core/index.js";
|
|
4
4
|
import { equal } from '@wry/equality';
|
|
5
5
|
import { DocumentType, verifyDocumentType } from "../parser/index.js";
|
|
@@ -17,82 +17,73 @@ export function useMutation(mutation, options) {
|
|
|
17
17
|
result: result,
|
|
18
18
|
mutationId: 0,
|
|
19
19
|
isMounted: true,
|
|
20
|
-
execute: null,
|
|
21
20
|
client: client,
|
|
22
21
|
mutation: mutation,
|
|
23
22
|
options: options,
|
|
24
23
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
{
|
|
25
|
+
Object.assign(ref.current, { client: client, options: options, mutation: mutation });
|
|
26
|
+
}
|
|
27
|
+
var execute = useCallback(function (executeOptions) {
|
|
28
|
+
if (executeOptions === void 0) { executeOptions = {}; }
|
|
29
|
+
var _a = ref.current, client = _a.client, options = _a.options, mutation = _a.mutation;
|
|
30
|
+
var baseOptions = __assign(__assign({}, options), { mutation: mutation });
|
|
31
|
+
if (!ref.current.result.loading && !baseOptions.ignoreResults) {
|
|
32
|
+
setResult(ref.current.result = {
|
|
33
|
+
loading: true,
|
|
34
|
+
error: void 0,
|
|
35
|
+
data: void 0,
|
|
36
|
+
called: true,
|
|
37
|
+
client: client,
|
|
38
|
+
});
|
|
31
39
|
}
|
|
32
|
-
ref.current.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
var mutationId = ++ref.current.mutationId;
|
|
41
|
+
var clientOptions = mergeOptions(baseOptions, executeOptions);
|
|
42
|
+
return client.mutate(clientOptions).then(function (response) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
var data = response.data, errors = response.errors;
|
|
45
|
+
var error = errors && errors.length > 0
|
|
46
|
+
? new ApolloError({ graphQLErrors: errors })
|
|
47
|
+
: void 0;
|
|
48
|
+
if (mutationId === ref.current.mutationId &&
|
|
49
|
+
!clientOptions.ignoreResults) {
|
|
50
|
+
var result_1 = {
|
|
43
51
|
called: true,
|
|
52
|
+
loading: false,
|
|
53
|
+
data: data,
|
|
54
|
+
error: error,
|
|
44
55
|
client: client,
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
var clientOptions = mergeOptions(baseOptions, executeOptions);
|
|
49
|
-
return client.mutate(clientOptions).then(function (response) {
|
|
50
|
-
var _a, _b;
|
|
51
|
-
var data = response.data, errors = response.errors;
|
|
52
|
-
var error = errors && errors.length > 0
|
|
53
|
-
? new ApolloError({ graphQLErrors: errors })
|
|
54
|
-
: void 0;
|
|
55
|
-
if (mutationId === ref.current.mutationId &&
|
|
56
|
-
!clientOptions.ignoreResults) {
|
|
57
|
-
var result_1 = {
|
|
58
|
-
called: true,
|
|
59
|
-
loading: false,
|
|
60
|
-
data: data,
|
|
61
|
-
error: error,
|
|
62
|
-
client: client,
|
|
63
|
-
};
|
|
64
|
-
if (ref.current.isMounted && !equal(ref.current.result, result_1)) {
|
|
65
|
-
setResult(ref.current.result = result_1);
|
|
66
|
-
}
|
|
56
|
+
};
|
|
57
|
+
if (ref.current.isMounted && !equal(ref.current.result, result_1)) {
|
|
58
|
+
setResult(ref.current.result = result_1);
|
|
67
59
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
if (baseOptions.onError || clientOptions.onError) {
|
|
87
|
-
(_a = baseOptions.onError) === null || _a === void 0 ? void 0 : _a.call(baseOptions, error);
|
|
88
|
-
(_b = executeOptions.onError) === null || _b === void 0 ? void 0 : _b.call(executeOptions, error);
|
|
89
|
-
return { data: void 0, errors: error };
|
|
60
|
+
}
|
|
61
|
+
(_a = baseOptions.onCompleted) === null || _a === void 0 ? void 0 : _a.call(baseOptions, response.data);
|
|
62
|
+
(_b = executeOptions.onCompleted) === null || _b === void 0 ? void 0 : _b.call(executeOptions, response.data);
|
|
63
|
+
return response;
|
|
64
|
+
}).catch(function (error) {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
if (mutationId === ref.current.mutationId &&
|
|
67
|
+
ref.current.isMounted) {
|
|
68
|
+
var result_2 = {
|
|
69
|
+
loading: false,
|
|
70
|
+
error: error,
|
|
71
|
+
data: void 0,
|
|
72
|
+
called: true,
|
|
73
|
+
client: client,
|
|
74
|
+
};
|
|
75
|
+
if (!equal(ref.current.result, result_2)) {
|
|
76
|
+
setResult(ref.current.result = result_2);
|
|
90
77
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
78
|
+
}
|
|
79
|
+
if (baseOptions.onError || clientOptions.onError) {
|
|
80
|
+
(_a = baseOptions.onError) === null || _a === void 0 ? void 0 : _a.call(baseOptions, error);
|
|
81
|
+
(_b = executeOptions.onError) === null || _b === void 0 ? void 0 : _b.call(executeOptions, error);
|
|
82
|
+
return { data: void 0, errors: error };
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
});
|
|
86
|
+
}, []);
|
|
96
87
|
var reset = useCallback(function () {
|
|
97
88
|
setResult({ called: false, loading: false, client: client });
|
|
98
89
|
}, []);
|
|
@@ -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,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAUjE,OAAO,EAGL,YAAY,GAEb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,MAAM,UAAU,WAAW,CAMzB,QAA6D,EAC7D,OAA0D;IAE1D,IAAM,MAAM,GAAG,eAAe,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC,CAAC;IAChD,kBAAkB,CAAC,QAAQ,EAA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G,CAAC,OAAO,CAAC,MAAM,EAAE,QAAM,CAAC,EAAE;oBACtC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,GAAG,QAAM,CAAC,CAAC;iBACxC;aACF;YAED,IAAI,WAAW,CAAC,OAAO,IAAI,aAAa,CAAC,OAAO,EAAE;gBAChD,MAAA,WAAW,CAAC,OAAO,+CAAnB,WAAW,EAAW,KAAK,CAAC,CAAC;gBAC7B,MAAA,cAAc,CAAC,OAAO,+CAAtB,cAAc,EAAW,KAAK,CAAC,CAAC;gBAEhC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;aACxC;YAED,MAAM,KAAK,CAAC;QACd,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,KAAK,GAAG,WAAW,CAAC;QACxB,SAAS,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,QAAA,EAAE,CAAC,CAAC;IACvD,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,CAAC,cAAM,OAAA;QACd,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC;IAChC,CAAC,EAFe,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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAgB,MAAM,YAAY,CAAC;AAG9D,OAAO,EAIL,YAAY,EACZ,iBAAiB,EAElB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gBAAgB,EAChB,WAAW,EACZ,MAAM,gBAAgB,CAAC;AAKxB,wBAAgB,QAAQ,CACtB,KAAK,GAAG,GAAG,EACX,UAAU,GAAG,kBAAkB,EAE/B,KAAK,EAAE,YAAY,GAAG,iBAAiB,CAAC,KAAK,EAAE,UAAU,CAAC,EAC1D,OAAO,CAAC,EAAE,gBAAgB,CAAC,KAAK,EAAE,UAAU,CAAC,GAC5C,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,CAsShC"}
|
package/react/hooks/useQuery.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { __assign, __rest } from "tslib";
|
|
2
2
|
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
3
|
import { equal } from '@wry/equality';
|
|
4
|
+
import { mergeOptions } from "../../core/index.js";
|
|
4
5
|
import { getApolloContext } from "../context/index.js";
|
|
5
6
|
import { ApolloError } from "../../errors/index.js";
|
|
6
7
|
import { NetworkStatus, } from "../../core/index.js";
|
|
@@ -10,9 +11,10 @@ export function useQuery(query, options) {
|
|
|
10
11
|
var _a;
|
|
11
12
|
var context = useContext(getApolloContext());
|
|
12
13
|
var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
|
|
14
|
+
var defaultWatchQueryOptions = client.defaultOptions.watchQuery;
|
|
13
15
|
verifyDocumentType(query, DocumentType.Query);
|
|
14
16
|
var _b = useState(function () {
|
|
15
|
-
var watchQueryOptions = createWatchQueryOptions(query, options);
|
|
17
|
+
var watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);
|
|
16
18
|
var obsQuery = null;
|
|
17
19
|
if (context.renderPromises) {
|
|
18
20
|
obsQuery = context.renderPromises.getSSRObservable(watchQueryOptions);
|
|
@@ -28,7 +30,7 @@ export function useQuery(query, options) {
|
|
|
28
30
|
!(options === null || options === void 0 ? void 0 : options.skip) &&
|
|
29
31
|
obsQuery.getCurrentResult().loading) {
|
|
30
32
|
context.renderPromises.addQueryPromise({
|
|
31
|
-
getOptions: function () { return createWatchQueryOptions(query, options); },
|
|
33
|
+
getOptions: function () { return createWatchQueryOptions(query, options, defaultWatchQueryOptions); },
|
|
32
34
|
fetchData: function () { return new Promise(function (resolve) {
|
|
33
35
|
var sub = obsQuery.subscribe({
|
|
34
36
|
next: function (result) {
|
|
@@ -69,11 +71,11 @@ export function useQuery(query, options) {
|
|
|
69
71
|
options: options,
|
|
70
72
|
result: result,
|
|
71
73
|
previousData: void 0,
|
|
72
|
-
watchQueryOptions: createWatchQueryOptions(query, options),
|
|
74
|
+
watchQueryOptions: createWatchQueryOptions(query, options, defaultWatchQueryOptions),
|
|
73
75
|
});
|
|
74
76
|
useEffect(function () {
|
|
75
77
|
var _a, _b;
|
|
76
|
-
var watchQueryOptions = createWatchQueryOptions(query, options);
|
|
78
|
+
var watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);
|
|
77
79
|
var nextResult;
|
|
78
80
|
if (ref.current.client !== client || !equal(ref.current.query, query)) {
|
|
79
81
|
var obsQuery_1 = client.watchQuery(watchQueryOptions);
|
|
@@ -92,17 +94,15 @@ export function useQuery(query, options) {
|
|
|
92
94
|
}
|
|
93
95
|
setResult(ref.current.result = nextResult);
|
|
94
96
|
if (!nextResult.loading && options) {
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
(_b = options.onCompleted) === null || _b === void 0 ? void 0 : _b.call(options, result.data);
|
|
101
|
-
}
|
|
97
|
+
if (nextResult.error) {
|
|
98
|
+
(_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, nextResult.error);
|
|
99
|
+
}
|
|
100
|
+
else if (nextResult.data) {
|
|
101
|
+
(_b = options.onCompleted) === null || _b === void 0 ? void 0 : _b.call(options, nextResult.data);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
Object.assign(ref.current, { client: client, query: query
|
|
105
|
+
Object.assign(ref.current, { client: client, query: query });
|
|
106
106
|
}, [obsQuery, client, query, options]);
|
|
107
107
|
useEffect(function () {
|
|
108
108
|
if (context.renderPromises) {
|
|
@@ -170,8 +170,9 @@ export function useQuery(query, options) {
|
|
|
170
170
|
(options === null || options === void 0 ? void 0 : options.ssr) !== false &&
|
|
171
171
|
!(options === null || options === void 0 ? void 0 : options.skip) &&
|
|
172
172
|
result.loading) {
|
|
173
|
-
obsQuery.setOptions(createWatchQueryOptions(query, options)).catch(function () { });
|
|
173
|
+
obsQuery.setOptions(createWatchQueryOptions(query, options, defaultWatchQueryOptions)).catch(function () { });
|
|
174
174
|
}
|
|
175
|
+
Object.assign(ref.current, { options: options });
|
|
175
176
|
}
|
|
176
177
|
if ((context.renderPromises || client.disableNetworkFetches) &&
|
|
177
178
|
(options === null || options === void 0 ? void 0 : options.ssr) === false) {
|
|
@@ -201,12 +202,16 @@ export function useQuery(query, options) {
|
|
|
201
202
|
stopPolling: obsQuery.stopPolling.bind(obsQuery),
|
|
202
203
|
subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
|
|
203
204
|
}); }, [obsQuery]);
|
|
204
|
-
return __assign(__assign(__assign({}, obsQueryFields), { variables:
|
|
205
|
+
return __assign(__assign(__assign({}, obsQueryFields), { variables: createWatchQueryOptions(query, options, defaultWatchQueryOptions).variables, client: client, called: true, previousData: ref.current.previousData }), result);
|
|
205
206
|
}
|
|
206
|
-
function createWatchQueryOptions(query, options) {
|
|
207
|
+
function createWatchQueryOptions(query, options, defaultOptions) {
|
|
207
208
|
var _a;
|
|
208
209
|
if (options === void 0) { options = {}; }
|
|
209
|
-
var skip = options.skip, ssr = options.ssr, onCompleted = options.onCompleted, onError = options.onError, displayName = options.displayName,
|
|
210
|
+
var skip = options.skip, ssr = options.ssr, onCompleted = options.onCompleted, onError = options.onError, displayName = options.displayName, otherOptions = __rest(options, ["skip", "ssr", "onCompleted", "onError", "displayName"]);
|
|
211
|
+
var watchQueryOptions = __assign({ query: query }, otherOptions);
|
|
212
|
+
if (defaultOptions) {
|
|
213
|
+
watchQueryOptions = mergeOptions(defaultOptions, watchQueryOptions);
|
|
214
|
+
}
|
|
210
215
|
if (skip) {
|
|
211
216
|
watchQueryOptions.fetchPolicy = 'standby';
|
|
212
217
|
}
|
|
@@ -218,6 +223,9 @@ function createWatchQueryOptions(query, options) {
|
|
|
218
223
|
else if (!watchQueryOptions.fetchPolicy) {
|
|
219
224
|
watchQueryOptions.fetchPolicy = 'cache-first';
|
|
220
225
|
}
|
|
221
|
-
|
|
226
|
+
if (!watchQueryOptions.variables) {
|
|
227
|
+
watchQueryOptions.variables = {};
|
|
228
|
+
}
|
|
229
|
+
return watchQueryOptions;
|
|
222
230
|
}
|
|
223
231
|
//# 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,OA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YAAiD;IAM/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;IAEZ,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 { 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 [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 resolve();\n },\n });\n }),\n },\n // This callback never seemed to do anything\n () => null,\n );\n }\n\n return obsQuery;\n });\n\n let [result, setResult] = useState(() => {\n const result = obsQuery.getCurrentResult();\n if (!result.loading && options) {\n if (result.error) {\n options.onError?.(result.error);\n } else if (result.data) {\n options.onCompleted?.(result.data);\n }\n }\n\n return result;\n });\n\n const ref = useRef({\n client,\n query,\n options,\n result,\n previousData: void 0 as TData | undefined,\n watchQueryOptions: createWatchQueryOptions(query, options),\n });\n\n // An effect to recreate the obsQuery whenever the client or query changes.\n // This effect is also responsible for checking and updating the obsQuery\n // options whenever they change.\n useEffect(() => {\n const watchQueryOptions = createWatchQueryOptions(query, options);\n let nextResult: ApolloQueryResult<TData> | undefined;\n if (ref.current.client !== client || !equal(ref.current.query, query)) {\n const obsQuery = client.watchQuery(watchQueryOptions);\n setObsQuery(obsQuery);\n nextResult = obsQuery.getCurrentResult();\n } else if (!equal(ref.current.watchQueryOptions, watchQueryOptions)) {\n obsQuery.setOptions(watchQueryOptions).catch(() => {});\n nextResult = obsQuery.getCurrentResult();\n ref.current.watchQueryOptions = watchQueryOptions;\n }\n\n if (nextResult) {\n const previousResult = ref.current.result;\n if (previousResult.data) {\n ref.current.previousData = previousResult.data;\n }\n\n setResult(ref.current.result = nextResult);\n if (!nextResult.loading && options) {\n if (!result.loading) {\n if (result.error) {\n options.onError?.(result.error);\n } else if (result.data) {\n options.onCompleted?.(result.data);\n }\n }\n }\n }\n\n Object.assign(ref.current, { client, query, options });\n }, [obsQuery, client, query, options]);\n\n // An effect to subscribe to the current observable query\n useEffect(() => {\n if (context.renderPromises) {\n return;\n }\n\n let subscription = obsQuery.subscribe(onNext, onError);\n // We use `getCurrentResult()` instead of the callback 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 function onNext() {\n const previousResult = ref.current.result;\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 if (previousResult.data) {\n ref.current.previousData = previousResult.data;\n }\n\n setResult(ref.current.result = result);\n if (!result.loading) {\n ref.current.options?.onCompleted?.(result.data);\n }\n }\n\n function 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 (!error.hasOwnProperty('graphQLErrors')) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = ref.current.result;\n if (\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n setResult(ref.current.result = {\n data: previousResult.data,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n ref.current.options?.onError?.(error as ApolloError);\n }\n }\n\n return () => subscription.unsubscribe();\n }, [obsQuery, context.renderPromises, client.disableNetworkFetches]);\n\n let partial: boolean | undefined;\n ({ partial, ...result } = result);\n\n {\n // BAD BOY CODE BLOCK WHERE WE PUT 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 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 (\n context.renderPromises &&\n options?.ssr !== false &&\n !options?.skip &&\n result.loading\n ) {\n obsQuery.setOptions(createWatchQueryOptions(query, options)).catch(() => {});\n }\n }\n\n if (\n (context.renderPromises || client.disableNetworkFetches) &&\n options?.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 = ref.current.result = {\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n };\n } else 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 }\n\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: result.error || new ApolloError({ graphQLErrors: result.errors }),\n };\n }\n\n const 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 return {\n ...obsQueryFields,\n variables: obsQuery.variables,\n client,\n called: true,\n previousData: ref.current.previousData,\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 // TODO: For some reason, we pass context, which is the React Apollo Context,\n // into observable queries, and test for that.\n // removing hook specific options\n const {\n skip,\n ssr,\n onCompleted,\n onError,\n displayName,\n ...watchQueryOptions\n } = options;\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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CAAC","sourcesContent":["import { useContext, useEffect, useMemo, useRef, useState } from 'react';\nimport { equal } from '@wry/equality';\nimport { OperationVariables, mergeOptions } 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 const defaultWatchQueryOptions = client.defaultOptions.watchQuery;\n verifyDocumentType(query, DocumentType.Query);\n const [obsQuery, setObsQuery] = useState(() => {\n const watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);\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, defaultWatchQueryOptions),\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 }\n\n return obsQuery;\n });\n\n let [result, setResult] = useState(() => {\n const result = obsQuery.getCurrentResult();\n if (!result.loading && options) {\n if (result.error) {\n options.onError?.(result.error);\n } else if (result.data) {\n options.onCompleted?.(result.data);\n }\n }\n\n return result;\n });\n\n const ref = useRef({\n client,\n query,\n options,\n result,\n previousData: void 0 as TData | undefined,\n watchQueryOptions: createWatchQueryOptions(query, options, defaultWatchQueryOptions),\n });\n\n // An effect to recreate the obsQuery whenever the client or query changes.\n // This effect is also responsible for checking and updating the obsQuery\n // options whenever they change.\n useEffect(() => {\n const watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);\n let nextResult: ApolloQueryResult<TData> | undefined;\n if (ref.current.client !== client || !equal(ref.current.query, query)) {\n const obsQuery = client.watchQuery(watchQueryOptions);\n setObsQuery(obsQuery);\n nextResult = obsQuery.getCurrentResult();\n } else if (!equal(ref.current.watchQueryOptions, watchQueryOptions)) {\n obsQuery.setOptions(watchQueryOptions).catch(() => {});\n nextResult = obsQuery.getCurrentResult();\n ref.current.watchQueryOptions = watchQueryOptions;\n }\n\n if (nextResult) {\n const previousResult = ref.current.result;\n if (previousResult.data) {\n ref.current.previousData = previousResult.data;\n }\n\n setResult(ref.current.result = nextResult);\n if (!nextResult.loading && options) {\n if (nextResult.error) {\n options.onError?.(nextResult.error);\n } else if (nextResult.data) {\n options.onCompleted?.(nextResult.data);\n }\n }\n }\n\n Object.assign(ref.current, { client, query });\n }, [obsQuery, client, query, options]);\n\n // An effect to subscribe to the current observable query\n useEffect(() => {\n if (context.renderPromises) {\n return;\n }\n\n let subscription = obsQuery.subscribe(onNext, onError);\n // We use `getCurrentResult()` instead of the callback 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 function onNext() {\n const previousResult = ref.current.result;\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 if (previousResult.data) {\n ref.current.previousData = previousResult.data;\n }\n\n setResult(ref.current.result = result);\n if (!result.loading) {\n ref.current.options?.onCompleted?.(result.data);\n }\n }\n\n function 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 (!error.hasOwnProperty('graphQLErrors')) {\n // The error is not a GraphQL error\n throw error;\n }\n\n const previousResult = ref.current.result;\n if (\n (previousResult && previousResult.loading) ||\n !equal(error, previousResult.error)\n ) {\n setResult(ref.current.result = {\n data: previousResult.data,\n error: error as ApolloError,\n loading: false,\n networkStatus: NetworkStatus.error,\n });\n ref.current.options?.onError?.(error as ApolloError);\n }\n }\n\n return () => subscription.unsubscribe();\n }, [obsQuery, context.renderPromises, client.disableNetworkFetches]);\n\n let partial: boolean | undefined;\n ({ partial, ...result } = result);\n\n {\n // BAD BOY CODE BLOCK WHERE WE PUT 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 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 (\n context.renderPromises &&\n options?.ssr !== false &&\n !options?.skip &&\n result.loading\n ) {\n obsQuery.setOptions(createWatchQueryOptions(query, options, defaultWatchQueryOptions)).catch(() => {});\n }\n\n // We assign options during rendering as a guard to make sure that\n // callbacks like onCompleted and onError are not stale.\n Object.assign(ref.current, { options });\n }\n\n if (\n (context.renderPromises || client.disableNetworkFetches) &&\n options?.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 = ref.current.result = {\n loading: true,\n data: void 0 as unknown as TData,\n error: void 0,\n networkStatus: NetworkStatus.loading,\n };\n } else 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 }\n\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: result.error || new ApolloError({ graphQLErrors: result.errors }),\n };\n }\n\n const 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 return {\n ...obsQueryFields,\n variables: createWatchQueryOptions(query, options, defaultWatchQueryOptions).variables,\n client,\n called: true,\n previousData: ref.current.previousData,\n ...result,\n };\n}\n\n/**\n * A function to massage options before passing them the ObservableQuery.\n */\nfunction createWatchQueryOptions<TData, TVariables>(\n query: DocumentNode | TypedDocumentNode<TData, TVariables>,\n options: QueryHookOptions<TData, TVariables> = {},\n defaultOptions?: Partial<WatchQueryOptions<any, any>>\n): 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 // removing hook specific options\n const {\n skip,\n ssr,\n onCompleted,\n onError,\n displayName,\n ...otherOptions\n } = options;\n\n let watchQueryOptions = { query, ...otherOptions };\n if (defaultOptions) {\n watchQueryOptions = mergeOptions(defaultOptions, watchQueryOptions);\n }\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 if (!watchQueryOptions.variables) {\n watchQueryOptions.variables = {} as TVariables;\n }\n\n return watchQueryOptions;\n}\n"]}
|
package/react/parser/index.js
CHANGED
|
@@ -26,7 +26,7 @@ export function parser(document) {
|
|
|
26
26
|
if (cached)
|
|
27
27
|
return cached;
|
|
28
28
|
var variables, type, name;
|
|
29
|
-
__DEV__ ? invariant(!!document && !!document.kind, "Argument of "
|
|
29
|
+
__DEV__ ? invariant(!!document && !!document.kind, "Argument of ".concat(document, " passed to parser was not a valid GraphQL ") +
|
|
30
30
|
"DocumentNode. You may need to use 'graphql-tag' or another method " +
|
|
31
31
|
"to convert your operation into a document") : invariant(!!document && !!document.kind, 30);
|
|
32
32
|
var fragments = document.definitions.filter(function (x) { return x.kind === 'FragmentDefinition'; });
|
|
@@ -44,8 +44,8 @@ export function parser(document) {
|
|
|
44
44
|
"You must include a query, subscription or mutation as well") : invariant(!fragments.length ||
|
|
45
45
|
(queries.length || mutations.length || subscriptions.length), 31);
|
|
46
46
|
__DEV__ ? invariant(queries.length + mutations.length + subscriptions.length <= 1, "react-apollo only supports a query, subscription, or a mutation per HOC. " +
|
|
47
|
-
(document
|
|
48
|
-
|
|
47
|
+
"".concat(document, " had ").concat(queries.length, " queries, ").concat(subscriptions.length, " ") +
|
|
48
|
+
"subscriptions and ".concat(mutations.length, " mutations. ") +
|
|
49
49
|
"You can use 'compose' to join multiple operation types to a component") : invariant(queries.length + mutations.length + subscriptions.length <= 1, 32);
|
|
50
50
|
type = queries.length ? DocumentType.Query : DocumentType.Mutation;
|
|
51
51
|
if (!queries.length && !mutations.length)
|
|
@@ -55,8 +55,8 @@ export function parser(document) {
|
|
|
55
55
|
: mutations.length
|
|
56
56
|
? mutations
|
|
57
57
|
: subscriptions;
|
|
58
|
-
__DEV__ ? invariant(definitions.length === 1, "react-apollo only supports one definition per HOC. "
|
|
59
|
-
(definitions.length
|
|
58
|
+
__DEV__ ? invariant(definitions.length === 1, "react-apollo only supports one definition per HOC. ".concat(document, " had ") +
|
|
59
|
+
"".concat(definitions.length, " definitions. ") +
|
|
60
60
|
"You can use 'compose' to join multiple operation types to a component") : invariant(definitions.length === 1, 33);
|
|
61
61
|
var definition = definitions[0];
|
|
62
62
|
variables = definition.variableDefinitions || [];
|
|
@@ -74,7 +74,7 @@ export function verifyDocumentType(document, type) {
|
|
|
74
74
|
var operation = parser(document);
|
|
75
75
|
var requiredOperationName = operationName(type);
|
|
76
76
|
var usedOperationName = operationName(operation.type);
|
|
77
|
-
__DEV__ ? invariant(operation.type === type, "Running a "
|
|
78
|
-
(requiredOperationName
|
|
77
|
+
__DEV__ ? invariant(operation.type === type, "Running a ".concat(requiredOperationName, " requires a graphql ") +
|
|
78
|
+
"".concat(requiredOperationName, ", but a ").concat(usedOperationName, " was used instead.")) : invariant(operation.type === type, 34);
|
|
79
79
|
}
|
|
80
80
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/parser/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AASpD,MAAM,CAAN,IAAY,YAIX;AAJD,WAAY,YAAY;IACtB,iDAAK,CAAA;IACL,uDAAQ,CAAA;IACR,+DAAY,CAAA;AACd,CAAC,EAJW,YAAY,KAAZ,YAAY,QAIvB;AAQD,IAAM,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;AAExB,MAAM,UAAU,aAAa,CAAC,IAAkB;IAC9C,IAAI,IAAI,CAAC;IACT,QAAQ,IAAI,EAAE;QACZ,KAAK,YAAY,CAAC,KAAK;YACrB,IAAI,GAAG,OAAO,CAAC;YACf,MAAM;QACR,KAAK,YAAY,CAAC,QAAQ;YACxB,IAAI,GAAG,UAAU,CAAC;YAClB,MAAM;QACR,KAAK,YAAY,CAAC,YAAY;YAC5B,IAAI,GAAG,cAAc,CAAC;YACtB,MAAM;KACT;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAGD,MAAM,UAAU,MAAM,CAAC,QAAsB;IAC3C,IAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAE1B,IAAI,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC;IAE1B,SAAS,CACP,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,EAC7B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/parser/index.ts"],"names":[],"mappings":"AAAA,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CAAC,EACxB,6DAAsD,QAAQ,UAAO;QACnE,UAAG,WAAW,CAAC,MAAM,mBAAgB;QACrC,uEAAuE,CAC1E,CAAC;IAEF,IAAM,UAAU,GAAG,WAAW,CAAC,CAAC,CAA4B,CAAC;IAC7D,SAAS,GAAG,UAAU,CAAC,mBAAmB,IAAI,EAAE,CAAC;IAEjD,IAAI,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE;QACtD,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;KAC9B;SAAM;QACL,IAAI,GAAG,MAAM,CAAC;KACf;IAED,IAAM,OAAO,GAAG,EAAE,IAAI,MAAA,EAAE,IAAI,MAAA,EAAE,SAAS,WAAA,EAAE,CAAC;IAC1C,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC7B,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,QAAsB,EAAE,IAAkB;IAC3E,IAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAM,qBAAqB,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC;IAClD,IAAM,iBAAiB,GAAG,aAAa,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACxD,SAAS,CACP,SAAS,CAAC,IAAI,KAAK,IAAI,EACvB,oBAAa,qBAAqB,yBAAsB;QACtD,UAAG,qBAAqB,qBAAW,iBAAiB,uBAAoB,CAC3E,CAAC;AACJ,CAAC","sourcesContent":["import { invariant } from '../../utilities/globals';\n\nimport {\n DocumentNode,\n DefinitionNode,\n VariableDefinitionNode,\n OperationDefinitionNode\n} from 'graphql';\n\nexport enum DocumentType {\n Query,\n Mutation,\n Subscription\n}\n\nexport interface IDocumentDefinition {\n type: DocumentType;\n name: string;\n variables: ReadonlyArray<VariableDefinitionNode>;\n}\n\nconst cache = new Map();\n\nexport function operationName(type: DocumentType) {\n let name;\n switch (type) {\n case DocumentType.Query:\n name = 'Query';\n break;\n case DocumentType.Mutation:\n name = 'Mutation';\n break;\n case DocumentType.Subscription:\n name = 'Subscription';\n break;\n }\n return name;\n}\n\n// This parser is mostly used to safety check incoming documents.\nexport function parser(document: DocumentNode): IDocumentDefinition {\n const cached = cache.get(document);\n if (cached) return cached;\n\n let variables, type, name;\n\n invariant(\n !!document && !!document.kind,\n `Argument of ${document} passed to parser was not a valid GraphQL ` +\n `DocumentNode. You may need to use 'graphql-tag' or another method ` +\n `to convert your operation into a document`\n );\n\n const fragments = document.definitions.filter(\n (x: DefinitionNode) => x.kind === 'FragmentDefinition'\n );\n\n const queries = document.definitions.filter(\n (x: DefinitionNode) =>\n x.kind === 'OperationDefinition' && x.operation === 'query'\n );\n\n const mutations = document.definitions.filter(\n (x: DefinitionNode) =>\n x.kind === 'OperationDefinition' && x.operation === 'mutation'\n );\n\n const subscriptions = document.definitions.filter(\n (x: DefinitionNode) =>\n x.kind === 'OperationDefinition' && x.operation === 'subscription'\n );\n\n invariant(\n !fragments.length ||\n (queries.length || mutations.length || subscriptions.length),\n `Passing only a fragment to 'graphql' is not yet supported. ` +\n `You must include a query, subscription or mutation as well`\n );\n\n invariant(\n queries.length + mutations.length + subscriptions.length <= 1,\n `react-apollo only supports a query, subscription, or a mutation per HOC. ` +\n `${document} had ${queries.length} queries, ${subscriptions.length} ` +\n `subscriptions and ${mutations.length} mutations. ` +\n `You can use 'compose' to join multiple operation types to a component`\n );\n\n type = queries.length ? DocumentType.Query : DocumentType.Mutation;\n if (!queries.length && !mutations.length) type = DocumentType.Subscription;\n\n const definitions = queries.length\n ? queries\n : mutations.length\n ? mutations\n : subscriptions;\n\n invariant(\n definitions.length === 1,\n `react-apollo only supports one definition per HOC. ${document} had ` +\n `${definitions.length} definitions. ` +\n `You can use 'compose' to join multiple operation types to a component`\n );\n\n const definition = definitions[0] as OperationDefinitionNode;\n variables = definition.variableDefinitions || [];\n\n if (definition.name && definition.name.kind === 'Name') {\n name = definition.name.value;\n } else {\n name = 'data'; // fallback to using data if no name\n }\n\n const payload = { name, type, variables };\n cache.set(document, payload);\n return payload;\n}\n\nexport function verifyDocumentType(document: DocumentNode, type: DocumentType) {\n const operation = parser(document);\n const requiredOperationName = operationName(type);\n const usedOperationName = operationName(operation.type);\n invariant(\n operation.type === type,\n `Running a ${requiredOperationName} requires a graphql ` +\n `${requiredOperationName}, but a ${usedOperationName} was used instead.`\n );\n}\n\n"]}
|