UniqueEnumValueNames.js 2.1 KB

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