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

Differentiate between const and non-const AST nodes #1335

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Changed

- Differentiate between const and non-const AST nodes

## v15.2.3

### Fixed
Expand Down
30 changes: 17 additions & 13 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,10 @@ allowLegacySDLImplementsInterfaces?: bool,
experimentalFragmentVariables?: bool
}

@phpstan-import-type ConstValueNodeVariants from ConstValueNode
@phpstan-import-type ValueNodeVariants from ValueNode
@phpstan-import-type TypeNodeVariants from TypeNode

noLocation:
(By default, the parser creates AST nodes that know the location
in the source that they correspond to. This configuration flag
Expand Down Expand Up @@ -926,17 +930,17 @@ Those magic functions allow partial parsing:
@method static FragmentSpreadNode|InlineFragmentNode fragment(Source|string $source, bool[] $options = [])
@method static FragmentDefinitionNode fragmentDefinition(Source|string $source, bool[] $options = [])
@method static NameNode fragmentName(Source|string $source, bool[] $options = [])
@method static BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode valueLiteral(Source|string $source, bool[] $options = [])
@method static BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode constValueLiteral(Source|string $source, bool[] $options = [])
@method static ValueNodeVariants valueLiteral(Source|string $source, bool[] $options = [])
@method static ConstValueNodeVariants constValueLiteral(Source|string $source, bool[] $options = [])
@method static StringValueNode stringLiteral(Source|string $source, bool[] $options = [])
@method static BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode constValue(Source|string $source, bool[] $options = [])
@method static BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|ObjectValueNode|StringValueNode|VariableNode variableValue(Source|string $source, bool[] $options = [])
@method static ConstValueNodeVariants constValue(Source|string $source, bool[] $options = [])
@method static ValueNodeVariants variableValue(Source|string $source, bool[] $options = [])
@method static ListValueNode array(Source|string $source, bool[] $options = [])
@method static ListValueNode constArray(Source|string $source, bool[] $options = [])
@method static ConstListValueNode constArray(Source|string $source, bool[] $options = [])
@method static ObjectValueNode object(Source|string $source, bool[] $options = [])
@method static ObjectValueNode constObject(Source|string $source, bool[] $options = [])
@method static ConstObjectValueNode constObject(Source|string $source, bool[] $options = [])
@method static ObjectFieldNode objectField(Source|string $source, bool[] $options = [])
@method static ObjectFieldNode constObjectField(Source|string $source, bool[] $options = [])
@method static ConstObjectFieldNode constObjectField(Source|string $source, bool[] $options = [])
@method static NodeList<DirectiveNode> directives(Source|string $source, bool[] $options = [])
@method static NodeList<DirectiveNode> constDirectives(Source|string $source, bool[] $options = [])
@method static DirectiveNode directive(Source|string $source, bool[] $options = [])
Expand Down Expand Up @@ -1012,11 +1016,11 @@ static function parse($source, array $options = []): GraphQL\Language\AST\Docume
* @throws \JsonException
* @throws SyntaxError
*
* @return BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode
* @return ValueNodeVariants
*
* @api
*/
static function parseValue($source, array $options = [])
static function parseValue($source, array $options = []): GraphQL\Language\AST\ValueNode
```

```php
Expand All @@ -1037,11 +1041,11 @@ static function parseValue($source, array $options = [])
* @throws \JsonException
* @throws SyntaxError
*
* @return ListTypeNode|NamedTypeNode|NonNullTypeNode
* @return TypeNodeVariants
*
* @api
*/
static function parseType($source, array $options = [])
static function parseType($source, array $options = []): GraphQL\Language\AST\TypeNode
```

## GraphQL\Language\Printer
Expand Down Expand Up @@ -2534,7 +2538,7 @@ static function astFromValue($value, GraphQL\Type\Definition\InputType $type): ?
* | Enum Value | Mixed |
* | Null Value | null |
*
* @param (ValueNode&Node)|null $valueNode
* @param (ValueNode&Node)|(ConstValueNode&Node)|null $valueNode
* @param array<string, mixed>|null $variables
*
* @throws \Exception
Expand All @@ -2544,7 +2548,7 @@ static function astFromValue($value, GraphQL\Type\Definition\InputType $type): ?
* @api
*/
static function valueFromAST(
?GraphQL\Language\AST\ValueNode $valueNode,
?GraphQL\Language\AST\Node $valueNode,
GraphQL\Type\Definition\Type $type,
?array $variables = null
)
Expand Down
10 changes: 4 additions & 6 deletions src/Executor/Values.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace GraphQL\Executor;

use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumTypeExtensionNode;
Expand All @@ -29,6 +28,7 @@
use GraphQL\Language\AST\SchemaExtensionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Language\AST\ValueNode;
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Language\Printer;
Expand All @@ -42,9 +42,7 @@
use GraphQL\Utils\Value;

/**
* @see ArgumentNode - force IDE import
*
* @phpstan-import-type ArgumentNodeValue from ArgumentNode
* @phpstan-import-type ValueNodeVariants from ValueNode
*/
class Values
{
Expand Down Expand Up @@ -184,7 +182,7 @@ public static function getArgumentValues($def, Node $node, ?array $variableValue
return [];
}

/** @var array<string, ArgumentNodeValue> $argumentValueMap */
/** @var array<string, ValueNodeVariants> $argumentValueMap */
$argumentValueMap = [];

// Might not be defined when an AST from JS is used
Expand All @@ -199,7 +197,7 @@ public static function getArgumentValues($def, Node $node, ?array $variableValue

/**
* @param FieldDefinition|Directive $def
* @param array<string, ArgumentNodeValue> $argumentValueMap
* @param array<string, ValueNodeVariants> $argumentValueMap
* @param array<string, mixed>|null $variableValues
*
* @throws \Exception
Expand Down
4 changes: 2 additions & 2 deletions src/Language/AST/ArgumentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace GraphQL\Language\AST;

/**
* @phpstan-type ArgumentNodeValue VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode
* @phpstan-import-type ValueNodeVariants from ValueNode
*/
class ArgumentNode extends Node
{
public string $kind = NodeKind::ARGUMENT;

/** @phpstan-var ArgumentNodeValue */
/** @var ValueNodeVariants */
public ValueNode $value;

public NameNode $name;
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/BooleanValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class BooleanValueNode extends Node implements ValueNode
class BooleanValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::BOOLEAN;

Expand Down
16 changes: 16 additions & 0 deletions src/Language/AST/ConstArgumentNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace GraphQL\Language\AST;

/**
* @phpstan-import-type ConstValueNodeVariants from ConstValueNode
*/
class ConstArgumentNode extends Node
{
public string $kind = NodeKind::ARGUMENT;

/** @var ConstValueNodeVariants */
public ConstValueNode $value;

public NameNode $name;
}
11 changes: 11 additions & 0 deletions src/Language/AST/ConstListValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace GraphQL\Language\AST;

class ConstListValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::LST;

/** @var NodeList<ConstValueNode&Node> */
public NodeList $values;
}
16 changes: 16 additions & 0 deletions src/Language/AST/ConstObjectFieldNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace GraphQL\Language\AST;

/**
* @phpstan-import-type ConstValueNodeVariants from ConstValueNode
*/
class ConstObjectFieldNode extends Node
{
public string $kind = NodeKind::OBJECT_FIELD;

public NameNode $name;

/** @var ConstValueNodeVariants */
public ConstValueNode $value;
}
11 changes: 11 additions & 0 deletions src/Language/AST/ConstObjectValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace GraphQL\Language\AST;

class ConstObjectValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::OBJECT;

/** @var NodeList<ConstObjectFieldNode> */
public NodeList $fields;
}
10 changes: 10 additions & 0 deletions src/Language/AST/ConstValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace GraphQL\Language\AST;

/**
* @phpstan-type ConstValueNodeVariants NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ConstListValueNode|ConstObjectValueNode
*/
interface ConstValueNode
{
}
2 changes: 1 addition & 1 deletion src/Language/AST/EnumValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class EnumValueNode extends Node implements ValueNode
class EnumValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::ENUM;

Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/FloatValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class FloatValueNode extends Node implements ValueNode
class FloatValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::FLOAT;

Expand Down
5 changes: 4 additions & 1 deletion src/Language/AST/InputValueDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace GraphQL\Language\AST;

/**
* @phpstan-import-type ValueNodeVariants from ValueNode
*/
class InputValueDefinitionNode extends Node
{
public string $kind = NodeKind::INPUT_VALUE_DEFINITION;
Expand All @@ -11,7 +14,7 @@ class InputValueDefinitionNode extends Node
/** @var NamedTypeNode|ListTypeNode|NonNullTypeNode */
public TypeNode $type;

/** @var VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode|null */
/** @var ValueNodeVariants|null */
public ?ValueNode $defaultValue = null;

/** @var NodeList<DirectiveNode> */
Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/IntValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class IntValueNode extends Node implements ValueNode
class IntValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::INT;

Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/ListValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class ListValueNode extends Node implements ValueNode
class ListValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::LST;

Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/NullValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class NullValueNode extends Node implements ValueNode
class NullValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::NULL;
}
5 changes: 4 additions & 1 deletion src/Language/AST/ObjectFieldNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace GraphQL\Language\AST;

/**
* @phpstan-import-type ValueNodeVariants from ValueNode
*/
class ObjectFieldNode extends Node
{
public string $kind = NodeKind::OBJECT_FIELD;

public NameNode $name;

/** @var VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode */
/** @var ValueNodeVariants */
public ValueNode $value;
}
2 changes: 1 addition & 1 deletion src/Language/AST/ObjectValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class ObjectValueNode extends Node implements ValueNode
class ObjectValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::OBJECT;

Expand Down
2 changes: 1 addition & 1 deletion src/Language/AST/StringValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GraphQL\Language\AST;

class StringValueNode extends Node implements ValueNode
class StringValueNode extends Node implements ValueNode, ConstValueNode
{
public string $kind = NodeKind::STRING;

Expand Down
4 changes: 1 addition & 3 deletions src/Language/AST/TypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace GraphQL\Language\AST;

/**
* export type TypeNode = NamedTypeNode
* | ListTypeNode
* | NonNullTypeNode.
* @phpstan-type TypeNodeVariants NamedTypeNode|ListTypeNode|NonNullTypeNode
*/
interface TypeNode
{
Expand Down
10 changes: 1 addition & 9 deletions src/Language/AST/ValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
namespace GraphQL\Language\AST;

/**
export type ValueNode = VariableNode
| NullValueNode
| IntValueNode
| FloatValueNode
| StringValueNode
| BooleanValueNode
| EnumValueNode
| ListValueNode
| ObjectValueNode
* @phpstan-type ValueNodeVariants VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode
*/
interface ValueNode
{
Expand Down
7 changes: 5 additions & 2 deletions src/Language/AST/VariableDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace GraphQL\Language\AST;

/**
* @phpstan-import-type ConstValueNodeVariants from ConstValueNode
*/
class VariableDefinitionNode extends Node implements DefinitionNode
{
public string $kind = NodeKind::VARIABLE_DEFINITION;
Expand All @@ -11,8 +14,8 @@ class VariableDefinitionNode extends Node implements DefinitionNode
/** @var NamedTypeNode|ListTypeNode|NonNullTypeNode */
public TypeNode $type;

/** @var VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode|null */
public ?ValueNode $defaultValue = null;
/** @var ConstValueNodeVariants|null */
public ?ConstValueNode $defaultValue = null;

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