UNPKG

1.95 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.UniqueEnumValueNamesRule = UniqueEnumValueNamesRule;
7
8var _GraphQLError = require("../../error/GraphQLError.js");
9
10var _definition = require("../../type/definition.js");
11
12/**
13 * Unique enum value names
14 *
15 * A GraphQL enum type is only valid if all its values are uniquely named.
16 */
17function UniqueEnumValueNamesRule(context) {
18 var schema = context.getSchema();
19 var existingTypeMap = schema ? schema.getTypeMap() : Object.create(null);
20 var knownValueNames = Object.create(null);
21 return {
22 EnumTypeDefinition: checkValueUniqueness,
23 EnumTypeExtension: checkValueUniqueness
24 };
25
26 function checkValueUniqueness(node) {
27 var _node$values;
28
29 var typeName = node.name.value;
30
31 if (!knownValueNames[typeName]) {
32 knownValueNames[typeName] = Object.create(null);
33 } // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
34
35
36 var valueNodes = (_node$values = node.values) !== null && _node$values !== void 0 ? _node$values : [];
37 var valueNames = knownValueNames[typeName];
38
39 for (var _i2 = 0; _i2 < valueNodes.length; _i2++) {
40 var valueDef = valueNodes[_i2];
41 var valueName = valueDef.name.value;
42 var existingType = existingTypeMap[typeName];
43
44 if ((0, _definition.isEnumType)(existingType) && existingType.getValue(valueName)) {
45 context.reportError(new _GraphQLError.GraphQLError("Enum value \"".concat(typeName, ".").concat(valueName, "\" already exists in the schema. It cannot also be defined in this type extension."), valueDef.name));
46 } else if (valueNames[valueName]) {
47 context.reportError(new _GraphQLError.GraphQLError("Enum value \"".concat(typeName, ".").concat(valueName, "\" can only be defined once."), [valueNames[valueName], valueDef.name]));
48 } else {
49 valueNames[valueName] = valueDef.name;
50 }
51 }
52
53 return false;
54 }
55}