1 | "use strict";
|
2 | var assign = require('lodash.assign');
|
3 | var countBy = require('lodash.countby');
|
4 | var identity = require('lodash.identity');
|
5 | function 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 | }
|
19 | exports.getMutationDefinition = getMutationDefinition;
|
20 | function 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 | }
|
35 | exports.checkDocument = checkDocument;
|
36 | function 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 | }
|
46 | exports.getOperationName = getOperationName;
|
47 | function 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 | }
|
58 | exports.getFragmentDefinitions = getFragmentDefinitions;
|
59 | function 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 | }
|
73 | exports.getQueryDefinition = getQueryDefinition;
|
74 | function 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 | }
|
87 | exports.getFragmentDefinition = getFragmentDefinition;
|
88 | function 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 | }
|
96 | exports.createFragmentMap = createFragmentMap;
|
97 | function addFragmentsToDocument(queryDoc, fragments) {
|
98 | checkDocument(queryDoc);
|
99 | return assign({}, queryDoc, {
|
100 | definitions: queryDoc.definitions.concat(fragments),
|
101 | });
|
102 | }
|
103 | exports.addFragmentsToDocument = addFragmentsToDocument;
|
104 |
|
\ | No newline at end of file |