Skip to content

Commit

Permalink
Initialize directives and arguments in Node with empty NodeList
Browse files Browse the repository at this point in the history
  • Loading branch information
shmax committed Feb 28, 2023
1 parent f50040e commit fd3540d
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Language/AST/DirectiveDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ class DirectiveDefinitionNode extends Node implements TypeSystemDefinitionNode

/** @var NodeList<NameNode> */
public NodeList $locations;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->arguments ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/DirectiveNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ class DirectiveNode extends Node

/** @var NodeList<ArgumentNode> */
public NodeList $arguments;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->arguments ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/EnumTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public function getName(): NameNode
{
return $this->name;
}

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/EnumTypeExtensionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class EnumTypeExtensionNode extends Node implements TypeExtensionNode
/** @var NodeList<EnumValueDefinitionNode> */
public NodeList $values;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}

public function getName(): NameNode
{
return $this->name;
Expand Down
7 changes: 7 additions & 0 deletions src/Language/AST/FieldNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ class FieldNode extends Node implements SelectionNode
public NodeList $directives;

public ?SelectionSetNode $selectionSet = null;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
$this->arguments ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/FragmentDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class FragmentDefinitionNode extends Node implements ExecutableDefinitionNode, H

public SelectionSetNode $selectionSet;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}

public function getSelectionSet(): SelectionSetNode
{
return $this->selectionSet;
Expand Down
6 changes: 6 additions & 0 deletions src/Language/AST/FragmentSpreadNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ class FragmentSpreadNode extends Node implements SelectionNode

/** @var NodeList<DirectiveNode> */
public NodeList $directives;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/InlineFragmentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class InlineFragmentNode extends Node implements SelectionNode
public NodeList $directives;

public SelectionSetNode $selectionSet;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/InputObjectTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public function getName(): NameNode
{
return $this->name;
}

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}
}
6 changes: 6 additions & 0 deletions src/Language/AST/OperationDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class OperationDefinitionNode extends Node implements ExecutableDefinitionNode,

public SelectionSetNode $selectionSet;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}

public function getSelectionSet(): SelectionSetNode
{
return $this->selectionSet;
Expand Down
6 changes: 6 additions & 0 deletions src/Language/AST/VariableDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ class VariableDefinitionNode extends Node implements DefinitionNode

/** @var NodeList<DirectiveNode> */
public NodeList $directives;

public function __construct(array $vars)
{
parent::__construct($vars);
$this->directives ??= new NodeList([]);
}
}
103 changes: 103 additions & 0 deletions tests/Utils/AstFromArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php declare(strict_types=1);

namespace GraphQL\Tests\Utils;

use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Utils\AST;
use PHPUnit\Framework\TestCase;

final class AstFromArrayTest extends TestCase
{
public function testFromArray(): void
{
$res = AST::fromArray([
'kind' => 'VariableDefinition',
'directives' => [],
]);
assert($res instanceof VariableDefinitionNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'OperationDefinition',
'directives' => [],
]);
assert($res instanceof OperationDefinitionNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'Field',
'directives' => [],
'arguments' => [],
]);
assert($res instanceof FieldNode);
self::assertEquals($res->directives, new NodeList([]));
self::assertEquals($res->arguments, new NodeList([]));

$res = AST::fromArray([
'kind' => 'FragmentSpread',
'directives' => [],
]);
assert($res instanceof FragmentSpreadNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'FragmentDefinition',
'directives' => [],
]);
assert($res instanceof FragmentDefinitionNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'InlineFragment',
'directives' => [],
]);
assert($res instanceof InlineFragmentNode);
self::assertEquals($res->directives, new NodeList([]));
}

public function testFromOptimizedArray(): void
{
$res = AST::fromArray([
'kind' => 'VariableDefinition',
]);
assert($res instanceof VariableDefinitionNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'OperationDefinition',
]);
assert($res instanceof OperationDefinitionNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'Field',
]);
assert($res instanceof FieldNode);
self::assertEquals($res->directives, new NodeList([]));
self::assertEquals($res->arguments, new NodeList([]));

$res = AST::fromArray([
'kind' => 'FragmentSpread',
]);
assert($res instanceof FragmentSpreadNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'FragmentDefinition',
]);
assert($res instanceof FragmentDefinitionNode);
self::assertEquals($res->directives, new NodeList([]));

$res = AST::fromArray([
'kind' => 'InlineFragment',
]);
assert($res instanceof InlineFragmentNode);
self::assertEquals($res->directives, new NodeList([]));
}
}

0 comments on commit fd3540d

Please sign in to comment.