node_modules.graphql.utilities.findBreakingChanges.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-client-maven-plugin Show documentation
Show all versions of apollo-client-maven-plugin Show documentation
Maven plugin for generating graphql clients
The newest version!
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DangerousChangeType = exports.BreakingChangeType = undefined;
exports.findBreakingChanges = findBreakingChanges;
exports.findDangerousChanges = findDangerousChanges;
exports.findRemovedTypes = findRemovedTypes;
exports.findTypesThatChangedKind = findTypesThatChangedKind;
exports.findArgChanges = findArgChanges;
exports.findFieldsThatChangedType = findFieldsThatChangedType;
exports.findFieldsThatChangedTypeOnInputObjectTypes = findFieldsThatChangedTypeOnInputObjectTypes;
exports.findTypesRemovedFromUnions = findTypesRemovedFromUnions;
exports.findValuesRemovedFromEnums = findValuesRemovedFromEnums;
exports.findInterfacesRemovedFromObjectTypes = findInterfacesRemovedFromObjectTypes;
var _definition = require('../type/definition');
var _schema = require('../type/schema');
/**
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
var BreakingChangeType = exports.BreakingChangeType = {
FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND',
FIELD_REMOVED: 'FIELD_REMOVED',
TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND',
TYPE_REMOVED: 'TYPE_REMOVED',
TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION',
VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM',
ARG_REMOVED: 'ARG_REMOVED',
ARG_CHANGED_KIND: 'ARG_CHANGED_KIND',
NON_NULL_ARG_ADDED: 'NON_NULL_ARG_ADDED',
NON_NULL_INPUT_FIELD_ADDED: 'NON_NULL_INPUT_FIELD_ADDED',
INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT'
};
var DangerousChangeType = exports.DangerousChangeType = {
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.
*/
function findBreakingChanges(oldSchema, newSchema) {
return [].concat(findRemovedTypes(oldSchema, newSchema), findTypesThatChangedKind(oldSchema, newSchema), findFieldsThatChangedType(oldSchema, newSchema), findTypesRemovedFromUnions(oldSchema, newSchema), findValuesRemovedFromEnums(oldSchema, newSchema), findArgChanges(oldSchema, newSchema).breakingChanges, findInterfacesRemovedFromObjectTypes(oldSchema, newSchema));
}
/**
* Given two schemas, returns an Array containing descriptions of all the types
* of potentially dangerous changes covered by the other functions down below.
*/
function findDangerousChanges(oldSchema, newSchema) {
return [].concat(findArgChanges(oldSchema, newSchema).dangerousChanges);
}
/**
* Given two schemas, returns an Array containing descriptions of any breaking
* changes in the newSchema related to removing an entire type.
*/
function findRemovedTypes(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var breakingChanges = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
if (!newTypeMap[typeName]) {
breakingChanges.push({
type: BreakingChangeType.TYPE_REMOVED,
description: typeName + ' was removed.'
});
}
});
return breakingChanges;
}
/**
* Given two schemas, returns an Array containing descriptions of any breaking
* changes in the newSchema related to changing the type of a type.
*/
function findTypesThatChangedKind(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var breakingChanges = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
if (!newTypeMap[typeName]) {
return;
}
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof newType.constructor)) {
breakingChanges.push({
type: BreakingChangeType.TYPE_CHANGED_KIND,
description: typeName + ' changed from ' + (typeKindName(oldType) + ' to ' + typeKindName(newType) + '.')
});
}
});
return breakingChanges;
}
/**
* Given two schemas, returns an Array containing descriptions of any
* breaking or dangerous changes in the newSchema related to arguments
* (such as removal or change of type of an argument, or a change in an
* argument's default value).
*/
function findArgChanges(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var breakingChanges = [];
var dangerousChanges = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof _definition.GraphQLObjectType || oldType instanceof _definition.GraphQLInterfaceType) || !(newType instanceof oldType.constructor)) {
return;
}
var oldTypeFields = oldType.getFields();
var newTypeFields = newType.getFields();
Object.keys(oldTypeFields).forEach(function (fieldName) {
if (!newTypeFields[fieldName]) {
return;
}
oldTypeFields[fieldName].args.forEach(function (oldArgDef) {
var newArgs = newTypeFields[fieldName].args;
var newArgDef = newArgs.find(function (arg) {
return arg.name === oldArgDef.name;
});
// Arg not present
if (!newArgDef) {
breakingChanges.push({
type: BreakingChangeType.ARG_REMOVED,
description: oldType.name + '.' + fieldName + ' arg ' + (oldArgDef.name + ' was removed')
});
} else {
var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldArgDef.type, newArgDef.type);
if (!isSafe) {
breakingChanges.push({
type: BreakingChangeType.ARG_CHANGED_KIND,
description: oldType.name + '.' + fieldName + ' arg ' + (oldArgDef.name + ' has changed type from ') + (oldArgDef.type.toString() + ' to ' + newArgDef.type.toString())
});
} else if (oldArgDef.defaultValue !== undefined && oldArgDef.defaultValue !== newArgDef.defaultValue) {
dangerousChanges.push({
type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE,
description: oldType.name + '.' + fieldName + ' arg ' + (oldArgDef.name + ' has changed defaultValue')
});
}
}
});
// Check if a non-null arg was added to the field
newTypeFields[fieldName].args.forEach(function (newArgDef) {
var oldArgs = oldTypeFields[fieldName].args;
var oldArgDef = oldArgs.find(function (arg) {
return arg.name === newArgDef.name;
});
if (!oldArgDef && newArgDef.type instanceof _definition.GraphQLNonNull) {
breakingChanges.push({
type: BreakingChangeType.NON_NULL_ARG_ADDED,
description: 'A non-null arg ' + newArgDef.name + ' on ' + (newType.name + '.' + fieldName + ' was added')
});
}
});
});
});
return {
breakingChanges: breakingChanges,
dangerousChanges: dangerousChanges
};
}
function typeKindName(type) {
if (type instanceof _definition.GraphQLScalarType) {
return 'a Scalar type';
}
if (type instanceof _definition.GraphQLObjectType) {
return 'an Object type';
}
if (type instanceof _definition.GraphQLInterfaceType) {
return 'an Interface type';
}
if (type instanceof _definition.GraphQLUnionType) {
return 'a Union type';
}
if (type instanceof _definition.GraphQLEnumType) {
return 'an Enum type';
}
if (type instanceof _definition.GraphQLInputObjectType) {
return 'an Input type';
}
throw new TypeError('Unknown type ' + type.constructor.name);
}
/**
* Given two schemas, returns an Array containing descriptions of any breaking
* changes in the newSchema related to the fields on a type. This includes if
* a field has been removed from a type, if a field has changed type, or if
* a non-null field is added to an input type.
*/
function findFieldsThatChangedType(oldSchema, newSchema) {
return [].concat(findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema), findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema));
}
function findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var breakingFieldChanges = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof _definition.GraphQLObjectType || oldType instanceof _definition.GraphQLInterfaceType) || !(newType instanceof oldType.constructor)) {
return;
}
var oldTypeFieldsDef = oldType.getFields();
var newTypeFieldsDef = newType.getFields();
Object.keys(oldTypeFieldsDef).forEach(function (fieldName) {
// Check if the field is missing on the type in the new schema.
if (!(fieldName in newTypeFieldsDef)) {
breakingFieldChanges.push({
type: BreakingChangeType.FIELD_REMOVED,
description: typeName + '.' + fieldName + ' was removed.'
});
} else {
var oldFieldType = oldTypeFieldsDef[fieldName].type;
var newFieldType = newTypeFieldsDef[fieldName].type;
var isSafe = isChangeSafeForObjectOrInterfaceField(oldFieldType, newFieldType);
if (!isSafe) {
var oldFieldTypeString = (0, _definition.isNamedType)(oldFieldType) ? oldFieldType.name : oldFieldType.toString();
var newFieldTypeString = (0, _definition.isNamedType)(newFieldType) ? newFieldType.name : newFieldType.toString();
breakingFieldChanges.push({
type: BreakingChangeType.FIELD_CHANGED_KIND,
description: typeName + '.' + fieldName + ' changed type from ' + (oldFieldTypeString + ' to ' + newFieldTypeString + '.')
});
}
}
});
});
return breakingFieldChanges;
}
function findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var breakingFieldChanges = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof _definition.GraphQLInputObjectType) || !(newType instanceof _definition.GraphQLInputObjectType)) {
return;
}
var oldTypeFieldsDef = oldType.getFields();
var newTypeFieldsDef = newType.getFields();
Object.keys(oldTypeFieldsDef).forEach(function (fieldName) {
// Check if the field is missing on the type in the new schema.
if (!(fieldName in newTypeFieldsDef)) {
breakingFieldChanges.push({
type: BreakingChangeType.FIELD_REMOVED,
description: typeName + '.' + fieldName + ' was removed.'
});
} else {
var oldFieldType = oldTypeFieldsDef[fieldName].type;
var newFieldType = newTypeFieldsDef[fieldName].type;
var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldFieldType, newFieldType);
if (!isSafe) {
var oldFieldTypeString = (0, _definition.isNamedType)(oldFieldType) ? oldFieldType.name : oldFieldType.toString();
var newFieldTypeString = (0, _definition.isNamedType)(newFieldType) ? newFieldType.name : newFieldType.toString();
breakingFieldChanges.push({
type: BreakingChangeType.FIELD_CHANGED_KIND,
description: typeName + '.' + fieldName + ' changed type from ' + (oldFieldTypeString + ' to ' + newFieldTypeString + '.')
});
}
}
});
// Check if a non-null field was added to the input object type
Object.keys(newTypeFieldsDef).forEach(function (fieldName) {
if (!(fieldName in oldTypeFieldsDef) && newTypeFieldsDef[fieldName].type instanceof _definition.GraphQLNonNull) {
breakingFieldChanges.push({
type: BreakingChangeType.NON_NULL_INPUT_FIELD_ADDED,
description: 'A non-null field ' + fieldName + ' on ' + ('input type ' + newType.name + ' was added.')
});
}
});
});
return breakingFieldChanges;
}
function isChangeSafeForObjectOrInterfaceField(oldType, newType) {
if ((0, _definition.isNamedType)(oldType)) {
return (
// if they're both named types, see if their names are equivalent
(0, _definition.isNamedType)(newType) && oldType.name === newType.name ||
// moving from nullable to non-null of the same underlying type is safe
newType instanceof _definition.GraphQLNonNull && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)
);
} else if (oldType instanceof _definition.GraphQLList) {
return (
// if they're both lists, make sure the underlying types are compatible
newType instanceof _definition.GraphQLList && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType) ||
// moving from nullable to non-null of the same underlying type is safe
newType instanceof _definition.GraphQLNonNull && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)
);
} else if (oldType instanceof _definition.GraphQLNonNull) {
// if they're both non-null, make sure the underlying types are compatible
return newType instanceof _definition.GraphQLNonNull && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType);
}
return false;
}
function isChangeSafeForInputObjectFieldOrFieldArg(oldType, newType) {
if ((0, _definition.isNamedType)(oldType)) {
// if they're both named types, see if their names are equivalent
return (0, _definition.isNamedType)(newType) && oldType.name === newType.name;
} else if (oldType instanceof _definition.GraphQLList) {
// if they're both lists, make sure the underlying types are compatible
return newType instanceof _definition.GraphQLList && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType);
} else if (oldType instanceof _definition.GraphQLNonNull) {
return (
// if they're both non-null, make sure the underlying types are
// compatible
newType instanceof _definition.GraphQLNonNull && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType) ||
// moving from non-null to nullable of the same underlying type is safe
!(newType instanceof _definition.GraphQLNonNull) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType)
);
}
return false;
}
/**
* Given two schemas, returns an Array containing descriptions of any breaking
* changes in the newSchema related to removing types from a union type.
*/
function findTypesRemovedFromUnions(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var typesRemovedFromUnion = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof _definition.GraphQLUnionType) || !(newType instanceof _definition.GraphQLUnionType)) {
return;
}
var typeNamesInNewUnion = Object.create(null);
newType.getTypes().forEach(function (type) {
typeNamesInNewUnion[type.name] = true;
});
oldType.getTypes().forEach(function (type) {
if (!typeNamesInNewUnion[type.name]) {
typesRemovedFromUnion.push({
type: BreakingChangeType.TYPE_REMOVED_FROM_UNION,
description: type.name + ' was removed from union type ' + typeName + '.'
});
}
});
});
return typesRemovedFromUnion;
}
/**
* Given two schemas, returns an Array containing descriptions of any breaking
* changes in the newSchema related to removing values from an enum type.
*/
function findValuesRemovedFromEnums(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var valuesRemovedFromEnums = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof _definition.GraphQLEnumType) || !(newType instanceof _definition.GraphQLEnumType)) {
return;
}
var valuesInNewEnum = Object.create(null);
newType.getValues().forEach(function (value) {
valuesInNewEnum[value.name] = true;
});
oldType.getValues().forEach(function (value) {
if (!valuesInNewEnum[value.name]) {
valuesRemovedFromEnums.push({
type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM,
description: value.name + ' was removed from enum type ' + typeName + '.'
});
}
});
});
return valuesRemovedFromEnums;
}
function findInterfacesRemovedFromObjectTypes(oldSchema, newSchema) {
var oldTypeMap = oldSchema.getTypeMap();
var newTypeMap = newSchema.getTypeMap();
var breakingChanges = [];
Object.keys(oldTypeMap).forEach(function (typeName) {
var oldType = oldTypeMap[typeName];
var newType = newTypeMap[typeName];
if (!(oldType instanceof _definition.GraphQLObjectType) || !(newType instanceof _definition.GraphQLObjectType)) {
return;
}
var oldInterfaces = oldType.getInterfaces();
var newInterfaces = newType.getInterfaces();
oldInterfaces.forEach(function (oldInterface) {
if (!newInterfaces.some(function (int) {
return int.name === oldInterface.name;
})) {
breakingChanges.push({
type: BreakingChangeType.INTERFACE_REMOVED_FROM_OBJECT,
description: typeName + ' no longer implements interface ' + (oldInterface.name + '.')
});
}
});
});
return breakingChanges;
}