Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve ambiguous null interpretation with regards to defaultValues #262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function testWhenNullValueVariableProvided()
public function testWhenNullValueVariableProvided() : void

{
$ast = Parser::parse('query optionalVariable($optional: String) {
fieldWithDefaultArgumentValue(input: $optional)
}');

$this->assertEquals(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals(
self::assertEquals(

['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
Executor::execute($this->schema(), $ast, null, null, ['optional' => null])->toArray()
);
}

/**
* @it when null value provided directly
*/
public function testWhenNullValueProvidedDirectly()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function testWhenNullValueProvidedDirectly()
public function testWhenNullValueProvidedDirectly() : void

{
$ast = Parser::parse('query {
fieldWithDefaultArgumentValue(input: null)
}');

$this->assertEquals(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals(
self::assertEquals(

['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
Executor::execute($this->schema(), $ast)->toArray()
);
}

/**
* @it not when argument cannot be coerced
*/
Expand Down