1 | import { invariant } from "../../utilities/globals/index.js";
|
2 | export var DocumentType;
|
3 | (function (DocumentType) {
|
4 | DocumentType[DocumentType["Query"] = 0] = "Query";
|
5 | DocumentType[DocumentType["Mutation"] = 1] = "Mutation";
|
6 | DocumentType[DocumentType["Subscription"] = 2] = "Subscription";
|
7 | })(DocumentType || (DocumentType = {}));
|
8 | var cache = new Map();
|
9 | export function operationName(type) {
|
10 | var name;
|
11 | switch (type) {
|
12 | case DocumentType.Query:
|
13 | name = 'Query';
|
14 | break;
|
15 | case DocumentType.Mutation:
|
16 | name = 'Mutation';
|
17 | break;
|
18 | case DocumentType.Subscription:
|
19 | name = 'Subscription';
|
20 | break;
|
21 | }
|
22 | return name;
|
23 | }
|
24 | export function parser(document) {
|
25 | var cached = cache.get(document);
|
26 | if (cached)
|
27 | return cached;
|
28 | var variables, type, name;
|
29 | __DEV__ ? invariant(!!document && !!document.kind, "Argument of ".concat(document, " passed to parser was not a valid GraphQL ") +
|
30 | "DocumentNode. You may need to use 'graphql-tag' or another method " +
|
31 | "to convert your operation into a document") : invariant(!!document && !!document.kind, 33);
|
32 | var fragments = [];
|
33 | var queries = [];
|
34 | var mutations = [];
|
35 | var subscriptions = [];
|
36 | for (var _i = 0, _a = document.definitions; _i < _a.length; _i++) {
|
37 | var x = _a[_i];
|
38 | if (x.kind === 'FragmentDefinition') {
|
39 | fragments.push(x);
|
40 | continue;
|
41 | }
|
42 | if (x.kind === 'OperationDefinition') {
|
43 | switch (x.operation) {
|
44 | case 'query':
|
45 | queries.push(x);
|
46 | break;
|
47 | case 'mutation':
|
48 | mutations.push(x);
|
49 | break;
|
50 | case 'subscription':
|
51 | subscriptions.push(x);
|
52 | break;
|
53 | }
|
54 | }
|
55 | }
|
56 | __DEV__ ? invariant(!fragments.length ||
|
57 | (queries.length || mutations.length || subscriptions.length), "Passing only a fragment to 'graphql' is not yet supported. " +
|
58 | "You must include a query, subscription or mutation as well") : invariant(!fragments.length ||
|
59 | (queries.length || mutations.length || subscriptions.length), 34);
|
60 | __DEV__ ? invariant(queries.length + mutations.length + subscriptions.length <= 1, "react-apollo only supports a query, subscription, or a mutation per HOC. " +
|
61 | "".concat(document, " had ").concat(queries.length, " queries, ").concat(subscriptions.length, " ") +
|
62 | "subscriptions and ".concat(mutations.length, " mutations. ") +
|
63 | "You can use 'compose' to join multiple operation types to a component") : invariant(queries.length + mutations.length + subscriptions.length <= 1, 35);
|
64 | type = queries.length ? DocumentType.Query : DocumentType.Mutation;
|
65 | if (!queries.length && !mutations.length)
|
66 | type = DocumentType.Subscription;
|
67 | var definitions = queries.length
|
68 | ? queries
|
69 | : mutations.length
|
70 | ? mutations
|
71 | : subscriptions;
|
72 | __DEV__ ? invariant(definitions.length === 1, "react-apollo only supports one definition per HOC. ".concat(document, " had ") +
|
73 | "".concat(definitions.length, " definitions. ") +
|
74 | "You can use 'compose' to join multiple operation types to a component") : invariant(definitions.length === 1, 36);
|
75 | var definition = definitions[0];
|
76 | variables = definition.variableDefinitions || [];
|
77 | if (definition.name && definition.name.kind === 'Name') {
|
78 | name = definition.name.value;
|
79 | }
|
80 | else {
|
81 | name = 'data';
|
82 | }
|
83 | var payload = { name: name, type: type, variables: variables };
|
84 | cache.set(document, payload);
|
85 | return payload;
|
86 | }
|
87 | export function verifyDocumentType(document, type) {
|
88 | var operation = parser(document);
|
89 | var requiredOperationName = operationName(type);
|
90 | var usedOperationName = operationName(operation.type);
|
91 | __DEV__ ? invariant(operation.type === type, "Running a ".concat(requiredOperationName, " requires a graphql ") +
|
92 | "".concat(requiredOperationName, ", but a ").concat(usedOperationName, " was used instead.")) : invariant(operation.type === type, 37);
|
93 | }
|
94 |
|
\ | No newline at end of file |