Skip to content

add support for NullValue #76

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

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
1 change: 1 addition & 0 deletions src/Language/AST/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ abstract class Node

// Values

const NULL = 'NullValue';
const INT = 'IntValue';
const FLOAT = 'FloatValue';
const STRING = 'StringValue';
Expand Down
32 changes: 32 additions & 0 deletions src/Language/AST/NullValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace GraphQL\Language\AST;

class NullValue extends Node implements Value
{
public $kind = Node::NULL;

/**
* @var null
*/
protected $value = null;

/**
* NullValue constructor.
*
* @param string $value
* @param null $loc
*/
public function __construct($loc = null)
{
$this->loc = $loc;
}

/**
* @return null
*/
public function getValue()
{
return $this->value;
}
}
1 change: 1 addition & 0 deletions src/Language/AST/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| EnumValue
| ListValue
| ObjectValue
| NullValue
*/
interface Value
{
Expand Down
9 changes: 7 additions & 2 deletions src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use GraphQL\Language\AST\Name;
use GraphQL\Language\AST\NamedType;
use GraphQL\Language\AST\NonNullType;
use GraphQL\Language\AST\NullValue;
use GraphQL\Language\AST\ObjectField;
use GraphQL\Language\AST\ObjectTypeDefinition;
use GraphQL\Language\AST\ObjectValue;
Expand Down Expand Up @@ -655,14 +656,18 @@ function parseValueLiteral($isConst)
'value' => $token->value === 'true',
'loc' => $this->loc($token)
]);
} else if ($token->value !== 'null') {
} else if ($token->value === 'null') {
$this->lexer->advance();
return new NullValue(
$this->loc($token)
);
} else {
$this->lexer->advance();
return new EnumValue([
'value' => $token->value,
'loc' => $this->loc($token)
]);
}
break;

case Token::DOLLAR:
if (!$isConst) {
Expand Down
23 changes: 20 additions & 3 deletions tests/Language/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GraphQL\Language\AST\Name;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\OperationDefinition;
use GraphQL\Language\AST\NullValue;
use GraphQL\Language\AST\SelectionSet;
use GraphQL\Language\AST\StringValue;
use GraphQL\Language\Parser;
Expand Down Expand Up @@ -112,10 +113,26 @@ public function testDoesNotAcceptFragmentSpreadOfOn()
/**
* @it does not allow null as value
*/
public function testDoesNotAllowNullAsValue()
public function testAllowsNullAsValue()
{
$this->setExpectedException('GraphQL\Error\SyntaxError', 'Syntax Error GraphQL (1:39) Unexpected Name "null"');
Parser::parse('{ fieldWithNullableStringInput(input: null) }');
$expected = new SelectionSet([
'selections' => [
new Field([
'name' => new Name(['value' => 'fieldWithNullableStringInput']),
'arguments' => [
new Argument([
'name' => new Name(['value' => 'input']),
'value' => new NullValue()
])
],
'directives' => []
])
]
]);

$result = Parser::parse('{ fieldWithNullableStringInput(input: null) }', ['noLocation' => true]);

$this->assertEquals($expected, $result->definitions[0]->selectionSet);
}

/**
Expand Down