@graphql-inspector/cli
Version:
Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.
57 lines (56 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectTypeInterfaceAdded = objectTypeInterfaceAdded;
exports.objectTypeInterfaceRemoved = objectTypeInterfaceRemoved;
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 objectTypeInterfaceAdded(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const typeNode = nodeByPath.get(change.path);
if (!typeNode) {
config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedInterfaceName), change);
return;
}
if (typeNode.kind !== graphql_1.Kind.OBJECT_TYPE_DEFINITION &&
typeNode.kind !== graphql_1.Kind.INTERFACE_TYPE_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.OBJECT_TYPE_DEFINITION, // or Kind.INTERFACE_TYPE_DEFINITION
typeNode.kind), change);
return;
}
const existing = (0, utils_js_1.findNamedNode)(typeNode.interfaces, change.meta.addedInterfaceName);
if (existing) {
config.onError(new errors_js_1.AddedAttributeAlreadyExistsError(change.path, change.type, 'interfaces', change.meta.addedInterfaceName), change);
return;
}
typeNode.interfaces = [
...(typeNode.interfaces ?? []),
(0, node_templates_js_1.namedTypeNode)(change.meta.addedInterfaceName),
];
}
function objectTypeInterfaceRemoved(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const typeNode = nodeByPath.get(change.path);
if (!typeNode) {
config.onError(new errors_js_1.DeletedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.removedInterfaceName), change);
return;
}
if (typeNode.kind !== graphql_1.Kind.OBJECT_TYPE_DEFINITION &&
typeNode.kind !== graphql_1.Kind.INTERFACE_TYPE_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.OBJECT_TYPE_DEFINITION, typeNode.kind), change);
return;
}
const existing = (0, utils_js_1.findNamedNode)(typeNode.interfaces, change.meta.removedInterfaceName);
if (!existing) {
config.onError(new errors_js_1.DeletedCoordinateNotFound(change.path, change.type), change);
return;
}
typeNode.interfaces = typeNode.interfaces?.filter(i => i.name.value !== change.meta.removedInterfaceName);
}