@graphql-inspector/core
Version:
Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.
67 lines (66 loc) • 2.84 kB
JavaScript
import { Kind } from 'graphql';
import { compareLists, isNotEqual, isVoid } from '../utils/compare.js';
import { isDeprecated } from '../utils/is-deprecated.js';
import { changesInArgument } from './argument.js';
import { directiveUsageAdded, directiveUsageRemoved } from './changes/directive-usage.js';
import { fieldArgumentAdded, fieldArgumentRemoved, fieldDeprecationAdded, fieldDeprecationReasonAdded, fieldDeprecationReasonChanged, fieldDeprecationReasonRemoved, fieldDeprecationRemoved, fieldDescriptionAdded, fieldDescriptionChanged, fieldDescriptionRemoved, fieldTypeChanged, } from './changes/field.js';
export function changesInField(type, oldField, newField, addChange) {
if (isNotEqual(oldField.description, newField.description)) {
if (isVoid(oldField.description)) {
addChange(fieldDescriptionAdded(type, newField));
}
else if (isVoid(newField.description)) {
addChange(fieldDescriptionRemoved(type, oldField));
}
else {
addChange(fieldDescriptionChanged(type, oldField, newField));
}
}
if (isNotEqual(isDeprecated(oldField), isDeprecated(newField))) {
if (isDeprecated(newField)) {
addChange(fieldDeprecationAdded(type, newField));
}
else {
addChange(fieldDeprecationRemoved(type, oldField));
}
}
if (isNotEqual(oldField.deprecationReason, newField.deprecationReason)) {
if (isVoid(oldField.deprecationReason)) {
addChange(fieldDeprecationReasonAdded(type, newField));
}
else if (isVoid(newField.deprecationReason)) {
addChange(fieldDeprecationReasonRemoved(type, oldField));
}
else {
addChange(fieldDeprecationReasonChanged(type, oldField, newField));
}
}
if (isNotEqual(oldField.type.toString(), newField.type.toString())) {
addChange(fieldTypeChanged(type, oldField, newField));
}
compareLists(oldField.args, newField.args, {
onAdded(arg) {
addChange(fieldArgumentAdded(type, newField, arg));
},
onRemoved(arg) {
addChange(fieldArgumentRemoved(type, oldField, arg));
},
onMutual(arg) {
changesInArgument(type, oldField, arg.oldVersion, arg.newVersion, addChange);
},
});
compareLists(oldField.astNode?.directives || [], newField.astNode?.directives || [], {
onAdded(directive) {
addChange(directiveUsageAdded(Kind.FIELD_DEFINITION, directive, {
parentType: type,
field: newField,
}));
},
onRemoved(arg) {
addChange(directiveUsageRemoved(Kind.FIELD_DEFINITION, arg, {
parentType: type,
field: oldField,
}));
},
});
}