UNPKG

@graphql-inspector/cli

Version:

Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.

84 lines (83 loc) 4.38 kB
"use strict"; 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, oldInput == null)); changesInInputField(newInput, null, field, addChange); }, onRemoved(field) { addChange((0, input_js_1.inputFieldRemoved)(oldInput, field)); }, onMutual(field) { changesInInputField(newInput, field.oldVersion, field.newVersion, addChange); }, }); (0, compare_js_1.compareDirectiveLists)(oldInput?.astNode?.directives || [], newInput.astNode?.directives || [], { onAdded(directive) { addChange((0, directive_usage_js_1.directiveUsageAdded)(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION, directive, newInput, oldInput === null)); (0, directive_usage_js_1.directiveUsageChanged)(null, directive, addChange, newInput); }, onMutual(directive) { (0, directive_usage_js_1.directiveUsageChanged)(directive.oldVersion, directive.newVersion, addChange, 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.isVoid)(oldField)) { 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.isVoid)(oldField) && (0, compare_js_1.isNotEqual)(oldField.type.toString(), newField.type.toString())) { addChange((0, input_js_1.inputFieldTypeChanged)(input, oldField, newField)); } } if (newField.astNode?.directives) { (0, compare_js_1.compareDirectiveLists)(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, }, oldField === null)); (0, directive_usage_js_1.directiveUsageChanged)(null, directive, addChange, input, newField); }, onMutual(directive) { (0, directive_usage_js_1.directiveUsageChanged)(directive.oldVersion, directive.newVersion, addChange, input, newField); }, onRemoved(directive) { addChange((0, directive_usage_js_1.directiveUsageRemoved)(graphql_1.Kind.INPUT_VALUE_DEFINITION, directive, { type: input, field: newField, })); }, }); } }