Merge pull request #431 from simPod/fix-bc-finder

Invert instance of in BCFinder
This commit is contained in:
Vladimir Razuvaev 2019-02-19 10:53:03 +07:00 committed by GitHub
commit 616fc10837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 13 deletions

View File

@ -112,27 +112,31 @@ class BreakingChangesFinder
* @return string[][]
*/
public static function findTypesThatChangedKind(
Schema $oldSchema,
Schema $newSchema
) {
$oldTypeMap = $oldSchema->getTypeMap();
$newTypeMap = $newSchema->getTypeMap();
Schema $schemaA,
Schema $schemaB
) : iterable {
$schemaATypeMap = $schemaA->getTypeMap();
$schemaBTypeMap = $schemaB->getTypeMap();
$breakingChanges = [];
foreach ($oldTypeMap as $typeName => $oldType) {
if (! isset($newTypeMap[$typeName])) {
foreach ($schemaATypeMap as $typeName => $schemaAType) {
if (! isset($schemaBTypeMap[$typeName])) {
continue;
}
$newType = $newTypeMap[$typeName];
if ($oldType instanceof $newType) {
$schemaBType = $schemaBTypeMap[$typeName];
if ($schemaAType instanceof $schemaBType) {
continue;
}
$oldTypeKindName = self::typeKindName($oldType);
$newTypeKindName = self::typeKindName($newType);
$breakingChanges[] = [
if ($schemaBType instanceof $schemaAType) {
continue;
}
$schemaATypeKindName = self::typeKindName($schemaAType);
$schemaBTypeKindName = self::typeKindName($schemaBType);
$breakingChanges[] = [
'type' => self::BREAKING_CHANGE_TYPE_CHANGED_KIND,
'description' => "${typeName} changed from ${oldTypeKindName} to ${newTypeKindName}.",
'description' => "${typeName} changed from ${schemaATypeKindName} to ${schemaBTypeKindName}.",
];
}

View File

@ -125,6 +125,47 @@ class BreakingChangesFinderTest extends TestCase
);
}
/**
* We need to compare type of class A (old type) and type of class B (new type)
* Class B extends A but are evaluated as same types (if all properties match).
* The reason is that when constructing schema from remote schema,
* we have no certain way to get information about our classes.
* Thus object types from remote schema are constructed as Object Type
* while their local counterparts are usually a subclass of Object Type.
*
* @see https://github.com/webonyx/graphql-php/pull/431
*/
public function testShouldNotMarkTypesWithInheritedClassesAsChanged() : void
{
$objectTypeConstructedFromRemoteSchema = new ObjectType([
'name' => 'ObjectType',
'fields' => [
'field1' => ['type' => Type::string()],
],
]);
$localObjectType = new class([
'name' => 'ObjectType',
'fields' => [
'field1' => ['type' => Type::string()],
],
]) extends ObjectType{
};
$schemaA = new Schema([
'query' => $this->queryType,
'types' => [$objectTypeConstructedFromRemoteSchema],
]);
$schemaB = new Schema([
'query' => $this->queryType,
'types' => [$localObjectType],
]);
self::assertEmpty(BreakingChangesFinder::findTypesThatChangedKind($schemaA, $schemaB));
self::assertEmpty(BreakingChangesFinder::findTypesThatChangedKind($schemaB, $schemaA));
}
/**
* @see it('should detect if a field on a type was deleted or changed type')
*/