Make sure default argument values are passed to resolve function (#4)

This commit is contained in:
vladar 2015-10-23 17:30:02 +06:00
parent 965c3ff743
commit d982bad63a
2 changed files with 35 additions and 1 deletions

View File

@ -60,7 +60,7 @@ class Values
*/ */
public static function getArgumentValues($argDefs, $argASTs, $variableValues) public static function getArgumentValues($argDefs, $argASTs, $variableValues)
{ {
if (!$argDefs || !$argASTs) { if (!$argDefs) {
return []; return [];
} }
$argASTMap = $argASTs ? Utils::keyMap($argASTs, function ($arg) { $argASTMap = $argASTs ? Utils::keyMap($argASTs, function ($arg) {

View File

@ -512,4 +512,38 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $result->toArray()); $this->assertEquals($expected, $result->toArray());
} }
public function testSubstitutesArgumentWithDefaultValue()
{
$schema = new Schema(
new ObjectType([
'name' => 'Type',
'fields' => [
'field' => [
'type' => Type::string(),
'resolve' => function($data, $args) {return $args ? json_encode($args) : '';},
'args' => [
'a' => ['type' => Type::boolean(), 'defaultValue' => 1],
'b' => ['type' => Type::boolean(), 'defaultValue' => null],
'c' => ['type' => Type::boolean(), 'defaultValue' => 0],
'd' => ['type' => Type::int(), 'defaultValue' => false],
'e' => ['type' => Type::int(), 'defaultValue' => '0'],
'f' => ['type' => Type::int(), 'defaultValue' => 'some-string'],
'g' => ['type' => Type::boolean()]
]
]
]
])
);
$query = Parser::parse('{ field }');
$result = Executor::execute($schema, $query);
$expected = [
'data' => [
'field' => '{"a":1,"c":0,"d":false,"e":"0","f":"some-string"}'
]
];
$this->assertEquals($expected, $result->toArray());
}
} }