From d982bad63a561197ac7db53711e60f605b690577 Mon Sep 17 00:00:00 2001 From: vladar Date: Fri, 23 Oct 2015 17:30:02 +0600 Subject: [PATCH] Make sure default argument values are passed to resolve function (#4) --- src/Executor/Values.php | 2 +- tests/Executor/ExecutorTest.php | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Executor/Values.php b/src/Executor/Values.php index 5d0caaf..5068f53 100644 --- a/src/Executor/Values.php +++ b/src/Executor/Values.php @@ -60,7 +60,7 @@ class Values */ public static function getArgumentValues($argDefs, $argASTs, $variableValues) { - if (!$argDefs || !$argASTs) { + if (!$argDefs) { return []; } $argASTMap = $argASTs ? Utils::keyMap($argASTs, function ($arg) { diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index 744f093..9c23386 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -512,4 +512,38 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase $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()); + } }