UNPKG

1.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var graphql_1 = require("graphql");
4var OperationStore = (function () {
5 function OperationStore(schema) {
6 this.schema = schema;
7 this.storedOperations = new Map();
8 }
9 OperationStore.prototype.put = function (operation) {
10 function isOperationDefinition(definition) {
11 return definition.kind === graphql_1.Kind.OPERATION_DEFINITION;
12 }
13 function isString(definition) {
14 return typeof definition === 'string';
15 }
16 var ast = isString(operation) ? graphql_1.parse(operation) : operation;
17 var definitions = ast.definitions.filter(isOperationDefinition);
18 if (definitions.length === 0) {
19 throw new Error('OperationDefinitionNode must contain at least one definition');
20 }
21 if (definitions.length > 1) {
22 throw new Error('OperationDefinitionNode must contain only one definition');
23 }
24 var validationErrors = graphql_1.validate(this.schema, ast);
25 if (validationErrors.length > 0) {
26 var messages = validationErrors.map(function (e) { return e.message; });
27 var e = new Error("Validation Errors:\n" + messages.join('\n'));
28 e['originalErrors'] = validationErrors;
29 throw e;
30 }
31 this.storedOperations.set(definitions[0].name.value, ast);
32 };
33 OperationStore.prototype.get = function (operationName) {
34 return this.storedOperations.get(operationName);
35 };
36 OperationStore.prototype.delete = function (operationName) {
37 return this.storedOperations.delete(operationName);
38 };
39 OperationStore.prototype.getMap = function () {
40 return this.storedOperations;
41 };
42 return OperationStore;
43}());
44exports.OperationStore = OperationStore;
45//# sourceMappingURL=operationStore.js.map
\No newline at end of file