2017-07-03 19:08:20 +03:00
|
|
|
<?php
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-07-03 19:08:20 +03:00
|
|
|
namespace GraphQL\Tests\Executor;
|
|
|
|
|
|
|
|
use GraphQL\Deferred;
|
2017-07-18 16:57:30 +03:00
|
|
|
use GraphQL\Error\UserError;
|
2017-07-03 19:08:20 +03:00
|
|
|
use GraphQL\GraphQL;
|
2018-09-01 18:07:06 +03:00
|
|
|
use GraphQL\Tests\Executor\TestClasses\Cat;
|
|
|
|
use GraphQL\Tests\Executor\TestClasses\Dog;
|
|
|
|
use GraphQL\Tests\Executor\TestClasses\Human;
|
2017-07-03 19:08:20 +03:00
|
|
|
use GraphQL\Type\Definition\InterfaceType;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Type\Definition\UnionType;
|
2018-09-01 18:07:06 +03:00
|
|
|
use GraphQL\Type\Schema;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-07-03 19:08:20 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/**
|
|
|
|
* DESCRIBE: Execute: Handles execution of abstract types with promises
|
|
|
|
*/
|
2018-07-29 18:43:10 +03:00
|
|
|
class AbstractPromiseTest extends TestCase
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('isTypeOf used to resolve runtime type for Interface')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$PetType = new InterfaceType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
|
|
|
'interfaces' => [$PetType],
|
2018-09-26 12:07:23 +03:00
|
|
|
'isTypeOf' => static function ($obj) {
|
|
|
|
return new Deferred(static function () use ($obj) {
|
2017-07-03 19:08:20 +03:00
|
|
|
return $obj instanceof Dog;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
'woofs' => ['type' => Type::boolean()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
|
|
|
'interfaces' => [$PetType],
|
2018-09-26 12:07:23 +03:00
|
|
|
'isTypeOf' => static function ($obj) {
|
|
|
|
return new Deferred(static function () use ($obj) {
|
2017-07-03 19:08:20 +03:00
|
|
|
return $obj instanceof Cat;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
'meows' => ['type' => Type::boolean()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [
|
|
|
|
new Dog('Odie', true),
|
2018-09-01 18:07:06 +03:00
|
|
|
new Cat('Garfield', false),
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]),
|
2018-09-01 18:07:06 +03:00
|
|
|
'types' => [$CatType, $DogType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
name
|
|
|
|
... on Dog {
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray();
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
['name' => 'Odie', 'woofs' => true],
|
|
|
|
['name' => 'Garfield', 'meows' => false],
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('isTypeOf can be rejected')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIsTypeOfCanBeRejected() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$PetType = new InterfaceType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-26 12:07:23 +03:00
|
|
|
'isTypeOf' => static function () {
|
|
|
|
return new Deferred(static function () {
|
2017-07-18 16:57:30 +03:00
|
|
|
throw new UserError('We are testing this error');
|
2017-07-03 19:08:20 +03:00
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'woofs' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-26 12:07:23 +03:00
|
|
|
'isTypeOf' => static function ($obj) {
|
|
|
|
return new Deferred(static function () use ($obj) {
|
2017-07-03 19:08:20 +03:00
|
|
|
return $obj instanceof Cat;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'meows' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [
|
|
|
|
new Dog('Odie', true),
|
2018-09-01 18:07:06 +03:00
|
|
|
new Cat('Garfield', false),
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]),
|
2018-09-01 18:07:06 +03:00
|
|
|
'types' => [$CatType, $DogType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
name
|
|
|
|
... on Dog {
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray();
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
2018-09-01 18:07:06 +03:00
|
|
|
'data' => [
|
|
|
|
'pets' => [null, null],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
|
|
|
'errors' => [
|
|
|
|
[
|
2018-09-01 18:07:06 +03:00
|
|
|
'message' => 'We are testing this error',
|
2017-07-03 19:08:20 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 7]],
|
2018-09-01 18:07:06 +03:00
|
|
|
'path' => ['pets', 0],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
|
|
|
[
|
2018-09-01 18:07:06 +03:00
|
|
|
'message' => 'We are testing this error',
|
2017-07-03 19:08:20 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 7]],
|
2018-09-01 18:07:06 +03:00
|
|
|
'path' => ['pets', 1],
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertArraySubset($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('isTypeOf used to resolve runtime type for Union')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
2018-09-26 12:07:23 +03:00
|
|
|
'isTypeOf' => static function ($obj) {
|
|
|
|
return new Deferred(static function () use ($obj) {
|
2017-07-03 19:08:20 +03:00
|
|
|
return $obj instanceof Dog;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'woofs' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
2018-09-26 12:07:23 +03:00
|
|
|
'isTypeOf' => static function ($obj) {
|
|
|
|
return new Deferred(static function () use ($obj) {
|
2017-07-03 19:08:20 +03:00
|
|
|
return $obj instanceof Cat;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'meows' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$PetType = new UnionType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
|
|
|
'types' => [$DogType, $CatType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [new Dog('Odie', true), new Cat('Garfield', false)];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
... on Dog {
|
|
|
|
name
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
name
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray();
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'pets' => [
|
|
|
|
['name' => 'Odie', 'woofs' => true],
|
2018-09-01 18:07:06 +03:00
|
|
|
['name' => 'Garfield', 'meows' => false],
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('resolveType on Interface yields useful error')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testResolveTypeOnInterfaceYieldsUsefulError() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$PetType = new InterfaceType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolveType' => static function ($obj) use (&$DogType, &$CatType, &$HumanType) {
|
|
|
|
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType) {
|
2017-07-03 19:08:20 +03:00
|
|
|
if ($obj instanceof Dog) {
|
|
|
|
return $DogType;
|
|
|
|
}
|
|
|
|
if ($obj instanceof Cat) {
|
|
|
|
return $CatType;
|
|
|
|
}
|
|
|
|
if ($obj instanceof Human) {
|
|
|
|
return $HumanType;
|
|
|
|
}
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2017-07-03 19:08:20 +03:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$HumanType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Human',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'woofs' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'meows' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
|
|
|
return new Deferred(static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [
|
|
|
|
new Dog('Odie', true),
|
|
|
|
new Cat('Garfield', false),
|
2018-09-01 18:07:06 +03:00
|
|
|
new Human('Jon'),
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
});
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]),
|
2018-09-01 18:07:06 +03:00
|
|
|
'types' => [$CatType, $DogType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
name
|
|
|
|
... on Dog {
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray(true);
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
2018-09-01 18:07:06 +03:00
|
|
|
'data' => [
|
2017-07-03 19:08:20 +03:00
|
|
|
'pets' => [
|
|
|
|
['name' => 'Odie', 'woofs' => true],
|
|
|
|
['name' => 'Garfield', 'meows' => false],
|
2018-09-01 18:07:06 +03:00
|
|
|
null,
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
|
|
|
'errors' => [
|
|
|
|
[
|
2017-08-14 19:52:17 +03:00
|
|
|
'debugMessage' => 'Runtime Object type "Human" is not a possible type for "Pet".',
|
2018-09-01 18:07:06 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 7]],
|
|
|
|
'path' => ['pets', 2],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertArraySubset($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('resolveType on Union yields useful error')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testResolveTypeOnUnionYieldsUsefulError() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$HumanType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Human',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'woofs' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'meows' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$PetType = new UnionType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolveType' => static function ($obj) use ($DogType, $CatType, $HumanType) {
|
|
|
|
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType) {
|
2017-07-03 19:08:20 +03:00
|
|
|
if ($obj instanceof Dog) {
|
|
|
|
return $DogType;
|
|
|
|
}
|
|
|
|
if ($obj instanceof Cat) {
|
|
|
|
return $CatType;
|
|
|
|
}
|
|
|
|
if ($obj instanceof Human) {
|
|
|
|
return $HumanType;
|
|
|
|
}
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2017-07-03 19:08:20 +03:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'types' => [$DogType, $CatType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [
|
|
|
|
new Dog('Odie', true),
|
|
|
|
new Cat('Garfield', false),
|
2018-09-01 18:07:06 +03:00
|
|
|
new Human('Jon'),
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
... on Dog {
|
|
|
|
name
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
name
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray(true);
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
2018-09-01 18:07:06 +03:00
|
|
|
'data' => [
|
2017-07-03 19:08:20 +03:00
|
|
|
'pets' => [
|
|
|
|
['name' => 'Odie', 'woofs' => true],
|
|
|
|
['name' => 'Garfield', 'meows' => false],
|
2018-09-01 18:07:06 +03:00
|
|
|
null,
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
|
|
|
'errors' => [
|
|
|
|
[
|
2017-08-14 19:52:17 +03:00
|
|
|
'debugMessage' => 'Runtime Object type "Human" is not a possible type for "Pet".',
|
2018-09-01 18:07:06 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 7]],
|
|
|
|
'path' => ['pets', 2],
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertArraySubset($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('resolveType allows resolving with type name')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testResolveTypeAllowsResolvingWithTypeName() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$PetType = new InterfaceType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolveType' => static function ($obj) {
|
|
|
|
return new Deferred(static function () use ($obj) {
|
2017-07-03 19:08:20 +03:00
|
|
|
if ($obj instanceof Dog) {
|
|
|
|
return 'Dog';
|
|
|
|
}
|
|
|
|
if ($obj instanceof Cat) {
|
|
|
|
return 'Cat';
|
|
|
|
}
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2017-07-03 19:08:20 +03:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'woofs' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'meows' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [
|
|
|
|
new Dog('Odie', true),
|
2018-09-01 18:07:06 +03:00
|
|
|
new Cat('Garfield', false),
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]),
|
2018-09-01 18:07:06 +03:00
|
|
|
'types' => [$CatType, $DogType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
name
|
|
|
|
... on Dog {
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray();
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'pets' => [
|
|
|
|
['name' => 'Odie', 'woofs' => true],
|
|
|
|
['name' => 'Garfield', 'meows' => false],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('resolveType can be caught')
|
2017-07-03 19:08:20 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testResolveTypeCanBeCaught() : void
|
2017-07-03 19:08:20 +03:00
|
|
|
{
|
|
|
|
$PetType = new InterfaceType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Pet',
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolveType' => static function () {
|
|
|
|
return new Deferred(static function () {
|
2017-07-18 16:57:30 +03:00
|
|
|
throw new UserError('We are testing this error');
|
2017-07-03 19:08:20 +03:00
|
|
|
});
|
|
|
|
},
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$DogType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Dog',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'woofs' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$CatType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Cat',
|
2017-07-03 19:08:20 +03:00
|
|
|
'interfaces' => [$PetType],
|
2018-09-01 18:07:06 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => ['type' => Type::string()],
|
2017-07-03 19:08:20 +03:00
|
|
|
'meows' => ['type' => Type::boolean()],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2017-07-03 19:08:20 +03:00
|
|
|
'fields' => [
|
|
|
|
'pets' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::listOf($PetType),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2017-07-03 19:08:20 +03:00
|
|
|
return [
|
|
|
|
new Dog('Odie', true),
|
2018-09-01 18:07:06 +03:00
|
|
|
new Cat('Garfield', false),
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
]),
|
2018-09-01 18:07:06 +03:00
|
|
|
'types' => [$CatType, $DogType],
|
2017-07-03 19:08:20 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$query = '{
|
|
|
|
pets {
|
|
|
|
name
|
|
|
|
... on Dog {
|
|
|
|
woofs
|
|
|
|
}
|
|
|
|
... on Cat {
|
|
|
|
meows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray();
|
2017-07-03 19:08:20 +03:00
|
|
|
|
|
|
|
$expected = [
|
2018-09-01 18:07:06 +03:00
|
|
|
'data' => [
|
|
|
|
'pets' => [null, null],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
|
|
|
'errors' => [
|
|
|
|
[
|
2018-09-01 18:07:06 +03:00
|
|
|
'message' => 'We are testing this error',
|
2017-07-03 19:08:20 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 7]],
|
2018-09-01 18:07:06 +03:00
|
|
|
'path' => ['pets', 0],
|
2017-07-03 19:08:20 +03:00
|
|
|
],
|
|
|
|
[
|
2018-09-01 18:07:06 +03:00
|
|
|
'message' => 'We are testing this error',
|
2017-07-03 19:08:20 +03:00
|
|
|
'locations' => [['line' => 2, 'column' => 7]],
|
2018-09-01 18:07:06 +03:00
|
|
|
'path' => ['pets', 1],
|
|
|
|
],
|
|
|
|
],
|
2017-07-03 19:08:20 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertArraySubset($expected, $result);
|
2017-07-03 19:08:20 +03:00
|
|
|
}
|
|
|
|
}
|