Fix CoroutineExecutor::resultToArray for associative arrays

This commit is contained in:
Jan Bukva 2018-12-27 16:00:57 +01:00
parent 31d89acfae
commit 2295b96a49
No known key found for this signature in database
GPG Key ID: D5A7CEFD2A8F7D13
2 changed files with 16 additions and 12 deletions

View File

@ -34,8 +34,11 @@ use GraphQL\Utils\Utils;
use SplQueue; use SplQueue;
use stdClass; use stdClass;
use Throwable; use Throwable;
use function array_keys;
use function count;
use function is_array; use function is_array;
use function is_string; use function is_string;
use function range;
use function sprintf; use function sprintf;
class CoroutineExecutor implements Runtime, ExecutorImplementation class CoroutineExecutor implements Runtime, ExecutorImplementation
@ -151,9 +154,14 @@ class CoroutineExecutor implements Runtime, ExecutorImplementation
} }
if (is_array($value)) { if (is_array($value)) {
$array = []; $array = [];
foreach ($value as $item) { $isAssoc = array_keys($value) !== range(0, count($value) - 1);
$array[] = self::resultToArray($item); foreach ($value as $key => $item) {
if ($isAssoc) {
$array[$key] = self::resultToArray($item);
} else {
$array[] = self::resultToArray($item);
}
} }
return $array; return $array;
} }

View File

@ -23,9 +23,9 @@ class ExecutorSchemaTest extends TestCase
{ {
$BlogSerializableValueType = new CustomScalarType([ $BlogSerializableValueType = new CustomScalarType([
'name' => 'JsonSerializableValueScalar', 'name' => 'JsonSerializableValueScalar',
'serialize' => static function ($value) { return $value; }, 'serialize' => static function ($value) {
'parseValue' => static function ($value) { return $value; }, return $value;
'parseLiteral' => static function ($value) { return $value; }, },
]); ]);
$BlogArticle = null; $BlogArticle = null;
@ -201,9 +201,7 @@ class ExecutorSchemaTest extends TestCase
'keywords' => ['foo', 'bar', '1', 'true', null], 'keywords' => ['foo', 'bar', '1', 'true', null],
], ],
], ],
'meta' => [ 'meta' => [ 'title' => 'My Article 1 | My Blog' ],
'title' => 'My Article 1 | My Blog'
]
], ],
], ],
]; ];
@ -223,9 +221,7 @@ class ExecutorSchemaTest extends TestCase
'body' => 'This is a post', 'body' => 'This is a post',
'hidden' => 'This data is not exposed in the schema', 'hidden' => 'This data is not exposed in the schema',
'keywords' => ['foo', 'bar', 1, true, null], 'keywords' => ['foo', 'bar', 1, true, null],
'meta' => [ 'meta' => ['title' => 'My Article 1 | My Blog'],
'title' => 'My Article 1 | My Blog',
],
]; ];
}; };