Added test for useful error when returning invalid value from resolveType

This commit is contained in:
Vladimir Razuvaev 2018-08-07 23:25:01 +07:00
parent f4008f0fb2
commit 4227404aee
2 changed files with 61 additions and 5 deletions

View File

@ -1400,15 +1400,16 @@ class Executor
if (! $runtimeType instanceof ObjectType) { if (! $runtimeType instanceof ObjectType) {
throw new InvariantViolation( throw new InvariantViolation(
sprintf( sprintf(
'Abstract type %1$s must resolve to an Object type at ' . 'Abstract type %s must resolve to an Object type at ' .
'runtime for field %s.%s with value "%s", received "%s".' . 'runtime for field %s.%s with value "%s", received "%s". ' .
'Either the %1$s type should provide a "resolveType" ' . 'Either the %s type should provide a "resolveType" ' .
'function or each possible types should provide an "isTypeOf" function.', 'function or each possible type should provide an "isTypeOf" function.',
$returnType, $returnType,
$info->parentType, $info->parentType,
$info->fieldName, $info->fieldName,
Utils::printSafe($result), Utils::printSafe($result),
Utils::printSafe($runtimeType) Utils::printSafe($runtimeType),
$returnType
) )
); );
} }

View File

@ -357,6 +357,61 @@ class AbstractTest extends TestCase
$this->assertArraySubset($expected, $result); $this->assertArraySubset($expected, $result);
} }
/**
* @it returning invalid value from resolveType yields useful error
*/
public function testReturningInvalidValueFromResolveTypeYieldsUsefulError()
{
$fooInterface = new InterfaceType([
'name' => 'FooInterface',
'fields' => ['bar' => ['type' => Type::string()]],
'resolveType' => function () {
return [];
},
]);
$fooObject = new ObjectType([
'name' => 'FooObject',
'fields' => ['bar' => ['type' => Type::string()]],
'interfaces' => [$fooInterface],
]);
$schema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'foo' => [
'type' => $fooInterface,
'resolve' => function () {
return 'dummy';
},
],
],
]),
'types' => [$fooObject],
]);
$result = GraphQL::executeQuery($schema, '{ foo { bar } }');
$expected = [
'data' => ['foo' => null],
'errors' => [
[
'message' => 'Internal server error',
'debugMessage' =>
'Abstract type FooInterface must resolve to an Object type at ' .
'runtime for field Query.foo with value "dummy", received "[]". ' .
'Either the FooInterface type should provide a "resolveType" ' .
'function or each possible type should provide an "isTypeOf" function.',
'locations' => [['line' => 1, 'column' => 3]],
'path' => ['foo'],
'category' => 'internal',
],
],
];
$this->assertEquals($expected, $result->toArray(true));
}
/** /**
* @it resolveType allows resolving with type name * @it resolveType allows resolving with type name
*/ */