diff --git a/src/Executor/Values.php b/src/Executor/Values.php index 06c9532fd..9a14d116a 100644 --- a/src/Executor/Values.php +++ b/src/Executor/Values.php @@ -12,6 +12,7 @@ use GraphQL\Language\AST\FragmentSpreadNode; use GraphQL\Language\AST\InlineFragmentNode; use GraphQL\Language\AST\NodeList; +use GraphQL\Language\AST\NullValueNode; use GraphQL\Language\AST\VariableNode; use GraphQL\Language\AST\VariableDefinitionNode; use GraphQL\Language\Printer; @@ -131,10 +132,16 @@ public static function getArgumentValues($def, $node, $variableValues = null) [$node] ); } + } else if ($argumentNode->value instanceof NullValueNode) { + // Explicitly set null values should produce the defaultValue if available + if ($argDef->defaultValueExists()) { + $coercedValues[$name] = $argDef->defaultValue; + } } else if ($argumentNode->value instanceof VariableNode) { $variableName = $argumentNode->value->name->value; - if ($variableValues && array_key_exists($variableName, $variableValues)) { + // Explicitly set null values should produce the defaultValue if available + if ($variableValues && array_key_exists($variableName, $variableValues) && $variableValues[$variableName] !== null) { // Note: this does not check that this variable value is correct. // This assumes that this query has been validated and the variable // usage here is of the correct type. @@ -152,6 +159,12 @@ public static function getArgumentValues($def, $node, $variableValues = null) } else { $valueNode = $argumentNode->value; $coercedValue = AST::valueFromAST($valueNode, $argType, $variableValues); + + // Explicitly set null values should produce the defaultValue if available + if ($coercedValue === null && $argDef->defaultValueExists()) { + $coercedValue = $argDef->defaultValue; + } + if ($coercedValue === $undefined) { $errors = DocumentValidator::isValidLiteralValue($argType, $valueNode); $message = !empty($errors) ? ("\n" . implode("\n", $errors)) : ''; diff --git a/tests/Executor/VariablesTest.php b/tests/Executor/VariablesTest.php index 418ed12d4..945f566cb 100644 --- a/tests/Executor/VariablesTest.php +++ b/tests/Executor/VariablesTest.php @@ -852,6 +852,36 @@ public function testWhenOmittedVariableProvided() ); } + /** + * @it when null value variable provided + */ + public function testWhenNullValueVariableProvided() + { + $ast = Parser::parse('query optionalVariable($optional: String) { + fieldWithDefaultArgumentValue(input: $optional) + }'); + + $this->assertEquals( + ['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']], + Executor::execute($this->schema(), $ast, null, null, ['optional' => null])->toArray() + ); + } + + /** + * @it when null value provided directly + */ + public function testWhenNullValueProvidedDirectly() + { + $ast = Parser::parse('query { + fieldWithDefaultArgumentValue(input: null) + }'); + + $this->assertEquals( + ['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']], + Executor::execute($this->schema(), $ast)->toArray() + ); + } + /** * @it not when argument cannot be coerced */