couple more functions

This commit is contained in:
Ben Roberts 2017-11-16 14:15:39 -05:00
parent 3811181f49
commit e649ef307a

View File

@ -96,15 +96,6 @@ class FindBreakingChanges
} }
} }
/*Object.keys(oldTypeMap).forEach(typeName => {
if (!newTypeMap[typeName]) {
breakingChanges.push({
type: BreakingChangeType.TYPE_REMOVED,
description: `${typeName} was removed.`,
});
}
});*/
return $breakingChanges; return $breakingChanges;
} }
@ -135,22 +126,6 @@ class FindBreakingChanges
} }
} }
/*
Object.keys(oldTypeMap).forEach(typeName => {
if (!newTypeMap[typeName]) {
return;
}
const oldType = oldTypeMap[typeName];
const 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; return $breakingChanges;
} }
@ -231,82 +206,6 @@ class FindBreakingChanges
} }
return ['breakingChanges' => $breakingChanges, 'dangerousChanges' => $dangerousChanges]; return ['breakingChanges' => $breakingChanges, 'dangerousChanges' => $dangerousChanges];
/*
Object.keys(oldTypeMap).forEach(typeName => {
const oldType = oldTypeMap[typeName];
const newType = newTypeMap[typeName];
if (
!(oldType instanceof GraphQLObjectType ||
oldType instanceof GraphQLInterfaceType) ||
!(newType instanceof oldType.constructor)
) {
return;
}
const oldTypeFields: GraphQLFieldMap<*, *> = oldType.getFields();
const newTypeFields: GraphQLFieldMap<*, *> = newType.getFields();
Object.keys(oldTypeFields).forEach(fieldName => {
if (!newTypeFields[fieldName]) {
return;
}
oldTypeFields[fieldName].args.forEach(oldArgDef => {
const newArgs = newTypeFields[fieldName].args;
const newArgDef = newArgs.find(
arg => 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 {
const 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(newArgDef => {
const oldArgs = oldTypeFields[fieldName].args;
const oldArgDef = oldArgs.find(
arg => arg.name === newArgDef.name
);
if (!oldArgDef && newArgDef.type instanceof GraphQLNonNull) {
breakingChanges.push({
type: BreakingChangeType.NON_NULL_ARG_ADDED,
description: `A non-null arg ${newArgDef.name} on ` +
`${newType.name}.${fieldName} was added`,
});
}
});
});
});
return {
breakingChanges,
dangerousChanges,
};*/
} }
/** /**
@ -319,7 +218,7 @@ class FindBreakingChanges
{ {
if ($type instanceof ScalarType) { if ($type instanceof ScalarType) {
return 'a Scalar type'; return 'a Scalar type';
} elseif($type instanceof ObjectType) { } elseif ($type instanceof ObjectType) {
return 'an Object type'; return 'an Object type';
} elseif ($type instanceof InterfaceType) { } elseif ($type instanceof InterfaceType) {
return 'an Interface type'; return 'an Interface type';
@ -344,12 +243,18 @@ class FindBreakingChanges
Schema $oldSchema, Schema $newSchema Schema $oldSchema, Schema $newSchema
) )
{ {
/*return [ return array_merge(
...findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema), self::findFieldsThatChangedTypeOnObjectOrInterfaceTypes($oldSchema, $newSchema),
...findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema), self::findFieldsThatChangedTypeOnInputObjectTypes($oldSchema, $newSchema)
];*/ );
} }
/**
* @param $oldSchema
* @param $newSchema
*
* @return array
*/
private static function findFieldsThatChangedTypeOnObjectOrInterfaceTypes( private static function findFieldsThatChangedTypeOnObjectOrInterfaceTypes(
$oldSchema, $newSchema $oldSchema, $newSchema
) )
@ -402,6 +307,12 @@ class FindBreakingChanges
return breakingFieldChanges;*/ return breakingFieldChanges;*/
} }
/**
* @param Schema $oldSchema
* @param Schema $newSchema
*
* @return array
*/
public static function findFieldsThatChangedTypeOnInputObjectTypes( public static function findFieldsThatChangedTypeOnInputObjectTypes(
Schema $oldSchema, Schema $newSchema Schema $oldSchema, Schema $newSchema
) )
@ -493,81 +404,30 @@ class FindBreakingChanges
} }
return false; return false;
/*if (isNamedType(oldType)) {
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
newType instanceof GraphQLNonNull &&
isChangeSafeForObjectOrInterfaceField(
oldType,
newType.ofType,
)
);
} else if (oldType instanceof GraphQLList) {
return (
// if they're both lists, make sure the underlying types are compatible
newType instanceof GraphQLList &&
isChangeSafeForObjectOrInterfaceField(
oldType.ofType,
newType.ofType,
)
) ||
(
// moving from nullable to non-null of the same underlying type is safe
newType instanceof GraphQLNonNull &&
isChangeSafeForObjectOrInterfaceField(
oldType,
newType.ofType,
)
);
} else if (oldType instanceof GraphQLNonNull) {
// if they're both non-null, make sure the underlying types are compatible
return newType instanceof GraphQLNonNull &&
isChangeSafeForObjectOrInterfaceField(
oldType.ofType,
newType.ofType,
);
}
return false;*/
} }
/**
* @param Type $oldType
* @param Schema $newSchema
*
* @return bool
*/
private static function isChangeSafeForInputObjectFieldOrFieldArg( private static function isChangeSafeForInputObjectFieldOrFieldArg(
Schema $oldSchema, Schema $newSchema Type $oldType, Type $newType
) )
{ {
/* if (isNamedType(oldType)) { if (self::isNamedType($oldType)) {
// if they're both named types, see if their names are equivalent return self::isNamedType($newType) && $oldType->name === $newType->name;
return isNamedType(newType) && oldType.name === newType.name; } elseif ($oldType instanceof ListOfType) {
} else if (oldType instanceof GraphQLList) { return $newType instanceof ListOfType && self::isChangeSafeForInputObjectFieldOrFieldArg($oldType->getWrappedType(), $newType->getWrappedType());
// if they're both lists, make sure the underlying types are compatible } elseif ($oldType instanceof NonNull) {
return newType instanceof GraphQLList && return (
isChangeSafeForInputObjectFieldOrFieldArg( $newType instanceof NonNull && self::isChangeSafeForInputObjectFieldOrFieldArg($oldType->getWrappedType(), $newType->getWrappedType())
oldType.ofType, ) || (
newType.ofType, !($newType instanceof NonNull) && self::isChangeSafeForInputObjectFieldOrFieldArg($oldType->getWrappedType(), $newType)
); );
} else if (oldType instanceof GraphQLNonNull) { }
return ( return false;
// if they're both non-null, make sure the underlying types are
// compatible
newType instanceof GraphQLNonNull &&
isChangeSafeForInputObjectFieldOrFieldArg(
oldType.ofType,
newType.ofType,
)
) ||
(
// moving from non-null to nullable of the same underlying type is safe
!(newType instanceof GraphQLNonNull) &&
isChangeSafeForInputObjectFieldOrFieldArg(
oldType.ofType,
newType,
)
);
}
return false;*/
} }
/** /**
@ -578,31 +438,27 @@ class FindBreakingChanges
Schema $oldSchema, Schema $newSchema Schema $oldSchema, Schema $newSchema
) )
{ {
/* const oldTypeMap = oldSchema.getTypeMap(); $oldTypeMap = $oldSchema->getTypeMap();
const newTypeMap = newSchema.getTypeMap(); $newTypeMap = $newSchema->getTypeMap();
const typesRemovedFromUnion = []; $typesRemovedFromUnion = [];
Object.keys(oldTypeMap).forEach(typeName => { foreach ($oldTypeMap as $typeName => $oldType) {
const oldType = oldTypeMap[typeName]; $newType = isset($newTypeMap[$typeName]) ? $newTypeMap[$typeName] : null;
const newType = newTypeMap[typeName]; if (!($oldType instanceof UnionType) || !($newType instanceof UnionType)) {
if (!(oldType instanceof GraphQLUnionType) || continue;
!(newType instanceof GraphQLUnionType)) {
return;
} }
const typeNamesInNewUnion = Object.create(null); $typeNamesInNewUnion = [];
newType.getTypes().forEach(type => { foreach ($newType->getTypes() as $type) {
typeNamesInNewUnion[type.name] = true; $typeNamesInNewUnion[$type->name] = true;
}); }
oldType.getTypes().forEach(type => { foreach ($oldType->getTypes() as $type) {
if (!typeNamesInNewUnion[type.name]) { if (!isset($typeNamesInNewUnion[$type->name])) {
typesRemovedFromUnion.push({ $missingTypeName = $type->name;
type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, $typesRemovedFromUnion[] = ['type' => self::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION, 'description' => "${missingTypeName} was removed from union type ${typeName}"];
description: `${type.name} was removed from union type ${typeName}.` }
}); }
} }
}); return $typesRemovedFromUnion;
});
return typesRemovedFromUnion;*/
} }
/** /**