mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-29 00:25:17 +03:00
Spec compliance improvement: data key should not exist in response when it is null / missing (#56)
This commit is contained in:
parent
3e2d9459aa
commit
7f22d4b874
@ -37,7 +37,11 @@ class ExecutionResult
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$result = ['data' => $this->data];
|
||||
$result = [];
|
||||
|
||||
if (null !== $this->data) {
|
||||
$result['data'] = $this->data;
|
||||
}
|
||||
|
||||
if (!empty($this->errors)) {
|
||||
$result['errors'] = array_map(['GraphQL\Error', 'formatError'], $this->errors);
|
||||
|
@ -9,17 +9,17 @@ class ExecutionResultTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$executionResult = new ExecutionResult();
|
||||
|
||||
$this->assertEquals(['data' => null], $executionResult->toArray());
|
||||
$this->assertEquals([], $executionResult->toArray());
|
||||
}
|
||||
|
||||
public function testToArrayExtensions()
|
||||
{
|
||||
$executionResult = new ExecutionResult(null, [], ['foo' => 'bar']);
|
||||
|
||||
$this->assertEquals(['data' => null, 'extensions' => ['foo' => 'bar']], $executionResult->toArray());
|
||||
$this->assertEquals(['extensions' => ['foo' => 'bar']], $executionResult->toArray());
|
||||
|
||||
$executionResult->extensions = ['bar' => 'foo'];
|
||||
|
||||
$this->assertEquals(['data' => null, 'extensions' => ['bar' => 'foo']], $executionResult->toArray());
|
||||
$this->assertEquals(['extensions' => ['bar' => 'foo']], $executionResult->toArray());
|
||||
}
|
||||
}
|
||||
|
@ -243,7 +243,6 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
|
||||
';
|
||||
|
||||
$expected = [
|
||||
'data' => null,
|
||||
'errors' => [
|
||||
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(2, 17)])
|
||||
]
|
||||
@ -259,7 +258,6 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
|
||||
';
|
||||
|
||||
$expected = [
|
||||
'data' => null,
|
||||
'errors' => [
|
||||
FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(2, 17)]),
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user