Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit 3c2df83

Browse files
authored
Fix coercing of false values (#303)
1 parent 45d38b8 commit 3c2df83

File tree

3 files changed

+42
-48
lines changed

3 files changed

+42
-48
lines changed

src/Execution/CoercedValue.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class CoercedValue
1818

1919
/**
2020
* CoercedValue constructor.
21-
* @param mixed $value
22-
* @param array|null $errors
21+
* @param mixed $value
22+
* @param array $errors
2323
*/
24-
public function __construct($value, ?array $errors)
24+
public function __construct($value, array $errors = [])
2525
{
2626
$this->errors = $errors;
2727
$this->value = $value;
@@ -32,7 +32,7 @@ public function __construct($value, ?array $errors)
3232
*/
3333
public function getErrors(): array
3434
{
35-
return $this->errors ?? [];
35+
return $this->errors;
3636
}
3737

3838
/**

src/Execution/ValuesHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ private function coerceValue($value, $type, $blameNode, ?Path $path = null): Coe
258258
return $this->coerceValueForNonNullType($value, $type, $blameNode, $path);
259259
}
260260

261-
if (empty($value)) {
262-
return new CoercedValue(null, null);
261+
if (null === $value) {
262+
return new CoercedValue(null);
263263
}
264264

265265
if ($type instanceof ScalarType) {
@@ -327,12 +327,12 @@ protected function coerceValueForScalarType(
327327
): CoercedValue {
328328
try {
329329
$parseResult = $type->parseValue($value);
330-
if (empty($parseResult)) {
330+
if (null === $parseResult) {
331331
return new CoercedValue(null, [
332332
new GraphQLException(sprintf('Expected type %s', (string)$type))
333333
]);
334334
}
335-
return new CoercedValue($parseResult, null);
335+
return new CoercedValue($parseResult);
336336
} catch (InvalidTypeException|CoercingException $ex) {
337337
return new CoercedValue(null, [
338338
$this->buildCoerceException(

tests/Functional/Execution/ValuesHelperTest.php

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,32 @@
1717

1818
class ValuesHelperTest extends TestCase
1919
{
20-
/**
21-
* @throws \Digia\GraphQL\Error\InvariantException
22-
* @throws \Digia\GraphQL\Error\SyntaxErrorException
23-
*/
2420
public function testCoerceArgumentValues()
2521
{
22+
/** @noinspection PhpUnhandledExceptionInspection */
2623
$schema = newSchema([
27-
'query' =>
28-
newObjectType([
29-
'name' => 'Greeting',
30-
'fields' => [
31-
'greeting' => [
32-
'type' => stringType(),
33-
'args' => [
34-
'name' => [
35-
'type' => stringType(),
36-
]
37-
]
38-
]
39-
]
40-
])
24+
'query' => newObjectType([
25+
'name' => 'Greeting',
26+
'fields' => [
27+
'greeting' => [
28+
'type' => stringType(),
29+
'args' => [
30+
'name' => [
31+
'type' => stringType(),
32+
],
33+
],
34+
],
35+
],
36+
]),
4137
]);
4238

39+
/** @noinspection PhpUnhandledExceptionInspection */
4340
$documentNode = parse('query Hello($name: String) { Greeting(name: $name) }');
4441
/** @var OperationDefinitionNode $operation */
4542
$operation = $documentNode->getDefinitions()[0];
4643
/** @var ArgumentsAwareInterface $node */
47-
$node = $operation->getSelectionSet()->getSelections()[0];
44+
$node = $operation->getSelectionSet()->getSelections()[0];
45+
/** @noinspection PhpUnhandledExceptionInspection */
4846
$definition = $schema->getQueryType()->getFields()['greeting'];
4947

5048
$context = new ExecutionContext(
@@ -56,30 +54,26 @@ public function testCoerceArgumentValues()
5654
$this->assertSame(['name' => 'Han Solo'], $args);
5755
}
5856

59-
/**
60-
* @throws \Digia\GraphQL\Error\InvariantException
61-
* @throws \Digia\GraphQL\Error\SyntaxErrorException
62-
*/
6357
public function testCoerceVariableValues(): void
6458
{
65-
// Test that non-nullable booleans are handled correctly
59+
/** @noinspection PhpUnhandledExceptionInspection */
6660
$schema = newSchema([
67-
'query' =>
68-
newObjectType([
69-
'name' => 'nonNullBoolean',
70-
'fields' => [
71-
'greeting' => [
72-
'type' => stringType(),
73-
'args' => [
74-
'shout' => [
75-
'type' => newNonNull(booleanType()),
76-
]
77-
]
78-
]
79-
]
80-
])
61+
'query' => newObjectType([
62+
'name' => 'nonNullBoolean',
63+
'fields' => [
64+
'greeting' => [
65+
'type' => stringType(),
66+
'args' => [
67+
'shout' => [
68+
'type' => newNonNull(booleanType()),
69+
],
70+
],
71+
],
72+
],
73+
]),
8174
]);
8275

76+
/** @noinspection PhpUnhandledExceptionInspection */
8377
$documentNode = parse('
8478
query ($shout: Boolean!) {
8579
nonNullBoolean(shout: $shout)
@@ -92,11 +86,11 @@ public function testCoerceVariableValues(): void
9286

9387
// Try with true and false and null (null should give errors, the rest shouldn't)
9488
$coercedValue = coerceVariableValues($schema, $variableDefinitions, ['shout' => true]);
95-
$this->assertEquals(['shout' => true], $coercedValue->getValue());
89+
$this->assertSame(['shout' => true], $coercedValue->getValue());
9690
$this->assertFalse($coercedValue->hasErrors());
9791

9892
$coercedValue = coerceVariableValues($schema, $variableDefinitions, ['shout' => false]);
99-
$this->assertEquals(['shout' => false], $coercedValue->getValue());
93+
$this->assertSame(['shout' => false], $coercedValue->getValue());
10094
$this->assertFalse($coercedValue->hasErrors());
10195

10296
$coercedValue = coerceVariableValues($schema, $variableDefinitions, ['shout' => null]);

0 commit comments

Comments
 (0)