Skip to content

Differentiate between const and non-const AST nodes - #1335

Draft
spawnia wants to merge 14 commits into
masterfrom
const-value-node
Draft

Differentiate between const and non-const AST nodes#1335
spawnia wants to merge 14 commits into
masterfrom
const-value-node

Conversation

@spawnia

@spawnia spawnia commented Mar 3, 2023

Copy link
Copy Markdown
Collaborator
  • Check where ConstValueNode needs to be used

@spawnia spawnia changed the title Add AST type ConstValueNode Differentiate between const and non-const AST nodes Mar 8, 2023
@spawnia spawnia added the breaking change Warrants a major version bump, deferred to the next major release label Mar 8, 2023
@spawnia
spawnia requested a review from simPod March 8, 2023 13:43

@simPod simPod left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I'm not really able to evaluate this addition since I don't have the required gql expertise but I don't see any harm in this either.

Comment thread src/Validator/Rules/UniqueInputFieldNames.php

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a separation between “const” and “non-const” GraphQL AST value nodes (notably for list/object literals), and updates parsing/printing/validation utilities to accept the new const node variants where appropriate.

Changes:

  • Add ConstValueNode and const-specific AST node classes for list/object literals (and fields).
  • Update Parser, Printer, TypeInfo, and several validation rules/utilities to handle const node variants.
  • Update docs/changelog and adjust phpstan type aliases to reflect the new AST model.

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/Executor/VariablesTest.php Test comment/docblock adjustments around variable usage.
src/Executor/Values.php Update phpstan value-node typing import/usage for argument maps.
src/Language/AST/ArgumentNode.php Switch phpstan typing to ValueNodeVariants.
src/Language/AST/BooleanValueNode.php Mark boolean literals as implementing ConstValueNode.
src/Language/AST/ConstArgumentNode.php Add const-only argument AST node type.
src/Language/AST/ConstListValueNode.php Add const-only list literal AST node type.
src/Language/AST/ConstObjectFieldNode.php Add const-only object field AST node type.
src/Language/AST/ConstObjectValueNode.php Add const-only object literal AST node type.
src/Language/AST/ConstValueNode.php Add new marker interface + phpstan const variants alias.
src/Language/AST/EnumValueNode.php Mark enum literals as implementing ConstValueNode.
src/Language/AST/FloatValueNode.php Mark float literals as implementing ConstValueNode.
src/Language/AST/InputValueDefinitionNode.php Adjust phpstan typing comment for default values.
src/Language/AST/IntValueNode.php Mark int literals as implementing ConstValueNode.
src/Language/AST/ListValueNode.php Update list literal node interfaces (const/non-const differentiation).
src/Language/AST/NullValueNode.php Mark null literals as implementing ConstValueNode.
src/Language/AST/ObjectFieldNode.php Refine phpstan typing for object field values.
src/Language/AST/ObjectValueNode.php Update object literal node interfaces (const/non-const differentiation).
src/Language/AST/StringValueNode.php Mark string literals as implementing ConstValueNode.
src/Language/AST/TypeNode.php Replace TS-style union comment with phpstan TypeNodeVariants.
src/Language/AST/TypeNode.php Replace TS-style union comment with phpstan TypeNodeVariants.
src/Language/AST/ValueNode.php Replace TS-style union comment with phpstan ValueNodeVariants.
src/Language/AST/VariableDefinitionNode.php Change variable default values to ?ConstValueNode.
src/Language/Parser.php Emit const-specific list/object/field nodes in const contexts; update return typing.
src/Language/Printer.php Print const list/object/object-field nodes; adjust internal printer signature.
src/Type/Definition/CustomScalarType.php Broaden phpstan types for parseLiteral to accept const value nodes.
src/Type/Definition/LeafType.php Update parseLiteral phpdoc to accept const value nodes.
src/Utils/AST.php Accept/handle const list/object nodes in value coercion helpers; use LeafType.
src/Utils/TypeInfo.php Handle const list/object-field nodes when tracking input type/default stacks.
src/Validator/Rules/UniqueInputFieldNames.php Allow const object-field nodes in the rule visitor.
src/Validator/Rules/ValuesOfCorrectType.php Allow const list/object/object-field nodes in validation visitors.
src/Validator/Rules/VariablesInAllowedPosition.php Type variable default values as ?ConstValueNode.
docs/class-reference.md Update generated API docs for new phpstan type aliases and signatures.
CHANGELOG.md Note the AST const/non-const differentiation under “Unreleased”.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

namespace GraphQL\Language\AST;

class ListValueNode extends Node implements ValueNode
class ListValueNode extends Node implements ValueNode, ConstValueNode
namespace GraphQL\Language\AST;

class ObjectValueNode extends Node implements ValueNode
class ObjectValueNode extends Node implements ValueNode, ConstValueNode
Comment on lines 14 to 18
/** @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;
Comment thread src/Language/Printer.php
* @throws \JsonException
*/
protected function p(?Node $node, bool $isDescription = false): string
protected function p(?Node $node): string
Comment thread src/Language/Parser.php
Comment on lines +1010 to 1014
private function parseConstValue(): ConstValueNode
{
// @phpstan-ignore-next-line return type depends on argument
return $this->parseValueLiteral(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Warrants a major version bump, deferred to the next major release enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants