Return stdClass vs empty array for empty ObjectType values (see #59)

This commit is contained in:
vladar 2016-10-22 17:11:03 +07:00
parent d41687913a
commit a612b780c9
2 changed files with 86 additions and 2 deletions

View File

@ -96,7 +96,7 @@ class Executor
$data = null; $data = null;
} }
return new ExecutionResult($data, $exeContext->errors); return new ExecutionResult((array) $data, $exeContext->errors);
} }
/** /**
@ -231,6 +231,10 @@ class Executor
$results[$responseName] = $result; $results[$responseName] = $result;
} }
} }
// see #59
if ([] === $results) {
$results = new \stdClass();
}
return $results; return $results;
} }

View File

@ -840,4 +840,84 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $result->toArray()); $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());
}
} }