UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

37 lines 1.71 kB
import { GraphQLError } from "../../error/GraphQLError.mjs"; import { isEnumType } from "../../type/definition.mjs"; export function UniqueEnumValueNamesRule(context) { const schema = context.getSchema(); const existingTypeMap = schema ? schema.getTypeMap() : Object.create(null); const knownValueNames = new Map(); return { EnumTypeDefinition: checkValueUniqueness, EnumTypeExtension: checkValueUniqueness, }; function checkValueUniqueness(node) { const typeName = node.name.value; let valueNames = knownValueNames.get(typeName); if (valueNames == null) { valueNames = new Map(); knownValueNames.set(typeName, valueNames); } const valueNodes = node.values ?? []; for (const valueDef of valueNodes) { const valueName = valueDef.name.value; const existingType = existingTypeMap[typeName]; if (isEnumType(existingType) && existingType.getValue(valueName)) { context.reportError(new GraphQLError(`Enum value "${typeName}.${valueName}" already exists in the schema. It cannot also be defined in this type extension.`, { nodes: valueDef.name })); continue; } const knownValueName = valueNames.get(valueName); if (knownValueName != null) { context.reportError(new GraphQLError(`Enum value "${typeName}.${valueName}" can only be defined once.`, { nodes: [knownValueName, valueDef.name] })); } else { valueNames.set(valueName, valueDef.name); } } return false; } } //# sourceMappingURL=UniqueEnumValueNamesRule.js.map