Skip to content

Commit 428870a

Browse files
committed
add support for NullValue
1 parent 1ee29e3 commit 428870a

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

src/Language/AST/Node.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ abstract class Node
3131

3232
// Values
3333

34+
const NULL = 'NullValue';
3435
const INT = 'IntValue';
3536
const FLOAT = 'FloatValue';
3637
const STRING = 'StringValue';

src/Language/AST/NullValue.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace GraphQL\Language\AST;
4+
5+
class NullValue extends Node implements Value
6+
{
7+
public $kind = Node::NULL;
8+
9+
/**
10+
* @var null
11+
*/
12+
protected $value = null;
13+
14+
/**
15+
* NullValue constructor.
16+
*
17+
* @param string $value
18+
* @param null $loc
19+
*/
20+
public function __construct($loc = null)
21+
{
22+
$this->loc = $loc;
23+
}
24+
25+
/**
26+
* @return null
27+
*/
28+
public function getValue()
29+
{
30+
return $this->value;
31+
}
32+
}

src/Language/AST/Value.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| EnumValue
1111
| ListValue
1212
| ObjectValue
13+
| NullValue
1314
*/
1415
interface Value
1516
{

src/Language/Parser.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use GraphQL\Language\AST\Name;
2828
use GraphQL\Language\AST\NamedType;
2929
use GraphQL\Language\AST\NonNullType;
30+
use GraphQL\Language\AST\NullValue;
3031
use GraphQL\Language\AST\ObjectField;
3132
use GraphQL\Language\AST\ObjectTypeDefinition;
3233
use GraphQL\Language\AST\ObjectValue;
@@ -655,14 +656,18 @@ function parseValueLiteral($isConst)
655656
'value' => $token->value === 'true',
656657
'loc' => $this->loc($token)
657658
]);
658-
} else if ($token->value !== 'null') {
659+
} else if ($token->value === 'null') {
660+
$this->lexer->advance();
661+
return new NullValue(
662+
$this->loc($token)
663+
);
664+
} else {
659665
$this->lexer->advance();
660666
return new EnumValue([
661667
'value' => $token->value,
662668
'loc' => $this->loc($token)
663669
]);
664670
}
665-
break;
666671

667672
case Token::DOLLAR:
668673
if (!$isConst) {

tests/Language/ParserTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use GraphQL\Language\AST\Name;
1010
use GraphQL\Language\AST\Node;
1111
use GraphQL\Language\AST\OperationDefinition;
12+
use GraphQL\Language\AST\NullValue;
1213
use GraphQL\Language\AST\SelectionSet;
1314
use GraphQL\Language\AST\StringValue;
1415
use GraphQL\Language\Parser;
@@ -112,10 +113,26 @@ public function testDoesNotAcceptFragmentSpreadOfOn()
112113
/**
113114
* @it does not allow null as value
114115
*/
115-
public function testDoesNotAllowNullAsValue()
116+
public function testAllowsNullAsValue()
116117
{
117-
$this->setExpectedException('GraphQL\Error\SyntaxError', 'Syntax Error GraphQL (1:39) Unexpected Name "null"');
118-
Parser::parse('{ fieldWithNullableStringInput(input: null) }');
118+
$expected = new SelectionSet([
119+
'selections' => [
120+
new Field([
121+
'name' => new Name(['value' => 'fieldWithNullableStringInput']),
122+
'arguments' => [
123+
new Argument([
124+
'name' => new Name(['value' => 'input']),
125+
'value' => new NullValue()
126+
])
127+
],
128+
'directives' => []
129+
])
130+
]
131+
]);
132+
133+
$result = Parser::parse('{ fieldWithNullableStringInput(input: null) }', ['noLocation' => true]);
134+
135+
$this->assertEquals($expected, $result->definitions[0]->selectionSet);
119136
}
120137

121138
/**

0 commit comments

Comments
 (0)