@graphql-inspector/core
Version:
Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.
73 lines (72 loc) • 3.55 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.changesInInputObject = changesInInputObject;
const graphql_1 = require("graphql");
const compare_js_1 = require("../utils/compare.js");
const directive_usage_js_1 = require("./changes/directive-usage.js");
const input_js_1 = require("./changes/input.js");
function changesInInputObject(oldInput, newInput, addChange) {
const oldFields = oldInput.getFields();
const newFields = newInput.getFields();
(0, compare_js_1.compareLists)(Object.values(oldFields), Object.values(newFields), {
onAdded(field) {
addChange((0, input_js_1.inputFieldAdded)(newInput, field));
},
onRemoved(field) {
addChange((0, input_js_1.inputFieldRemoved)(oldInput, field));
},
onMutual(field) {
changesInInputField(oldInput, field.oldVersion, field.newVersion, addChange);
},
});
(0, compare_js_1.compareLists)(oldInput.astNode?.directives || [], newInput.astNode?.directives || [], {
onAdded(directive) {
addChange((0, directive_usage_js_1.directiveUsageAdded)(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION, directive, newInput));
},
onRemoved(directive) {
addChange((0, directive_usage_js_1.directiveUsageRemoved)(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION, directive, oldInput));
},
});
}
function changesInInputField(input, oldField, newField, addChange) {
if ((0, compare_js_1.isNotEqual)(oldField.description, newField.description)) {
if ((0, compare_js_1.isVoid)(oldField.description)) {
addChange((0, input_js_1.inputFieldDescriptionAdded)(input, newField));
}
else if ((0, compare_js_1.isVoid)(newField.description)) {
addChange((0, input_js_1.inputFieldDescriptionRemoved)(input, oldField));
}
else {
addChange((0, input_js_1.inputFieldDescriptionChanged)(input, oldField, newField));
}
}
if ((0, compare_js_1.isNotEqual)(oldField.defaultValue, newField.defaultValue)) {
if (Array.isArray(oldField.defaultValue) && Array.isArray(newField.defaultValue)) {
if ((0, compare_js_1.diffArrays)(oldField.defaultValue, newField.defaultValue).length > 0) {
addChange((0, input_js_1.inputFieldDefaultValueChanged)(input, oldField, newField));
}
}
else if (JSON.stringify(oldField.defaultValue) !== JSON.stringify(newField.defaultValue)) {
addChange((0, input_js_1.inputFieldDefaultValueChanged)(input, oldField, newField));
}
}
if ((0, compare_js_1.isNotEqual)(oldField.type.toString(), newField.type.toString())) {
addChange((0, input_js_1.inputFieldTypeChanged)(input, oldField, newField));
}
if (oldField.astNode?.directives && newField.astNode?.directives) {
(0, compare_js_1.compareLists)(oldField.astNode.directives || [], newField.astNode.directives || [], {
onAdded(directive) {
addChange((0, directive_usage_js_1.directiveUsageAdded)(graphql_1.Kind.INPUT_VALUE_DEFINITION, directive, {
type: input,
field: newField,
}));
},
onRemoved(directive) {
addChange((0, directive_usage_js_1.directiveUsageRemoved)(graphql_1.Kind.INPUT_VALUE_DEFINITION, directive, {
type: input,
field: oldField,
}));
},
});
}
}
;