UNPKG

3.74 kBJavaScriptView Raw
1"use strict";
2var assign = require('lodash.assign');
3var countBy = require('lodash.countby');
4var identity = require('lodash.identity');
5function getMutationDefinition(doc) {
6 checkDocument(doc);
7 var mutationDef = null;
8 doc.definitions.forEach(function (definition) {
9 if (definition.kind === 'OperationDefinition'
10 && definition.operation === 'mutation') {
11 mutationDef = definition;
12 }
13 });
14 if (!mutationDef) {
15 throw new Error('Must contain a mutation definition.');
16 }
17 return mutationDef;
18}
19exports.getMutationDefinition = getMutationDefinition;
20function checkDocument(doc) {
21 if (doc.kind !== 'Document') {
22 throw new Error("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");
23 }
24 var definitionTypes = doc.definitions.map(function (definition) {
25 if (definition.kind !== 'OperationDefinition' && definition.kind !== 'FragmentDefinition') {
26 throw new Error("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\"");
27 }
28 return definition.kind;
29 });
30 var typeCounts = countBy(definitionTypes, identity);
31 if (typeCounts['OperationDefinition'] > 1) {
32 throw new Error('Queries must have exactly one operation definition.');
33 }
34}
35exports.checkDocument = checkDocument;
36function getOperationName(doc) {
37 var res = '';
38 doc.definitions.forEach(function (definition) {
39 if (definition.kind === 'OperationDefinition'
40 && definition.name) {
41 res = definition.name.value;
42 }
43 });
44 return res;
45}
46exports.getOperationName = getOperationName;
47function getFragmentDefinitions(doc) {
48 var fragmentDefinitions = doc.definitions.filter(function (definition) {
49 if (definition.kind === 'FragmentDefinition') {
50 return true;
51 }
52 else {
53 return false;
54 }
55 });
56 return fragmentDefinitions;
57}
58exports.getFragmentDefinitions = getFragmentDefinitions;
59function getQueryDefinition(doc) {
60 checkDocument(doc);
61 var queryDef = null;
62 doc.definitions.map(function (definition) {
63 if (definition.kind === 'OperationDefinition'
64 && definition.operation === 'query') {
65 queryDef = definition;
66 }
67 });
68 if (!queryDef) {
69 throw new Error('Must contain a query definition.');
70 }
71 return queryDef;
72}
73exports.getQueryDefinition = getQueryDefinition;
74function getFragmentDefinition(doc) {
75 if (doc.kind !== 'Document') {
76 throw new Error("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");
77 }
78 if (doc.definitions.length > 1) {
79 throw new Error('Fragment must have exactly one definition.');
80 }
81 var fragmentDef = doc.definitions[0];
82 if (fragmentDef.kind !== 'FragmentDefinition') {
83 throw new Error('Must be a fragment definition.');
84 }
85 return fragmentDef;
86}
87exports.getFragmentDefinition = getFragmentDefinition;
88function createFragmentMap(fragments) {
89 if (fragments === void 0) { fragments = []; }
90 var symTable = {};
91 fragments.forEach(function (fragment) {
92 symTable[fragment.name.value] = fragment;
93 });
94 return symTable;
95}
96exports.createFragmentMap = createFragmentMap;
97function addFragmentsToDocument(queryDoc, fragments) {
98 checkDocument(queryDoc);
99 return assign({}, queryDoc, {
100 definitions: queryDoc.definitions.concat(fragments),
101 });
102}
103exports.addFragmentsToDocument = addFragmentsToDocument;
104//# sourceMappingURL=getFromAST.js.map
\No newline at end of file