UNPKG

1.81 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.UniqueOperationTypesRule = UniqueOperationTypesRule;
7
8var _GraphQLError = require("../../error/GraphQLError.js");
9
10/**
11 * Unique operation types
12 *
13 * A GraphQL document is only valid if it has only one type per operation.
14 */
15function UniqueOperationTypesRule(context) {
16 var schema = context.getSchema();
17 var definedOperationTypes = Object.create(null);
18 var existingOperationTypes = schema ? {
19 query: schema.getQueryType(),
20 mutation: schema.getMutationType(),
21 subscription: schema.getSubscriptionType()
22 } : {};
23 return {
24 SchemaDefinition: checkOperationTypes,
25 SchemaExtension: checkOperationTypes
26 };
27
28 function checkOperationTypes(node) {
29 var _node$operationTypes;
30
31 // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
32 var operationTypesNodes = (_node$operationTypes = node.operationTypes) !== null && _node$operationTypes !== void 0 ? _node$operationTypes : [];
33
34 for (var _i2 = 0; _i2 < operationTypesNodes.length; _i2++) {
35 var operationType = operationTypesNodes[_i2];
36 var operation = operationType.operation;
37 var alreadyDefinedOperationType = definedOperationTypes[operation];
38
39 if (existingOperationTypes[operation]) {
40 context.reportError(new _GraphQLError.GraphQLError("Type for ".concat(operation, " already defined in the schema. It cannot be redefined."), operationType));
41 } else if (alreadyDefinedOperationType) {
42 context.reportError(new _GraphQLError.GraphQLError("There can be only one ".concat(operation, " type in schema."), [alreadyDefinedOperationType, operationType]));
43 } else {
44 definedOperationTypes[operation] = operationType;
45 }
46 }
47
48 return false;
49 }
50}