1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var ts_invariant_1 = require("ts-invariant");
|
5 | var assign_1 = require("./util/assign");
|
6 | var storeUtils_1 = require("./storeUtils");
|
7 | function getMutationDefinition(doc) {
|
8 | checkDocument(doc);
|
9 | var mutationDef = doc.definitions.filter(function (definition) {
|
10 | return definition.kind === 'OperationDefinition' &&
|
11 | definition.operation === 'mutation';
|
12 | })[0];
|
13 | ts_invariant_1.invariant(mutationDef, 'Must contain a mutation definition.');
|
14 | return mutationDef;
|
15 | }
|
16 | exports.getMutationDefinition = getMutationDefinition;
|
17 | function checkDocument(doc) {
|
18 | ts_invariant_1.invariant(doc && doc.kind === 'Document', "Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql");
|
19 | var operations = doc.definitions
|
20 | .filter(function (d) { return d.kind !== 'FragmentDefinition'; })
|
21 | .map(function (definition) {
|
22 | if (definition.kind !== 'OperationDefinition') {
|
23 | throw new ts_invariant_1.InvariantError("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\"");
|
24 | }
|
25 | return definition;
|
26 | });
|
27 | ts_invariant_1.invariant(operations.length <= 1, "Ambiguous GraphQL document: contains " + operations.length + " operations");
|
28 | return doc;
|
29 | }
|
30 | exports.checkDocument = checkDocument;
|
31 | function getOperationDefinition(doc) {
|
32 | checkDocument(doc);
|
33 | return doc.definitions.filter(function (definition) { return definition.kind === 'OperationDefinition'; })[0];
|
34 | }
|
35 | exports.getOperationDefinition = getOperationDefinition;
|
36 | function getOperationDefinitionOrDie(document) {
|
37 | var def = getOperationDefinition(document);
|
38 | ts_invariant_1.invariant(def, "GraphQL document is missing an operation");
|
39 | return def;
|
40 | }
|
41 | exports.getOperationDefinitionOrDie = getOperationDefinitionOrDie;
|
42 | function getOperationName(doc) {
|
43 | return (doc.definitions
|
44 | .filter(function (definition) {
|
45 | return definition.kind === 'OperationDefinition' && definition.name;
|
46 | })
|
47 | .map(function (x) { return x.name.value; })[0] || null);
|
48 | }
|
49 | exports.getOperationName = getOperationName;
|
50 | function getFragmentDefinitions(doc) {
|
51 | return doc.definitions.filter(function (definition) { return definition.kind === 'FragmentDefinition'; });
|
52 | }
|
53 | exports.getFragmentDefinitions = getFragmentDefinitions;
|
54 | function getQueryDefinition(doc) {
|
55 | var queryDef = getOperationDefinition(doc);
|
56 | ts_invariant_1.invariant(queryDef && queryDef.operation === 'query', 'Must contain a query definition.');
|
57 | return queryDef;
|
58 | }
|
59 | exports.getQueryDefinition = getQueryDefinition;
|
60 | function getFragmentDefinition(doc) {
|
61 | ts_invariant_1.invariant(doc.kind === 'Document', "Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql");
|
62 | ts_invariant_1.invariant(doc.definitions.length <= 1, 'Fragment must have exactly one definition.');
|
63 | var fragmentDef = doc.definitions[0];
|
64 | ts_invariant_1.invariant(fragmentDef.kind === 'FragmentDefinition', 'Must be a fragment definition.');
|
65 | return fragmentDef;
|
66 | }
|
67 | exports.getFragmentDefinition = getFragmentDefinition;
|
68 | function getMainDefinition(queryDoc) {
|
69 | checkDocument(queryDoc);
|
70 | var fragmentDefinition;
|
71 | for (var _i = 0, _a = queryDoc.definitions; _i < _a.length; _i++) {
|
72 | var definition = _a[_i];
|
73 | if (definition.kind === 'OperationDefinition') {
|
74 | var operation = definition.operation;
|
75 | if (operation === 'query' ||
|
76 | operation === 'mutation' ||
|
77 | operation === 'subscription') {
|
78 | return definition;
|
79 | }
|
80 | }
|
81 | if (definition.kind === 'FragmentDefinition' && !fragmentDefinition) {
|
82 | fragmentDefinition = definition;
|
83 | }
|
84 | }
|
85 | if (fragmentDefinition) {
|
86 | return fragmentDefinition;
|
87 | }
|
88 | throw new ts_invariant_1.InvariantError('Expected a parsed GraphQL query with a query, mutation, subscription, or a fragment.');
|
89 | }
|
90 | exports.getMainDefinition = getMainDefinition;
|
91 | function createFragmentMap(fragments) {
|
92 | if (fragments === void 0) { fragments = []; }
|
93 | var symTable = {};
|
94 | fragments.forEach(function (fragment) {
|
95 | symTable[fragment.name.value] = fragment;
|
96 | });
|
97 | return symTable;
|
98 | }
|
99 | exports.createFragmentMap = createFragmentMap;
|
100 | function getDefaultValues(definition) {
|
101 | if (definition &&
|
102 | definition.variableDefinitions &&
|
103 | definition.variableDefinitions.length) {
|
104 | var defaultValues = definition.variableDefinitions
|
105 | .filter(function (_a) {
|
106 | var defaultValue = _a.defaultValue;
|
107 | return defaultValue;
|
108 | })
|
109 | .map(function (_a) {
|
110 | var variable = _a.variable, defaultValue = _a.defaultValue;
|
111 | var defaultValueObj = {};
|
112 | storeUtils_1.valueToObjectRepresentation(defaultValueObj, variable.name, defaultValue);
|
113 | return defaultValueObj;
|
114 | });
|
115 | return assign_1.assign.apply(void 0, tslib_1.__spreadArrays([{}], defaultValues));
|
116 | }
|
117 | return {};
|
118 | }
|
119 | exports.getDefaultValues = getDefaultValues;
|
120 | function variablesInOperation(operation) {
|
121 | var names = new Set();
|
122 | if (operation.variableDefinitions) {
|
123 | for (var _i = 0, _a = operation.variableDefinitions; _i < _a.length; _i++) {
|
124 | var definition = _a[_i];
|
125 | names.add(definition.variable.name.value);
|
126 | }
|
127 | }
|
128 | return names;
|
129 | }
|
130 | exports.variablesInOperation = variablesInOperation;
|
131 |
|
\ | No newline at end of file |