function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import objectValues from '../polyfills/objectValues'; import keyMap from '../jsutils/keyMap'; import inspect from '../jsutils/inspect'; import invariant from '../jsutils/invariant'; import { print } from '../language/printer'; import { visit } from '../language/visitor'; import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isNonNullType, isListType, isNamedType, isRequiredArgument, isRequiredInputField } from '../type/definition'; import { astFromValue } from './astFromValue'; export var BreakingChangeType = Object.freeze({ TYPE_REMOVED: 'TYPE_REMOVED', TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND', TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION', VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM', REQUIRED_INPUT_FIELD_ADDED: 'REQUIRED_INPUT_FIELD_ADDED', INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT', FIELD_REMOVED: 'FIELD_REMOVED', FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND', REQUIRED_ARG_ADDED: 'REQUIRED_ARG_ADDED', ARG_REMOVED: 'ARG_REMOVED', ARG_CHANGED_KIND: 'ARG_CHANGED_KIND', DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED', DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED', REQUIRED_DIRECTIVE_ARG_ADDED: 'REQUIRED_DIRECTIVE_ARG_ADDED', DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED' }); export var DangerousChangeType = Object.freeze({ VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM', TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION', OPTIONAL_INPUT_FIELD_ADDED: 'OPTIONAL_INPUT_FIELD_ADDED', OPTIONAL_ARG_ADDED: 'OPTIONAL_ARG_ADDED', INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT', ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE' }); /** * Given two schemas, returns an Array containing descriptions of all the types * of breaking changes covered by the other functions down below. */ export function findBreakingChanges(oldSchema, newSchema) { var breakingChanges = findSchemaChanges(oldSchema, newSchema).filter(function (change) { return change.type in BreakingChangeType; }); return breakingChanges; } /** * Given two schemas, returns an Array containing descriptions of all the types * of potentially dangerous changes covered by the other functions down below. */ export function findDangerousChanges(oldSchema, newSchema) { var dangerousChanges = findSchemaChanges(oldSchema, newSchema).filter(function (change) { return change.type in DangerousChangeType; }); return dangerousChanges; } function findSchemaChanges(oldSchema, newSchema) { return [].concat(findTypeChanges(oldSchema, newSchema), findDirectiveChanges(oldSchema, newSchema)); } function findDirectiveChanges(oldSchema, newSchema) { var schemaChanges = []; var directivesDiff = diff(oldSchema.getDirectives(), newSchema.getDirectives()); for (var _i2 = 0, _directivesDiff$remov2 = directivesDiff.removed; _i2 < _directivesDiff$remov2.length; _i2++) { var oldDirective = _directivesDiff$remov2[_i2]; schemaChanges.push({ type: BreakingChangeType.DIRECTIVE_REMOVED, description: "".concat(oldDirective.name, " was removed.") }); } for (var _i4 = 0, _directivesDiff$persi2 = directivesDiff.persisted; _i4 < _directivesDiff$persi2.length; _i4++) { var _ref2 = _directivesDiff$persi2[_i4]; var _oldDirective = _ref2[0]; var newDirective = _ref2[1]; var argsDiff = diff(_oldDirective.args, newDirective.args); for (var _i6 = 0, _argsDiff$added2 = argsDiff.added; _i6 < _argsDiff$added2.length; _i6++) { var newArg = _argsDiff$added2[_i6]; if (isRequiredArgument(newArg)) { schemaChanges.push({ type: BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, description: "A required arg ".concat(newArg.name, " on directive ").concat(_oldDirective.name, " was added.") }); } } for (var _i8 = 0, _argsDiff$removed2 = argsDiff.removed; _i8 < _argsDiff$removed2.length; _i8++) { var oldArg = _argsDiff$removed2[_i8]; schemaChanges.push({ type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, description: "".concat(oldArg.name, " was removed from ").concat(_oldDirective.name, ".") }); } for (var _i10 = 0, _oldDirective$locatio2 = _oldDirective.locations; _i10 < _oldDirective$locatio2.length; _i10++) { var location = _oldDirective$locatio2[_i10]; if (newDirective.locations.indexOf(location) === -1) { schemaChanges.push({ type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, description: "".concat(location, " was removed from ").concat(_oldDirective.name, ".") }); } } } return schemaChanges; } function findTypeChanges(oldSchema, newSchema) { var schemaChanges = []; var typesDiff = diff(objectValues(oldSchema.getTypeMap()), objectValues(newSchema.getTypeMap())); for (var _i12 = 0, _typesDiff$removed2 = typesDiff.removed; _i12 < _typesDiff$removed2.length; _i12++) { var oldType = _typesDiff$removed2[_i12]; schemaChanges.push({ type: BreakingChangeType.TYPE_REMOVED, description: "".concat(oldType.name, " was removed.") }); } for (var _i14 = 0, _typesDiff$persisted2 = typesDiff.persisted; _i14 < _typesDiff$persisted2.length; _i14++) { var _ref4 = _typesDiff$persisted2[_i14]; var _oldType = _ref4[0]; var newType = _ref4[1]; if (isEnumType(_oldType) && isEnumType(newType)) { schemaChanges.push.apply(schemaChanges, findEnumTypeChanges(_oldType, newType)); } else if (isUnionType(_oldType) && isUnionType(newType)) { schemaChanges.push.apply(schemaChanges, findUnionTypeChanges(_oldType, newType)); } else if (isInputObjectType(_oldType) && isInputObjectType(newType)) { schemaChanges.push.apply(schemaChanges, findInputObjectTypeChanges(_oldType, newType)); } else if (isObjectType(_oldType) && isObjectType(newType)) { schemaChanges.push.apply(schemaChanges, findObjectTypeChanges(_oldType, newType)); } else if (isInterfaceType(_oldType) && isInterfaceType(newType)) { schemaChanges.push.apply(schemaChanges, findFieldChanges(_oldType, newType)); } else if (_oldType.constructor !== newType.constructor) { schemaChanges.push({ type: BreakingChangeType.TYPE_CHANGED_KIND, description: "".concat(_oldType.name, " changed from ") + "".concat(typeKindName(_oldType), " to ").concat(typeKindName(newType), ".") }); } } return schemaChanges; } function findInputObjectTypeChanges(oldType, newType) { var schemaChanges = []; var fieldsDiff = diff(objectValues(oldType.getFields()), objectValues(newType.getFields())); for (var _i16 = 0, _fieldsDiff$added2 = fieldsDiff.added; _i16 < _fieldsDiff$added2.length; _i16++) { var newField = _fieldsDiff$added2[_i16]; if (isRequiredInputField(newField)) { schemaChanges.push({ type: BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED, description: "A required field ".concat(newField.name, " on input type ").concat(oldType.name, " was added.") }); } else { schemaChanges.push({ type: DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED, description: "An optional field ".concat(newField.name, " on input type ").concat(oldType.name, " was added.") }); } } for (var _i18 = 0, _fieldsDiff$removed2 = fieldsDiff.removed; _i18 < _fieldsDiff$removed2.length; _i18++) { var oldField = _fieldsDiff$removed2[_i18]; schemaChanges.push({ type: BreakingChangeType.FIELD_REMOVED, description: "".concat(oldType.name, ".").concat(oldField.name, " was removed.") }); } for (var _i20 = 0, _fieldsDiff$persisted2 = fieldsDiff.persisted; _i20 < _fieldsDiff$persisted2.length; _i20++) { var _ref6 = _fieldsDiff$persisted2[_i20]; var _oldField = _ref6[0]; var _newField = _ref6[1]; var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(_oldField.type, _newField.type); if (!isSafe) { schemaChanges.push({ type: BreakingChangeType.FIELD_CHANGED_KIND, description: "".concat(oldType.name, ".").concat(_oldField.name, " changed type from ") + "".concat(String(_oldField.type), " to ").concat(String(_newField.type), ".") }); } } return schemaChanges; } function findUnionTypeChanges(oldType, newType) { var schemaChanges = []; var possibleTypesDiff = diff(oldType.getTypes(), newType.getTypes()); for (var _i22 = 0, _possibleTypesDiff$ad2 = possibleTypesDiff.added; _i22 < _possibleTypesDiff$ad2.length; _i22++) { var newPossibleType = _possibleTypesDiff$ad2[_i22]; schemaChanges.push({ type: DangerousChangeType.TYPE_ADDED_TO_UNION, description: "".concat(newPossibleType.name, " was added to union type ").concat(oldType.name, ".") }); } for (var _i24 = 0, _possibleTypesDiff$re2 = possibleTypesDiff.removed; _i24 < _possibleTypesDiff$re2.length; _i24++) { var oldPossibleType = _possibleTypesDiff$re2[_i24]; schemaChanges.push({ type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, description: "".concat(oldPossibleType.name, " was removed from union type ").concat(oldType.name, ".") }); } return schemaChanges; } function findEnumTypeChanges(oldType, newType) { var schemaChanges = []; var valuesDiff = diff(oldType.getValues(), newType.getValues()); for (var _i26 = 0, _valuesDiff$added2 = valuesDiff.added; _i26 < _valuesDiff$added2.length; _i26++) { var newValue = _valuesDiff$added2[_i26]; schemaChanges.push({ type: DangerousChangeType.VALUE_ADDED_TO_ENUM, description: "".concat(newValue.name, " was added to enum type ").concat(oldType.name, ".") }); } for (var _i28 = 0, _valuesDiff$removed2 = valuesDiff.removed; _i28 < _valuesDiff$removed2.length; _i28++) { var oldValue = _valuesDiff$removed2[_i28]; schemaChanges.push({ type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, description: "".concat(oldValue.name, " was removed from enum type ").concat(oldType.name, ".") }); } return schemaChanges; } function findObjectTypeChanges(oldType, newType) { var schemaChanges = findFieldChanges(oldType, newType); var interfacesDiff = diff(oldType.getInterfaces(), newType.getInterfaces()); for (var _i30 = 0, _interfacesDiff$added2 = interfacesDiff.added; _i30 < _interfacesDiff$added2.length; _i30++) { var newInterface = _interfacesDiff$added2[_i30]; schemaChanges.push({ type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT, description: "".concat(newInterface.name, " added to interfaces implemented by ").concat(oldType.name, ".") }); } for (var _i32 = 0, _interfacesDiff$remov2 = interfacesDiff.removed; _i32 < _interfacesDiff$remov2.length; _i32++) { var oldInterface = _interfacesDiff$remov2[_i32]; schemaChanges.push({ type: BreakingChangeType.INTERFACE_REMOVED_FROM_OBJECT, description: "".concat(oldType.name, " no longer implements interface ").concat(oldInterface.name, ".") }); } return schemaChanges; } function findFieldChanges(oldType, newType) { var schemaChanges = []; var fieldsDiff = diff(objectValues(oldType.getFields()), objectValues(newType.getFields())); for (var _i34 = 0, _fieldsDiff$removed4 = fieldsDiff.removed; _i34 < _fieldsDiff$removed4.length; _i34++) { var oldField = _fieldsDiff$removed4[_i34]; schemaChanges.push({ type: BreakingChangeType.FIELD_REMOVED, description: "".concat(oldType.name, ".").concat(oldField.name, " was removed.") }); } for (var _i36 = 0, _fieldsDiff$persisted4 = fieldsDiff.persisted; _i36 < _fieldsDiff$persisted4.length; _i36++) { var _ref8 = _fieldsDiff$persisted4[_i36]; var _oldField2 = _ref8[0]; var newField = _ref8[1]; schemaChanges.push.apply(schemaChanges, findArgChanges(oldType, _oldField2, newField)); var isSafe = isChangeSafeForObjectOrInterfaceField(_oldField2.type, newField.type); if (!isSafe) { schemaChanges.push({ type: BreakingChangeType.FIELD_CHANGED_KIND, description: "".concat(oldType.name, ".").concat(_oldField2.name, " changed type from ") + "".concat(String(_oldField2.type), " to ").concat(String(newField.type), ".") }); } } return schemaChanges; } function findArgChanges(oldType, oldField, newField) { var schemaChanges = []; var argsDiff = diff(oldField.args, newField.args); for (var _i38 = 0, _argsDiff$removed4 = argsDiff.removed; _i38 < _argsDiff$removed4.length; _i38++) { var oldArg = _argsDiff$removed4[_i38]; schemaChanges.push({ type: BreakingChangeType.ARG_REMOVED, description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(oldArg.name, " was removed.") }); } for (var _i40 = 0, _argsDiff$persisted2 = argsDiff.persisted; _i40 < _argsDiff$persisted2.length; _i40++) { var _ref10 = _argsDiff$persisted2[_i40]; var _oldArg = _ref10[0]; var newArg = _ref10[1]; var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(_oldArg.type, newArg.type); if (!isSafe) { schemaChanges.push({ type: BreakingChangeType.ARG_CHANGED_KIND, description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(_oldArg.name, " has changed type from ") + "".concat(String(_oldArg.type), " to ").concat(String(newArg.type), ".") }); } else if (_oldArg.defaultValue !== undefined) { if (newArg.defaultValue === undefined) { schemaChanges.push({ type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(_oldArg.name, " defaultValue was removed.") }); } else { // Since we looking only for client's observable changes we should // compare default values in the same representation as they are // represented inside introspection. var oldValueStr = stringifyValue(_oldArg.defaultValue, _oldArg.type); var newValueStr = stringifyValue(newArg.defaultValue, newArg.type); if (oldValueStr !== newValueStr) { schemaChanges.push({ type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, description: "".concat(oldType.name, ".").concat(oldField.name, " arg ").concat(_oldArg.name, " has changed defaultValue from ").concat(oldValueStr, " to ").concat(newValueStr, ".") }); } } } } for (var _i42 = 0, _argsDiff$added4 = argsDiff.added; _i42 < _argsDiff$added4.length; _i42++) { var _newArg = _argsDiff$added4[_i42]; if (isRequiredArgument(_newArg)) { schemaChanges.push({ type: BreakingChangeType.REQUIRED_ARG_ADDED, description: "A required arg ".concat(_newArg.name, " on ").concat(oldType.name, ".").concat(oldField.name, " was added.") }); } else { schemaChanges.push({ type: DangerousChangeType.OPTIONAL_ARG_ADDED, description: "An optional arg ".concat(_newArg.name, " on ").concat(oldType.name, ".").concat(oldField.name, " was added.") }); } } return schemaChanges; } function isChangeSafeForObjectOrInterfaceField(oldType, newType) { if (isListType(oldType)) { return (// if they're both lists, make sure the underlying types are compatible isListType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType) || // moving from nullable to non-null of the same underlying type is safe isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType) ); } if (isNonNullType(oldType)) { // if they're both non-null, make sure the underlying types are compatible return isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType); } return (// if they're both named types, see if their names are equivalent isNamedType(newType) && oldType.name === newType.name || // moving from nullable to non-null of the same underlying type is safe isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType) ); } function isChangeSafeForInputObjectFieldOrFieldArg(oldType, newType) { if (isListType(oldType)) { // if they're both lists, make sure the underlying types are compatible return isListType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType); } if (isNonNullType(oldType)) { return (// if they're both non-null, make sure the underlying types are // compatible isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType) || // moving from non-null to nullable of the same underlying type is safe !isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType) ); } // if they're both named types, see if their names are equivalent return isNamedType(newType) && oldType.name === newType.name; } function typeKindName(type) { if (isScalarType(type)) { return 'a Scalar type'; } if (isObjectType(type)) { return 'an Object type'; } if (isInterfaceType(type)) { return 'an Interface type'; } if (isUnionType(type)) { return 'a Union type'; } if (isEnumType(type)) { return 'an Enum type'; } /* istanbul ignore else */ if (isInputObjectType(type)) { return 'an Input type'; } // Not reachable. All possible named types have been considered. /* istanbul ignore next */ invariant(false, 'Unexpected type: ' + inspect(type)); } function stringifyValue(value, type) { var ast = astFromValue(value, type); /* istanbul ignore next */ ast != null || invariant(0); var sortedAST = visit(ast, { ObjectValue: function ObjectValue(objectNode) { var fields = [].concat(objectNode.fields).sort(function (fieldA, fieldB) { return fieldA.name.value.localeCompare(fieldB.name.value); }); return _objectSpread({}, objectNode, { fields: fields }); } }); return print(sortedAST); } function diff(oldArray, newArray) { var added = []; var removed = []; var persisted = []; var oldMap = keyMap(oldArray, function (_ref11) { var name = _ref11.name; return name; }); var newMap = keyMap(newArray, function (_ref12) { var name = _ref12.name; return name; }); for (var _i44 = 0; _i44 < oldArray.length; _i44++) { var oldItem = oldArray[_i44]; var newItem = newMap[oldItem.name]; if (newItem === undefined) { removed.push(oldItem); } else { persisted.push([oldItem, newItem]); } } for (var _i46 = 0; _i46 < newArray.length; _i46++) { var _newItem = newArray[_i46]; if (oldMap[_newItem.name] === undefined) { added.push(_newItem); } } return { added: added, persisted: persisted, removed: removed }; }