mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 07:49:24 +03:00
resolveType for interface/unions is now allowed to return type name vs instance
This commit is contained in:
parent
89eb6dede9
commit
3e2d9459aa
@ -734,6 +734,11 @@ class Executor
|
|||||||
call_user_func($resolveType, $result, $exeContext->contextValue, $info) :
|
call_user_func($resolveType, $result, $exeContext->contextValue, $info) :
|
||||||
Type::getTypeOf($result, $exeContext->contextValue, $info, $returnType);
|
Type::getTypeOf($result, $exeContext->contextValue, $info, $returnType);
|
||||||
|
|
||||||
|
// If resolveType returns a string, we assume it's a ObjectType name.
|
||||||
|
if (is_string($runtimeType)) {
|
||||||
|
$runtimeType = $exeContext->schema->getType($runtimeType);
|
||||||
|
}
|
||||||
|
|
||||||
if (!($runtimeType instanceof ObjectType)) {
|
if (!($runtimeType instanceof ObjectType)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Abstract type {$returnType} must resolve to an Object type at runtime " .
|
"Abstract type {$returnType} must resolve to an Object type at runtime " .
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Tests\Executor;
|
namespace GraphQL\Tests\Executor;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/TestClasses.php';
|
||||||
|
|
||||||
use GraphQL\Executor\ExecutionResult;
|
use GraphQL\Executor\ExecutionResult;
|
||||||
use GraphQL\Executor\Executor;
|
use GraphQL\Executor\Executor;
|
||||||
use GraphQL\FormattedError;
|
use GraphQL\FormattedError;
|
||||||
@ -13,8 +15,6 @@ use GraphQL\Type\Definition\ObjectType;
|
|||||||
use GraphQL\Type\Definition\Type;
|
use GraphQL\Type\Definition\Type;
|
||||||
use GraphQL\Type\Definition\UnionType;
|
use GraphQL\Type\Definition\UnionType;
|
||||||
|
|
||||||
spl_autoload_call('GraphQL\Tests\Executor\TestClasses');
|
|
||||||
|
|
||||||
class AbstractTest extends \PHPUnit_Framework_TestCase
|
class AbstractTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
// Execute: Handles execution of abstract types
|
// Execute: Handles execution of abstract types
|
||||||
@ -358,4 +358,81 @@ class AbstractTest extends \PHPUnit_Framework_TestCase
|
|||||||
];
|
];
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it resolveType allows resolving with type name
|
||||||
|
*/
|
||||||
|
public function testResolveTypeAllowsResolvingWithTypeName()
|
||||||
|
{
|
||||||
|
$PetType = new InterfaceType([
|
||||||
|
'name' => 'Pet',
|
||||||
|
'resolveType' => function($obj) {
|
||||||
|
if ($obj instanceof Dog) return 'Dog';
|
||||||
|
if ($obj instanceof Cat) return 'Cat';
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
'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)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]),
|
||||||
|
'types' => [ $CatType, $DogType ]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$query = '{
|
||||||
|
pets {
|
||||||
|
name
|
||||||
|
... on Dog {
|
||||||
|
woofs
|
||||||
|
}
|
||||||
|
... on Cat {
|
||||||
|
meows
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}';
|
||||||
|
|
||||||
|
$result = GraphQL::execute($schema, $query);
|
||||||
|
|
||||||
|
$this->assertEquals([
|
||||||
|
'data' => [
|
||||||
|
'pets' => [
|
||||||
|
['name' => 'Odie', 'woofs' => true],
|
||||||
|
['name' => 'Garfield', 'meows' => false]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
], $result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user