From 31d89acfae5a27b772cd0ebec992d0e564796cf5 Mon Sep 17 00:00:00 2001 From: Jan Bukva Date: Thu, 27 Dec 2018 14:48:03 +0100 Subject: [PATCH 1/3] Failing test for CoroutineExecutor removes associative array when using custom scalar producing JSON --- tests/Executor/ExecutorSchemaTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Executor/ExecutorSchemaTest.php b/tests/Executor/ExecutorSchemaTest.php index 4c3b9d6..36a2a73 100644 --- a/tests/Executor/ExecutorSchemaTest.php +++ b/tests/Executor/ExecutorSchemaTest.php @@ -6,6 +6,7 @@ namespace GraphQL\Tests\Executor; use GraphQL\Executor\Executor; use GraphQL\Language\Parser; +use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; @@ -20,6 +21,13 @@ class ExecutorSchemaTest extends TestCase */ public function testExecutesUsingASchema() : void { + $BlogSerializableValueType = new CustomScalarType([ + 'name' => 'JsonSerializableValueScalar', + 'serialize' => static function ($value) { return $value; }, + 'parseValue' => static function ($value) { return $value; }, + 'parseLiteral' => static function ($value) { return $value; }, + ]); + $BlogArticle = null; $BlogImage = new ObjectType([ 'name' => 'Image', @@ -57,6 +65,7 @@ class ExecutorSchemaTest extends TestCase 'title' => ['type' => Type::string()], 'body' => ['type' => Type::string()], 'keywords' => ['type' => Type::listOf(Type::string())], + 'meta' => ['type' => $BlogSerializableValueType], ], ]); @@ -113,6 +122,7 @@ class ExecutorSchemaTest extends TestCase keywords } } + meta } } @@ -191,6 +201,9 @@ class ExecutorSchemaTest extends TestCase 'keywords' => ['foo', 'bar', '1', 'true', null], ], ], + 'meta' => [ + 'title' => 'My Article 1 | My Blog' + ] ], ], ]; @@ -210,6 +223,9 @@ class ExecutorSchemaTest extends TestCase 'body' => 'This is a post', 'hidden' => 'This data is not exposed in the schema', 'keywords' => ['foo', 'bar', 1, true, null], + 'meta' => [ + 'title' => 'My Article 1 | My Blog', + ], ]; }; From 2295b96a493b9ce6528abd8e1a644e1164bb9fbe Mon Sep 17 00:00:00 2001 From: Jan Bukva Date: Thu, 27 Dec 2018 16:00:57 +0100 Subject: [PATCH 2/3] Fix CoroutineExecutor::resultToArray for associative arrays --- src/Experimental/Executor/CoroutineExecutor.php | 14 +++++++++++--- tests/Executor/ExecutorSchemaTest.php | 14 +++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Experimental/Executor/CoroutineExecutor.php b/src/Experimental/Executor/CoroutineExecutor.php index b473c56..7d985c1 100644 --- a/src/Experimental/Executor/CoroutineExecutor.php +++ b/src/Experimental/Executor/CoroutineExecutor.php @@ -34,8 +34,11 @@ use GraphQL\Utils\Utils; use SplQueue; use stdClass; use Throwable; +use function array_keys; +use function count; use function is_array; use function is_string; +use function range; use function sprintf; class CoroutineExecutor implements Runtime, ExecutorImplementation @@ -151,9 +154,14 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation } if (is_array($value)) { - $array = []; - foreach ($value as $item) { - $array[] = self::resultToArray($item); + $array = []; + $isAssoc = array_keys($value) !== range(0, count($value) - 1); + foreach ($value as $key => $item) { + if ($isAssoc) { + $array[$key] = self::resultToArray($item); + } else { + $array[] = self::resultToArray($item); + } } return $array; } diff --git a/tests/Executor/ExecutorSchemaTest.php b/tests/Executor/ExecutorSchemaTest.php index 36a2a73..4946c59 100644 --- a/tests/Executor/ExecutorSchemaTest.php +++ b/tests/Executor/ExecutorSchemaTest.php @@ -23,9 +23,9 @@ class ExecutorSchemaTest extends TestCase { $BlogSerializableValueType = new CustomScalarType([ 'name' => 'JsonSerializableValueScalar', - 'serialize' => static function ($value) { return $value; }, - 'parseValue' => static function ($value) { return $value; }, - 'parseLiteral' => static function ($value) { return $value; }, + 'serialize' => static function ($value) { + return $value; + }, ]); $BlogArticle = null; @@ -201,9 +201,7 @@ class ExecutorSchemaTest extends TestCase 'keywords' => ['foo', 'bar', '1', 'true', null], ], ], - 'meta' => [ - 'title' => 'My Article 1 | My Blog' - ] + 'meta' => [ 'title' => 'My Article 1 | My Blog' ], ], ], ]; @@ -223,9 +221,7 @@ class ExecutorSchemaTest extends TestCase 'body' => 'This is a post', 'hidden' => 'This data is not exposed in the schema', 'keywords' => ['foo', 'bar', 1, true, null], - 'meta' => [ - 'title' => 'My Article 1 | My Blog', - ], + 'meta' => ['title' => 'My Article 1 | My Blog'], ]; }; From 00490d289c0a858842e08cead45635016f147921 Mon Sep 17 00:00:00 2001 From: Jan Bukva Date: Thu, 27 Dec 2018 21:53:16 +0100 Subject: [PATCH 3/3] Use key-value foreach --- src/Experimental/Executor/CoroutineExecutor.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Experimental/Executor/CoroutineExecutor.php b/src/Experimental/Executor/CoroutineExecutor.php index 7d985c1..b089fad 100644 --- a/src/Experimental/Executor/CoroutineExecutor.php +++ b/src/Experimental/Executor/CoroutineExecutor.php @@ -34,11 +34,8 @@ use GraphQL\Utils\Utils; use SplQueue; use stdClass; use Throwable; -use function array_keys; -use function count; use function is_array; use function is_string; -use function range; use function sprintf; class CoroutineExecutor implements Runtime, ExecutorImplementation @@ -154,14 +151,9 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation } if (is_array($value)) { - $array = []; - $isAssoc = array_keys($value) !== range(0, count($value) - 1); + $array = []; foreach ($value as $key => $item) { - if ($isAssoc) { - $array[$key] = self::resultToArray($item); - } else { - $array[] = self::resultToArray($item); - } + $array[$key] = self::resultToArray($item); } return $array; }