function testResolveTypeOnInterfaceYieldsUsefulError()
{
$DogType = null;
$CatType = null;
$HumanType = null;
$PetType = new InterfaceType(['name' => 'Pet', 'resolveType' => function ($obj) use(&$DogType, &$CatType, &$HumanType) {
if ($obj instanceof Dog) {
return $DogType;
}
if ($obj instanceof Cat) {
return $CatType;
}
if ($obj instanceof Human) {
return $HumanType;
}
return null;
}, 'fields' => ['name' => ['type' => Type::string()]]]);
$HumanType = new ObjectType(['name' => 'Human', 'fields' => ['name' => ['type' => Type::string()]]]);
$DogType = new ObjectType(['name' => 'Dog', 'interfaces' => [$PetType], 'fields' => ['name' => ['type' => Type::string()], 'woofs' => ['type' => Type::boolean()]]]);
$CatType = new ObjectType(['name' => 'Cat', 'interfaces' => [$PetType], 'fields' => ['name' => ['type' => Type::string()], 'meows' => ['type' => Type::boolean()]]]);
$schema = new Schema(['query' => new ObjectType(['name' => 'Query', 'fields' => ['pets' => ['type' => Type::listOf($PetType), 'resolve' => function () {
return [new Dog('Odie', true), new Cat('Garfield', false), new Human('Jon')];
}]]]), 'types' => [$DogType, $CatType]]);
$query = '{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}';
$expected = ['data' => ['pets' => [['name' => 'Odie', 'woofs' => true], ['name' => 'Garfield', 'meows' => false], null]], 'errors' => [['message' => 'Runtime Object type "Human" is not a possible type for "Pet".', 'locations' => [['line' => 2, 'column' => 11]], 'path' => ['pets', 2]]]];
$actual = GraphQL::execute($schema, $query);
$this->assertEquals($expected, $actual);
}