From 04550f805fbe452612cf9256efad28ca54096a39 Mon Sep 17 00:00:00 2001 From: vladar Date: Sat, 19 Nov 2016 17:21:22 +0700 Subject: [PATCH] Differentiate between input values with no default and with default = null --- src/Type/Introspection.php | 7 ++++++- tests/Type/IntrospectionTest.php | 34 +++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Type/Introspection.php b/src/Type/Introspection.php index 6a325f5..5b44f50 100644 --- a/src/Type/Introspection.php +++ b/src/Type/Introspection.php @@ -6,7 +6,9 @@ use GraphQL\Language\Printer; use GraphQL\Schema; use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\EnumType; +use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldDefinition; +use GraphQL\Type\Definition\InputObjectField; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\ListOfType; @@ -613,8 +615,11 @@ EOD; ], 'defaultValue' => [ 'type' => Type::string(), + 'description' => + 'A GraphQL-formatted string representing the default value for this input value.', 'resolve' => function ($inputValue) { - return $inputValue->defaultValue === null + /** @var FieldArgument|InputObjectField $inputValue */ + return !$inputValue->defaultValueExists() ? null : Printer::doPrint(AST::astFromValue($inputValue->defaultValue, $inputValue->getType())); } diff --git a/tests/Type/IntrospectionTest.php b/tests/Type/IntrospectionTest.php index 3c10912..ac24e46 100644 --- a/tests/Type/IntrospectionTest.php +++ b/tests/Type/IntrospectionTest.php @@ -1079,7 +1079,8 @@ class IntrospectionTest extends \PHPUnit_Framework_TestCase 'name' => 'TestInputObject', 'fields' => [ 'a' => ['type' => Type::string(), 'defaultValue' => 'foo'], - 'b' => ['type' => Type::listOf(Type::string())] + 'b' => ['type' => Type::listOf(Type::string())], + 'c' => ['type' => Type::string(), 'defaultValue' => null ] ] ]); @@ -1135,17 +1136,32 @@ class IntrospectionTest extends \PHPUnit_Framework_TestCase 'kind' => 'INPUT_OBJECT', 'name' => 'TestInputObject', 'inputFields' => [ - ['name' => 'a', 'type' => [ - 'kind' => 'SCALAR', - 'name' => 'String', - 'ofType' => null], + [ + 'name' => 'a', + 'type' => [ + 'kind' => 'SCALAR', + 'name' => 'String', + 'ofType' => null + ], 'defaultValue' => '"foo"' ], - ['name' => 'b', 'type' => [ - 'kind' => 'LIST', - 'name' => null, - 'ofType' => ['kind' => 'SCALAR', 'name' => 'String', 'ofType' => null]], + [ + 'name' => 'b', + 'type' => [ + 'kind' => 'LIST', + 'name' => null, + 'ofType' => ['kind' => 'SCALAR', 'name' => 'String', 'ofType' => null] + ], 'defaultValue' => null + ], + [ + 'name' => 'c', + 'type' => [ + 'kind' => 'SCALAR', + 'name' => 'String', + 'ofType' => null + ], + 'defaultValue' => 'null' // defaultValue was set (even if it was set to null) ] ] ];