UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

85 lines (84 loc) 4.03 kB
import { Kind } from 'graphql'; import { AddedAttributeAlreadyExistsError, ChangedAncestorCoordinateNotFoundError, ChangedCoordinateKindMismatchError, ChangePathMissingError, DeletedAttributeNotFoundError, DeletedCoordinateNotFound, ValueMismatchError, } from '../errors.js'; import { nameNode, stringNode } from '../node-templates.js'; import { parentPath } from '../utils.js'; export function enumValueRemoved(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new ChangePathMissingError(change), change); return; } const enumNode = nodeByPath.get(parentPath(change.path)); if (!enumNode) { config.onError(new DeletedCoordinateNotFound(Kind.ENUM_TYPE_DEFINITION, change.meta.removedEnumValueName), change); return; } if (enumNode.kind !== Kind.ENUM_TYPE_DEFINITION) { config.onError(new ChangedCoordinateKindMismatchError(Kind.ENUM_TYPE_DEFINITION, enumNode.kind), change); return; } if (enumNode.values === undefined || enumNode.values.length === 0) { config.onError(new DeletedAttributeNotFoundError(Kind.ENUM_TYPE_DEFINITION, 'values', change.meta.removedEnumValueName), change); return; } const beforeLength = enumNode.values.length; enumNode.values = enumNode.values.filter(f => f.name.value !== change.meta.removedEnumValueName); if (beforeLength === enumNode.values.length) { config.onError(new DeletedAttributeNotFoundError(change.path, change.type, 'values', change.meta.removedEnumValueName), change); return; } // delete the reference to the removed field. nodeByPath.delete(change.path); } export function enumValueAdded(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new ChangePathMissingError(change), change); return; } const enumValuePath = change.path; const enumNode = nodeByPath.get(parentPath(enumValuePath)); const changedNode = nodeByPath.get(enumValuePath); if (!enumNode) { config.onError(new ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedEnumValueName), change); return; } if (changedNode) { config.onError(new AddedAttributeAlreadyExistsError(change.path, change.type, 'values', change.meta.addedEnumValueName), change); return; } if (enumNode.kind !== Kind.ENUM_TYPE_DEFINITION) { config.onError(new ChangedCoordinateKindMismatchError(Kind.ENUM_TYPE_DEFINITION, enumNode.kind), change); return; } const c = change; const node = { kind: Kind.ENUM_VALUE_DEFINITION, name: nameNode(c.meta.addedEnumValueName), description: c.meta.addedDirectiveDescription ? stringNode(c.meta.addedDirectiveDescription) : undefined, }; enumNode.values = [...(enumNode.values ?? []), node]; nodeByPath.set(enumValuePath, node); } export function enumValueDescriptionChanged(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new ChangePathMissingError(change), change); return; } const enumValueNode = nodeByPath.get(change.path); if (!enumValueNode) { config.onError(new ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.newEnumValueDescription), change); return; } if (enumValueNode.kind !== Kind.ENUM_VALUE_DEFINITION) { config.onError(new ChangedCoordinateKindMismatchError(Kind.ENUM_VALUE_DEFINITION, enumValueNode.kind), change); return; } const oldValueMatches = change.meta.oldEnumValueDescription === (enumValueNode.description?.value ?? null); if (!oldValueMatches) { config.onError(new ValueMismatchError(Kind.ENUM_TYPE_DEFINITION, change.meta.oldEnumValueDescription, enumValueNode.description?.value), change); } enumValueNode.description = change.meta.newEnumValueDescription ? stringNode(change.meta.newEnumValueDescription) : undefined; }