Spec compliance improvement: data key should not exist in response when it is null / missing (#56)

This commit is contained in:
vladar 2016-10-18 22:25:39 +07:00
parent 3e2d9459aa
commit 7f22d4b874
3 changed files with 8 additions and 6 deletions

View File

@ -37,7 +37,11 @@ class ExecutionResult
*/ */
public function toArray() public function toArray()
{ {
$result = ['data' => $this->data]; $result = [];
if (null !== $this->data) {
$result['data'] = $this->data;
}
if (!empty($this->errors)) { if (!empty($this->errors)) {
$result['errors'] = array_map(['GraphQL\Error', 'formatError'], $this->errors); $result['errors'] = array_map(['GraphQL\Error', 'formatError'], $this->errors);

View File

@ -9,17 +9,17 @@ class ExecutionResultTest extends \PHPUnit_Framework_TestCase
{ {
$executionResult = new ExecutionResult(); $executionResult = new ExecutionResult();
$this->assertEquals(['data' => null], $executionResult->toArray()); $this->assertEquals([], $executionResult->toArray());
} }
public function testToArrayExtensions() public function testToArrayExtensions()
{ {
$executionResult = new ExecutionResult(null, [], ['foo' => 'bar']); $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']; $executionResult->extensions = ['bar' => 'foo'];
$this->assertEquals(['data' => null, 'extensions' => ['bar' => 'foo']], $executionResult->toArray()); $this->assertEquals(['extensions' => ['bar' => 'foo']], $executionResult->toArray());
} }
} }

View File

@ -243,7 +243,6 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
'; ';
$expected = [ $expected = [
'data' => null,
'errors' => [ 'errors' => [
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(2, 17)]) FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(2, 17)])
] ]
@ -259,7 +258,6 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
'; ';
$expected = [ $expected = [
'data' => null,
'errors' => [ 'errors' => [
FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(2, 17)]), FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(2, 17)]),
] ]