graphql-php/tests/Executor/AbstractTest.php

569 lines
17 KiB
PHP
Raw Normal View History

<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Executor;
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor;
use GraphQL\GraphQL;
use GraphQL\Language\Parser;
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;
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;
2018-09-01 18:07:06 +03:00
/**
* Execute: Handles execution of abstract types
*/
2018-07-29 18:43:10 +03:00
class AbstractTest extends TestCase
{
/**
* @see it('isTypeOf used to resolve runtime type for Interface')
*/
public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
{
// isTypeOf used to resolve runtime type for Interface
$petType = new InterfaceType([
2018-09-01 18:07:06 +03:00
'name' => 'Pet',
'fields' => [
2018-09-01 18:07:06 +03:00
'name' => ['type' => Type::string()],
],
]);
// Added to interface type when defined
$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) {
2018-09-01 18:07:06 +03:00
return $obj instanceof Dog;
},
'fields' => [
'name' => ['type' => Type::string()],
'woofs' => ['type' => Type::boolean()],
],
]);
$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 $obj instanceof Cat;
},
2018-09-01 18:07:06 +03:00
'fields' => [
'name' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +03:00
],
]);
$schema = new Schema([
'query' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'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 Dog('Odie', true), new Cat('Garfield', false)];
2018-09-01 18:07:06 +03:00
},
],
],
]),
2018-09-01 18:07:06 +03:00
'types' => [$catType, $dogType],
]);
$query = '{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}';
$expected = new ExecutionResult([
'pets' => [
['name' => 'Odie', 'woofs' => true],
2018-09-01 18:07:06 +03:00
['name' => 'Garfield', 'meows' => false],
],
]);
$result = Executor::execute($schema, Parser::parse($query));
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, $result);
}
/**
* @see it('isTypeOf used to resolve runtime type for Union')
*/
public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
{
$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) {
2018-09-01 18:07:06 +03:00
return $obj instanceof Dog;
},
'fields' => [
'name' => ['type' => Type::string()],
'woofs' => ['type' => Type::boolean()],
],
]);
$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 $obj instanceof Cat;
},
2018-09-01 18:07:06 +03:00
'fields' => [
'name' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +03:00
],
]);
$petType = new UnionType([
2018-09-01 18:07:06 +03:00
'name' => 'Pet',
'types' => [$dogType, $catType],
]);
$schema = new Schema([
'query' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'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 () {
2018-09-01 18:07:06 +03:00
return [new Dog('Odie', true), new Cat('Garfield', false)];
},
],
],
]),
]);
$query = '{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}';
$expected = new ExecutionResult([
'pets' => [
['name' => 'Odie', 'woofs' => true],
2018-09-01 18:07:06 +03:00
['name' => 'Garfield', 'meows' => false],
],
]);
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, Executor::execute($schema, Parser::parse($query)));
}
/**
* @see it('resolveType on Interface yields useful error')
*/
2018-09-01 18:07:06 +03:00
public function testResolveTypeOnInterfaceYieldsUsefulError() : void
{
2018-09-01 18:07:06 +03:00
$DogType = null;
$CatType = null;
$HumanType = null;
$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) {
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
return null;
},
2018-09-01 18:07:06 +03:00
'fields' => [
'name' => ['type' => Type::string()],
],
]);
$HumanType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Human',
'fields' => [
'name' => ['type' => Type::string()],
2018-09-01 18:07:06 +03:00
],
]);
$DogType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Dog',
'interfaces' => [$PetType],
2018-09-01 18:07:06 +03:00
'fields' => [
'name' => ['type' => Type::string()],
'woofs' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +03:00
],
]);
$CatType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Cat',
'interfaces' => [$PetType],
2018-09-01 18:07:06 +03:00
'fields' => [
'name' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +03:00
],
]);
$schema = new Schema([
'query' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'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 Dog('Odie', true),
new Cat('Garfield', false),
2018-09-01 18:07:06 +03:00
new Human('Jon'),
];
2018-09-01 18:07:06 +03:00
},
],
],
]),
2018-09-01 18:07:06 +03:00
'types' => [$DogType, $CatType],
]);
$query = '{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}';
$expected = [
2018-09-01 18:07:06 +03:00
'data' => [
'pets' => [
['name' => 'Odie', 'woofs' => true],
['name' => 'Garfield', 'meows' => false],
2018-09-01 18:07:06 +03:00
null,
],
],
2016-11-19 00:15:40 +03:00
'errors' => [[
'debugMessage' => 'Runtime Object type "Human" is not a possible type for "Pet".',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 11]],
'path' => ['pets', 2],
],
],
];
2018-09-01 18:07:06 +03:00
$actual = GraphQL::executeQuery($schema, $query)->toArray(true);
2018-09-19 18:12:09 +03:00
self::assertArraySubset($expected, $actual);
}
/**
* @see it('resolveType on Union yields useful error')
*/
public function testResolveTypeOnUnionYieldsUsefulError() : void
{
$HumanType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Human',
'fields' => [
'name' => ['type' => Type::string()],
2018-09-01 18:07:06 +03:00
],
]);
$DogType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Dog',
'fields' => [
2018-09-01 18:07:06 +03:00
'name' => ['type' => Type::string()],
'woofs' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +03:00
],
]);
$CatType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Cat',
'fields' => [
2018-09-01 18:07:06 +03:00
'name' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
2018-09-01 18:07:06 +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) {
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
'types' => [$DogType, $CatType],
]);
$schema = new Schema([
'query' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'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 Dog('Odie', true),
new Cat('Garfield', false),
2018-09-01 18:07:06 +03:00
new Human('Jon'),
];
2018-09-01 18:07:06 +03:00
},
],
],
]),
]);
$query = '{
pets {
... on Dog {
name
woofs
}
... on Cat {
name
meows
}
}
}';
2018-09-01 18:07:06 +03:00
$result = GraphQL::executeQuery($schema, $query)->toArray(true);
$expected = [
2018-09-01 18:07:06 +03:00
'data' => [
'pets' => [
2018-09-01 18:07:06 +03:00
[
'name' => 'Odie',
'woofs' => true,
],
[
'name' => 'Garfield',
'meows' => false,
],
null,
],
],
2016-11-19 00:15:40 +03:00
'errors' => [[
'debugMessage' => 'Runtime Object type "Human" is not a possible type for "Pet".',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 11]],
'path' => ['pets', 2],
],
],
];
2018-09-19 18:12:09 +03:00
self::assertArraySubset($expected, $result);
}
/**
* @see it('returning invalid value from resolveType yields useful error')
*/
public function testReturningInvalidValueFromResolveTypeYieldsUsefulError() : void
{
$fooInterface = new InterfaceType([
2018-09-01 18:07:06 +03:00
'name' => 'FooInterface',
'fields' => ['bar' => ['type' => Type::string()]],
2018-09-26 12:07:23 +03:00
'resolveType' => static function () {
return [];
},
]);
$fooObject = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'FooObject',
'fields' => ['bar' => ['type' => Type::string()]],
'interfaces' => [$fooInterface],
]);
$schema = new Schema([
'query' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'fields' => [
'foo' => [
2018-09-01 18:07:06 +03:00
'type' => $fooInterface,
2018-09-26 12:07:23 +03:00
'resolve' => static function () {
return 'dummy';
},
],
],
]),
'types' => [$fooObject],
]);
$result = GraphQL::executeQuery($schema, '{ foo { bar } }');
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['foo' => null],
'errors' => [
[
2018-09-01 18:07:06 +03:00
'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.',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 1, 'column' => 3]],
'path' => ['foo'],
'category' => 'internal',
],
],
];
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, $result->toArray(true));
}
/**
* @see it('resolveType allows resolving with type name')
*/
public function testResolveTypeAllowsResolvingWithTypeName() : void
{
$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) {
2018-09-01 18:07:06 +03:00
if ($obj instanceof Dog) {
return 'Dog';
}
if ($obj instanceof Cat) {
return 'Cat';
}
return null;
},
2018-09-01 18:07:06 +03:00
'fields' => [
'name' => ['type' => Type::string()],
],
]);
$DogType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Dog',
'interfaces' => [$PetType],
'fields' => [
'name' => ['type' => Type::string()],
'woofs' => ['type' => Type::boolean()],
],
]);
$CatType = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Cat',
'interfaces' => [$PetType],
'fields' => [
'name' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
],
]);
$schema = new Schema([
'query' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'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 Dog('Odie', true),
2018-09-01 18:07:06 +03:00
new Cat('Garfield', false),
];
2018-09-01 18:07:06 +03:00
},
],
],
]),
2018-09-01 18:07:06 +03:00
'types' => [$CatType, $DogType],
]);
$query = '{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}';
$result = GraphQL::executeQuery($schema, $query)->toArray();
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-01 18:07:06 +03:00
[
'data' => [
'pets' => [
['name' => 'Odie', 'woofs' => true],
['name' => 'Garfield', 'meows' => false],
],
],
],
$result
);
}
public function testHintsOnConflictingTypeInstancesInResolveType() : void
{
2018-09-26 12:07:23 +03:00
$createTest = static function () use (&$iface) {
return new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Test',
'fields' => [
'a' => Type::string(),
],
2018-09-26 12:07:23 +03:00
'interfaces' => static function () use ($iface) {
return [$iface];
2018-09-01 18:07:06 +03:00
},
]);
};
$iface = new InterfaceType([
2018-09-01 18:07:06 +03:00
'name' => 'Node',
'fields' => [
'a' => Type::string(),
],
2018-09-26 12:07:23 +03:00
'resolveType' => static function () use (&$createTest) {
return $createTest();
2018-09-01 18:07:06 +03:00
},
]);
$query = new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Query',
'fields' => [
'node' => $iface,
2018-09-01 18:07:06 +03:00
'test' => $createTest(),
],
]);
2018-09-01 18:07:06 +03:00
$schema = new Schema(['query' => $query]);
$schema->assertValid();
$query = '
{
node {
a
}
}
';
$result = Executor::execute($schema, Parser::parse($query), ['node' => ['a' => 'value']]);
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-01 18:07:06 +03:00
'Schema must contain unique named types but contains multiple types named "Test". ' .
'Make sure that `resolveType` function of abstract type "Node" returns the same type instance ' .
'as referenced anywhere else within the schema ' .
'(see http://webonyx.github.io/graphql-php/type-system/#type-registry).',
$result->errors[0]->getMessage()
);
}
}