mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 14:26:08 +03:00
Return stdClass vs empty array for empty ObjectType values (see #59)
This commit is contained in:
parent
d41687913a
commit
a612b780c9
@ -96,7 +96,7 @@ class Executor
|
||||
$data = null;
|
||||
}
|
||||
|
||||
return new ExecutionResult($data, $exeContext->errors);
|
||||
return new ExecutionResult((array) $data, $exeContext->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -231,6 +231,10 @@ class Executor
|
||||
$results[$responseName] = $result;
|
||||
}
|
||||
}
|
||||
// see #59
|
||||
if ([] === $results) {
|
||||
$results = new \stdClass();
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
|
@ -840,4 +840,84 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals($expected, $result->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/webonyx/graphql-php/issues/59
|
||||
*/
|
||||
public function testSerializesToEmptyObjectVsEmptyArray()
|
||||
{
|
||||
$iface = null;
|
||||
|
||||
$a = new ObjectType([
|
||||
'name' => 'A',
|
||||
'fields' => [
|
||||
'id' => Type::id()
|
||||
],
|
||||
'interfaces' => function() use (&$iface) {
|
||||
return [$iface];
|
||||
}
|
||||
]);
|
||||
|
||||
$b = new ObjectType([
|
||||
'name' => 'B',
|
||||
'fields' => [
|
||||
'id' => Type::id()
|
||||
],
|
||||
'interfaces' => function() use (&$iface) {
|
||||
return [$iface];
|
||||
}
|
||||
]);
|
||||
|
||||
$iface = new InterfaceType([
|
||||
'name' => 'Iface',
|
||||
'fields' => [
|
||||
'id' => Type::id()
|
||||
],
|
||||
'resolveType' => function($v) use ($a, $b) {
|
||||
return $v['type'] === 'A' ? $a : $b;
|
||||
}
|
||||
]);
|
||||
|
||||
$schema = new Schema([
|
||||
'query' => new ObjectType([
|
||||
'name' => 'Query',
|
||||
'fields' => [
|
||||
'ab' => Type::listOf($iface)
|
||||
]
|
||||
]),
|
||||
'types' => [$a, $b]
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'ab' => [
|
||||
['id' => 1, 'type' => 'A'],
|
||||
['id' => 2, 'type' => 'A'],
|
||||
['id' => 3, 'type' => 'B'],
|
||||
['id' => 4, 'type' => 'B']
|
||||
]
|
||||
];
|
||||
|
||||
$query = Parser::parse('
|
||||
{
|
||||
ab {
|
||||
... on A{
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
');
|
||||
|
||||
$result = Executor::execute($schema, $query, $data, null);
|
||||
|
||||
$this->assertEquals([
|
||||
'data' => [
|
||||
'ab' => [
|
||||
['id' => '1'],
|
||||
['id' => '2'],
|
||||
new \stdClass(),
|
||||
new \stdClass()
|
||||
]
|
||||
]
|
||||
], $result->toArray());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user