UNPKG

@graphql-inspector/ci

Version:

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

64 lines (63 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.simplifyChanges = void 0; const change_js_1 = require("../changes/change.js"); const simpleChangeTypes = new Set([ change_js_1.ChangeType.DirectiveAdded, change_js_1.ChangeType.DirectiveArgumentAdded, change_js_1.ChangeType.DirectiveLocationAdded, change_js_1.ChangeType.DirectiveUsageArgumentAdded, change_js_1.ChangeType.DirectiveUsageArgumentDefinitionAdded, change_js_1.ChangeType.DirectiveUsageEnumAdded, change_js_1.ChangeType.DirectiveUsageEnumValueAdded, change_js_1.ChangeType.DirectiveUsageFieldAdded, change_js_1.ChangeType.DirectiveUsageFieldDefinitionAdded, change_js_1.ChangeType.DirectiveUsageInputFieldDefinitionAdded, change_js_1.ChangeType.DirectiveUsageInputObjectAdded, change_js_1.ChangeType.DirectiveUsageInterfaceAdded, change_js_1.ChangeType.DirectiveUsageObjectAdded, change_js_1.ChangeType.DirectiveUsageScalarAdded, change_js_1.ChangeType.DirectiveUsageSchemaAdded, change_js_1.ChangeType.DirectiveUsageUnionMemberAdded, change_js_1.ChangeType.EnumValueAdded, change_js_1.ChangeType.EnumValueDeprecationReasonAdded, change_js_1.ChangeType.FieldAdded, change_js_1.ChangeType.FieldArgumentAdded, change_js_1.ChangeType.FieldDeprecationAdded, // These are not additions -- but this is necessary to eliminate nested removals for directives // because the deprecationReasons are redundant with directives change_js_1.ChangeType.FieldDeprecationRemoved, change_js_1.ChangeType.FieldDeprecationReasonChanged, change_js_1.ChangeType.EnumValueDeprecationReasonChanged, change_js_1.ChangeType.FieldDeprecationReasonAdded, change_js_1.ChangeType.FieldDescriptionAdded, change_js_1.ChangeType.InputFieldAdded, change_js_1.ChangeType.InputFieldDescriptionAdded, change_js_1.ChangeType.ObjectTypeInterfaceAdded, change_js_1.ChangeType.TypeAdded, change_js_1.ChangeType.TypeDescriptionAdded, change_js_1.ChangeType.UnionMemberAdded, ]); const parentPath = (path) => { const lastDividerIndex = path.lastIndexOf('.'); return lastDividerIndex === -1 ? path : path.substring(0, lastDividerIndex); }; const simplifyChanges = ({ changes }) => { // Track which paths contained changes that represent a group of changes to the schema // e.g. the addition of a type implicity contains the addition of that type's fields. const changePaths = []; const filteredChanges = changes.filter(({ path, type }) => { if (path) { const parent = parentPath(path); const matches = changePaths.filter(matchedPath => matchedPath.startsWith(parent)); const hasChangedParent = matches.length > 0; if (simpleChangeTypes.has(type)) { changePaths.push(path); } return !hasChangedParent; } return true; }); return filteredChanges; }; exports.simplifyChanges = simplifyChanges;