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