@graphql-inspector/patch
Version:
Applies changes output from @graphql-inspect/diff
90 lines (89 loc) • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.enumValueRemoved = enumValueRemoved;
exports.enumValueAdded = enumValueAdded;
exports.enumValueDescriptionChanged = enumValueDescriptionChanged;
const graphql_1 = require("graphql");
const errors_js_1 = require("../errors.js");
const node_templates_js_1 = require("../node-templates.js");
const utils_js_1 = require("../utils.js");
function enumValueRemoved(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const enumNode = nodeByPath.get((0, utils_js_1.parentPath)(change.path));
if (!enumNode) {
config.onError(new errors_js_1.DeletedCoordinateNotFound(graphql_1.Kind.ENUM_TYPE_DEFINITION, change.meta.removedEnumValueName), change);
return;
}
if (enumNode.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.ENUM_TYPE_DEFINITION, enumNode.kind), change);
return;
}
if (enumNode.values === undefined || enumNode.values.length === 0) {
config.onError(new errors_js_1.DeletedAttributeNotFoundError(graphql_1.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 errors_js_1.DeletedAttributeNotFoundError(change.path, change.type, 'values', change.meta.removedEnumValueName), change);
return;
}
// delete the reference to the removed field.
nodeByPath.delete(change.path);
}
function enumValueAdded(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const enumValuePath = change.path;
const enumNode = nodeByPath.get((0, utils_js_1.parentPath)(enumValuePath));
const changedNode = nodeByPath.get(enumValuePath);
if (!enumNode) {
config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedEnumValueName), change);
return;
}
if (changedNode) {
config.onError(new errors_js_1.AddedAttributeAlreadyExistsError(change.path, change.type, 'values', change.meta.addedEnumValueName), change);
return;
}
if (enumNode.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.ENUM_TYPE_DEFINITION, enumNode.kind), change);
return;
}
const c = change;
const node = {
kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
name: (0, node_templates_js_1.nameNode)(c.meta.addedEnumValueName),
description: c.meta.addedDirectiveDescription
? (0, node_templates_js_1.stringNode)(c.meta.addedDirectiveDescription)
: undefined,
};
enumNode.values = [...(enumNode.values ?? []), node];
nodeByPath.set(enumValuePath, node);
}
function enumValueDescriptionChanged(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const enumValueNode = nodeByPath.get(change.path);
if (!enumValueNode) {
config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.newEnumValueDescription), change);
return;
}
if (enumValueNode.kind !== graphql_1.Kind.ENUM_VALUE_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.ENUM_VALUE_DEFINITION, enumValueNode.kind), change);
return;
}
const oldValueMatches = change.meta.oldEnumValueDescription === (enumValueNode.description?.value ?? null);
if (!oldValueMatches) {
config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.ENUM_TYPE_DEFINITION, change.meta.oldEnumValueDescription, enumValueNode.description?.value), change);
}
enumValueNode.description = change.meta.newEnumValueDescription
? (0, node_templates_js_1.stringNode)(change.meta.newEnumValueDescription)
: undefined;
}