Skip to content

Commit 658836c

Browse files
committed
Use defaultValue for explictly set null values
1 parent 94525c0 commit 658836c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Executor/Values.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use GraphQL\Language\AST\FragmentSpreadNode;
1313
use GraphQL\Language\AST\InlineFragmentNode;
1414
use GraphQL\Language\AST\NodeList;
15+
use GraphQL\Language\AST\NullValueNode;
1516
use GraphQL\Language\AST\VariableNode;
1617
use GraphQL\Language\AST\VariableDefinitionNode;
1718
use GraphQL\Language\Printer;
@@ -131,10 +132,16 @@ public static function getArgumentValues($def, $node, $variableValues = null)
131132
[$node]
132133
);
133134
}
135+
} else if ($argumentNode->value instanceof NullValueNode) {
136+
// Explicitly set null values should produce the defaultValue if available
137+
if ($argDef->defaultValueExists()) {
138+
$coercedValues[$name] = $argDef->defaultValue;
139+
}
134140
} else if ($argumentNode->value instanceof VariableNode) {
135141
$variableName = $argumentNode->value->name->value;
136142

137-
if ($variableValues && array_key_exists($variableName, $variableValues)) {
143+
// Explicitly set null values should produce the defaultValue if available
144+
if ($variableValues && array_key_exists($variableName, $variableValues) && $variableValues[$variableName] !== null) {
138145
// Note: this does not check that this variable value is correct.
139146
// This assumes that this query has been validated and the variable
140147
// usage here is of the correct type.
@@ -152,6 +159,12 @@ public static function getArgumentValues($def, $node, $variableValues = null)
152159
} else {
153160
$valueNode = $argumentNode->value;
154161
$coercedValue = AST::valueFromAST($valueNode, $argType, $variableValues);
162+
163+
// Explicitly set null values should produce the defaultValue if available
164+
if ($coercedValue === null && $argDef->defaultValueExists()) {
165+
$coercedValue = $argDef->defaultValue;
166+
}
167+
155168
if ($coercedValue === $undefined) {
156169
$errors = DocumentValidator::isValidLiteralValue($argType, $valueNode);
157170
$message = !empty($errors) ? ("\n" . implode("\n", $errors)) : '';

tests/Executor/VariablesTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,36 @@ public function testWhenOmittedVariableProvided()
852852
);
853853
}
854854

855+
/**
856+
* @it when null value variable provided
857+
*/
858+
public function testWhenNullValueVariableProvided()
859+
{
860+
$ast = Parser::parse('query optionalVariable($optional: String) {
861+
fieldWithDefaultArgumentValue(input: $optional)
862+
}');
863+
864+
$this->assertEquals(
865+
['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
866+
Executor::execute($this->schema(), $ast, null, null, ['optional' => null])->toArray()
867+
);
868+
}
869+
870+
/**
871+
* @it when null value provided directly
872+
*/
873+
public function testWhenNullValueProvidedDirectly()
874+
{
875+
$ast = Parser::parse('query {
876+
fieldWithDefaultArgumentValue(input: null)
877+
}');
878+
879+
$this->assertEquals(
880+
['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
881+
Executor::execute($this->schema(), $ast)->toArray()
882+
);
883+
}
884+
855885
/**
856886
* @it not when argument cannot be coerced
857887
*/

0 commit comments

Comments
 (0)