@graphql-inspector/ci
Version:
Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.
36 lines (35 loc) • 1.85 kB
JavaScript
import { AddedAttributeAlreadyExistsError, ChangedAncestorCoordinateNotFoundError, ChangePathMissingError, DeletedAncestorCoordinateNotFoundError, DeletedAttributeNotFoundError, } from '../errors.js';
import { namedTypeNode } from '../node-templates.js';
import { findNamedNode, parentPath } from '../utils.js';
export function unionMemberAdded(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new ChangePathMissingError(change), change);
return;
}
const union = nodeByPath.get(parentPath(change.path));
if (!union) {
config.onError(new ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedUnionMemberTypeName), change);
return;
}
if (findNamedNode(union.types, change.meta.addedUnionMemberTypeName)) {
config.onError(new AddedAttributeAlreadyExistsError(change.path, change.type, 'types', change.meta.addedUnionMemberTypeName), change);
return;
}
union.types = [...(union.types ?? []), namedTypeNode(change.meta.addedUnionMemberTypeName)];
}
export function unionMemberRemoved(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new ChangePathMissingError(change), change);
return;
}
const union = nodeByPath.get(parentPath(change.path));
if (!union) {
config.onError(new DeletedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.removedUnionMemberTypeName), change);
return;
}
if (!findNamedNode(union.types, change.meta.removedUnionMemberTypeName)) {
config.onError(new DeletedAttributeNotFoundError(change.path, change.type, 'types', change.meta.removedUnionMemberTypeName), change);
return;
}
union.types = union.types.filter(t => t.name.value !== change.meta.removedUnionMemberTypeName);
}