2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Executor;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\Error;
|
2016-04-09 10:36:53 +03:00
|
|
|
use GraphQL\Executor\Executor;
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Schema;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
|
|
|
|
class NonNullTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-10-18 18:15:21 +03:00
|
|
|
/** @var \Exception */
|
2015-07-15 20:05:46 +03:00
|
|
|
public $syncError;
|
2016-10-18 18:15:21 +03:00
|
|
|
|
|
|
|
/** @var \Exception */
|
2015-07-15 20:05:46 +03:00
|
|
|
public $nonNullSyncError;
|
|
|
|
public $throwingData;
|
|
|
|
public $nullingData;
|
|
|
|
public $schema;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-10-18 18:15:21 +03:00
|
|
|
$this->syncError = new \Exception('sync');
|
|
|
|
$this->nonNullSyncError = new \Exception('nonNullSync');
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
$this->throwingData = [
|
|
|
|
'sync' => function () {
|
|
|
|
throw $this->syncError;
|
|
|
|
},
|
|
|
|
'nonNullSync' => function () {
|
|
|
|
throw $this->nonNullSyncError;
|
|
|
|
},
|
|
|
|
'nest' => function () {
|
|
|
|
return $this->throwingData;
|
|
|
|
},
|
|
|
|
'nonNullNest' => function () {
|
|
|
|
return $this->throwingData;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->nullingData = [
|
|
|
|
'sync' => function () {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
'nonNullSync' => function () {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
'nest' => function () {
|
|
|
|
return $this->nullingData;
|
|
|
|
},
|
|
|
|
'nonNullNest' => function () {
|
|
|
|
return $this->nullingData;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
$dataType = new ObjectType([
|
|
|
|
'name' => 'DataType',
|
2016-10-22 20:49:25 +03:00
|
|
|
'fields' => function() use (&$dataType) {
|
|
|
|
return [
|
|
|
|
'sync' => ['type' => Type::string()],
|
|
|
|
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
|
|
|
|
'nest' => $dataType,
|
|
|
|
'nonNullNest' => Type::nonNull($dataType)
|
|
|
|
];
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
$this->schema = new Schema(['query' => $dataType]);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute: handles non-nullable types
|
2016-05-02 00:42:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @it nulls a nullable field that throws synchronously
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testNullsANullableFieldThatThrowsSynchronously()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query Q {
|
|
|
|
sync
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$ast = Parser::parse($doc);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'sync' => null,
|
|
|
|
],
|
|
|
|
'errors' => [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-10-18 18:15:21 +03:00
|
|
|
$this->syncError->getMessage(),
|
2015-07-15 20:05:46 +03:00
|
|
|
[new SourceLocation(3, 9)]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
];
|
2016-10-21 11:28:24 +03:00
|
|
|
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsSynchronously()
|
|
|
|
{
|
|
|
|
// nulls a synchronously returned object that contains a non-nullable field that throws synchronously
|
|
|
|
$doc = '
|
|
|
|
query Q {
|
|
|
|
nest {
|
|
|
|
nonNullSync,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$ast = Parser::parse($doc);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'nest' => null
|
|
|
|
],
|
|
|
|
'errors' => [
|
2016-10-18 18:15:21 +03:00
|
|
|
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(4, 11)])
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
];
|
2016-10-21 11:28:24 +03:00
|
|
|
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullsAComplexTreeOfNullableFieldsThatThrow()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query Q {
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$ast = Parser::parse($doc);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'nest' => [
|
|
|
|
'sync' => null,
|
|
|
|
'nest' => [
|
|
|
|
'sync' => null,
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'errors' => [
|
2016-10-18 18:15:21 +03:00
|
|
|
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(4, 11)]),
|
|
|
|
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(6, 13)]),
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
];
|
2016-10-21 11:28:24 +03:00
|
|
|
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullsANullableFieldThatSynchronouslyReturnsNull()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query Q {
|
|
|
|
sync
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$ast = Parser::parse($doc);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'sync' => null,
|
|
|
|
]
|
|
|
|
];
|
2016-05-02 00:42:05 +03:00
|
|
|
$this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test4()
|
|
|
|
{
|
|
|
|
// nulls a synchronously returned object that contains a non-nullable field that returns null synchronously
|
|
|
|
$doc = '
|
|
|
|
query Q {
|
|
|
|
nest {
|
|
|
|
nonNullSync,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$ast = Parser::parse($doc);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'nest' => null
|
|
|
|
],
|
|
|
|
'errors' => [
|
2016-05-02 00:42:05 +03:00
|
|
|
FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(4, 11)])
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
];
|
2016-10-21 11:28:24 +03:00
|
|
|
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test5()
|
|
|
|
{
|
|
|
|
// nulls a complex tree of nullable fields that return null
|
|
|
|
|
|
|
|
$doc = '
|
|
|
|
query Q {
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
$ast = Parser::parse($doc);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'nest' => [
|
|
|
|
'sync' => null,
|
|
|
|
'nest' => [
|
|
|
|
'sync' => null,
|
|
|
|
'nest' => [
|
|
|
|
'sync' => null
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
2016-05-02 00:42:05 +03:00
|
|
|
$this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullsTheTopLevelIfSyncNonNullableFieldThrows()
|
|
|
|
{
|
|
|
|
$doc = '
|
|
|
|
query Q { nonNullSync }
|
|
|
|
';
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
2016-10-18 18:15:21 +03:00
|
|
|
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(2, 17)])
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
];
|
2016-10-21 11:28:24 +03:00
|
|
|
$this->assertArraySubset($expected, Executor::execute($this->schema, Parser::parse($doc), $this->throwingData)->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullsTheTopLevelIfSyncNonNullableFieldReturnsNull()
|
|
|
|
{
|
|
|
|
// nulls the top level if sync non-nullable field returns null
|
|
|
|
$doc = '
|
|
|
|
query Q { nonNullSync }
|
|
|
|
';
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'errors' => [
|
2016-05-02 00:42:05 +03:00
|
|
|
FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(2, 17)]),
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
];
|
2016-10-21 11:28:24 +03:00
|
|
|
$this->assertArraySubset($expected, Executor::execute($this->schema, Parser::parse($doc), $this->nullingData)->toArray());
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|