From 7f22d4b87467cc844187ddf118c505b967437b7d Mon Sep 17 00:00:00 2001 From: vladar Date: Tue, 18 Oct 2016 22:25:39 +0700 Subject: [PATCH] Spec compliance improvement: data key should not exist in response when it is null / missing (#56) --- src/Executor/ExecutionResult.php | 6 +++++- tests/Executor/ExecutionResultTest.php | 6 +++--- tests/Executor/NonNullTest.php | 2 -- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Executor/ExecutionResult.php b/src/Executor/ExecutionResult.php index 7fc7523..8f78ef3 100644 --- a/src/Executor/ExecutionResult.php +++ b/src/Executor/ExecutionResult.php @@ -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); diff --git a/tests/Executor/ExecutionResultTest.php b/tests/Executor/ExecutionResultTest.php index 1c942f7..68bff90 100644 --- a/tests/Executor/ExecutionResultTest.php +++ b/tests/Executor/ExecutionResultTest.php @@ -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()); } } diff --git a/tests/Executor/NonNullTest.php b/tests/Executor/NonNullTest.php index b2cd3cd..c0b67ef 100644 --- a/tests/Executor/NonNullTest.php +++ b/tests/Executor/NonNullTest.php @@ -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)]), ]