Skip to content

Commit

Permalink
Use defaultValue for explictly set null values
Browse files Browse the repository at this point in the history
  • Loading branch information
samsouder committed Mar 21, 2018
1 parent 94525c0 commit 658836c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Executor/Values.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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)) : '';
Expand Down
30 changes: 30 additions & 0 deletions tests/Executor/VariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 658836c

Please sign in to comment.