From 46788f2ddb2b340293c779f6cd2d03fe9d53d1b0 Mon Sep 17 00:00:00 2001 From: Andreas Heiberg Date: Thu, 10 Nov 2016 17:25:54 +0000 Subject: [PATCH] move NodeTypes enum to seperate class --- src/Language/AST/Argument.php | 2 +- src/Language/AST/BooleanValue.php | 4 +- src/Language/AST/Directive.php | 2 +- src/Language/AST/DirectiveDefinition.php | 2 +- src/Language/AST/Document.php | 2 +- src/Language/AST/EnumTypeDefinition.php | 2 +- src/Language/AST/EnumValue.php | 2 +- src/Language/AST/EnumValueDefinition.php | 2 +- src/Language/AST/Field.php | 2 +- src/Language/AST/FieldDefinition.php | 2 +- src/Language/AST/FloatValue.php | 5 +- src/Language/AST/FragmentDefinition.php | 3 +- src/Language/AST/FragmentSpread.php | 2 +- src/Language/AST/InlineFragment.php | 4 +- .../AST/InputObjectTypeDefinition.php | 2 +- src/Language/AST/InputValueDefinition.php | 4 +- src/Language/AST/IntValue.php | 3 +- src/Language/AST/InterfaceTypeDefinition.php | 4 +- src/Language/AST/ListType.php | 2 +- src/Language/AST/ListValue.php | 3 +- src/Language/AST/Name.php | 2 +- src/Language/AST/NamedType.php | 2 +- src/Language/AST/Node.php | 70 --------------- src/Language/AST/NodeType.php | 72 ++++++++++++++++ src/Language/AST/NonNullType.php | 2 +- src/Language/AST/ObjectField.php | 2 +- src/Language/AST/ObjectTypeDefinition.php | 4 +- src/Language/AST/ObjectValue.php | 2 +- src/Language/AST/OperationDefinition.php | 2 +- src/Language/AST/OperationTypeDefinition.php | 2 +- src/Language/AST/ScalarTypeDefinition.php | 3 +- src/Language/AST/SchemaDefinition.php | 2 +- src/Language/AST/SelectionSet.php | 2 +- src/Language/AST/StringValue.php | 5 +- src/Language/AST/TypeExtensionDefinition.php | 2 +- src/Language/AST/UnionTypeDefinition.php | 2 +- src/Language/AST/Variable.php | 2 +- src/Language/AST/VariableDefinition.php | 2 +- src/Language/Printer.php | 73 ++++++++-------- src/Language/Visitor.php | 73 ++++++++-------- tests/Language/ParserTest.php | 65 +++++++------- tests/Language/SchemaParserTest.php | 85 ++++++++++--------- 42 files changed, 264 insertions(+), 266 deletions(-) create mode 100644 src/Language/AST/NodeType.php diff --git a/src/Language/AST/Argument.php b/src/Language/AST/Argument.php index 18579f0..e840ecc 100644 --- a/src/Language/AST/Argument.php +++ b/src/Language/AST/Argument.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class Argument extends Node { - public $kind = Node::ARGUMENT; + public $kind = NodeType::ARGUMENT; /** * @var Value diff --git a/src/Language/AST/BooleanValue.php b/src/Language/AST/BooleanValue.php index cc6a1b3..2e7077f 100644 --- a/src/Language/AST/BooleanValue.php +++ b/src/Language/AST/BooleanValue.php @@ -4,10 +4,10 @@ namespace GraphQL\Language\AST; class BooleanValue extends Node implements Value { - public $kind = Node::BOOLEAN; + public $kind = NodeType::BOOLEAN; /** * @var string */ public $value; -} \ No newline at end of file +} diff --git a/src/Language/AST/Directive.php b/src/Language/AST/Directive.php index 2b23886..b9f9d27 100644 --- a/src/Language/AST/Directive.php +++ b/src/Language/AST/Directive.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class Directive extends Node { - public $kind = Node::DIRECTIVE; + public $kind = NodeType::DIRECTIVE; /** * @var Name diff --git a/src/Language/AST/DirectiveDefinition.php b/src/Language/AST/DirectiveDefinition.php index 81cd696..fb0268d 100644 --- a/src/Language/AST/DirectiveDefinition.php +++ b/src/Language/AST/DirectiveDefinition.php @@ -6,7 +6,7 @@ class DirectiveDefinition extends Node implements TypeSystemDefinition /** * @var string */ - public $kind = Node::DIRECTIVE_DEFINITION; + public $kind = NodeType::DIRECTIVE_DEFINITION; /** * @var Name diff --git a/src/Language/AST/Document.php b/src/Language/AST/Document.php index 800f48b..8e74fd9 100644 --- a/src/Language/AST/Document.php +++ b/src/Language/AST/Document.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class Document extends Node { - public $kind = Node::DOCUMENT; + public $kind = NodeType::DOCUMENT; /** * @var Definition[] diff --git a/src/Language/AST/EnumTypeDefinition.php b/src/Language/AST/EnumTypeDefinition.php index fc1fc39..4a46528 100644 --- a/src/Language/AST/EnumTypeDefinition.php +++ b/src/Language/AST/EnumTypeDefinition.php @@ -6,7 +6,7 @@ class EnumTypeDefinition extends Node implements TypeDefinition /** * @var string */ - public $kind = self::ENUM_TYPE_DEFINITION; + public $kind = NodeType::ENUM_TYPE_DEFINITION; /** * @var Name diff --git a/src/Language/AST/EnumValue.php b/src/Language/AST/EnumValue.php index 7524735..e0c5828 100644 --- a/src/Language/AST/EnumValue.php +++ b/src/Language/AST/EnumValue.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class EnumValue extends Node implements Value { - public $kind = Node::ENUM; + public $kind = NodeType::ENUM; /** * @var string diff --git a/src/Language/AST/EnumValueDefinition.php b/src/Language/AST/EnumValueDefinition.php index d1a751d..8ff4616 100644 --- a/src/Language/AST/EnumValueDefinition.php +++ b/src/Language/AST/EnumValueDefinition.php @@ -6,7 +6,7 @@ class EnumValueDefinition extends Node /** * @var string */ - public $kind = Node::ENUM_VALUE_DEFINITION; + public $kind = NodeType::ENUM_VALUE_DEFINITION; /** * @var Name diff --git a/src/Language/AST/Field.php b/src/Language/AST/Field.php index 7a2f997..8f36475 100644 --- a/src/Language/AST/Field.php +++ b/src/Language/AST/Field.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class Field extends Node implements Selection { - public $kind = Node::FIELD; + public $kind = NodeType::FIELD; /** * @var Name diff --git a/src/Language/AST/FieldDefinition.php b/src/Language/AST/FieldDefinition.php index 13d24b6..01026d8 100644 --- a/src/Language/AST/FieldDefinition.php +++ b/src/Language/AST/FieldDefinition.php @@ -6,7 +6,7 @@ class FieldDefinition extends Node /** * @var string */ - public $kind = Node::FIELD_DEFINITION; + public $kind = NodeType::FIELD_DEFINITION; /** * @var Name diff --git a/src/Language/AST/FloatValue.php b/src/Language/AST/FloatValue.php index b29ffd3..3fa1d42 100644 --- a/src/Language/AST/FloatValue.php +++ b/src/Language/AST/FloatValue.php @@ -1,13 +1,12 @@ diff --git a/src/Language/AST/Name.php b/src/Language/AST/Name.php index e9fdb67..d3d8b8a 100644 --- a/src/Language/AST/Name.php +++ b/src/Language/AST/Name.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class Name extends Node implements Type { - public $kind = Node::NAME; + public $kind = NodeType::NAME; /** * @var string diff --git a/src/Language/AST/NamedType.php b/src/Language/AST/NamedType.php index b524364..a4bc404 100644 --- a/src/Language/AST/NamedType.php +++ b/src/Language/AST/NamedType.php @@ -3,7 +3,7 @@ namespace GraphQL\Language\AST; class NamedType extends Node implements Type { - public $kind = Node::NAMED_TYPE; + public $kind = NodeType::NAMED_TYPE; /** * @var Name diff --git a/src/Language/AST/Node.php b/src/Language/AST/Node.php index 76a5b3b..16056e1 100644 --- a/src/Language/AST/Node.php +++ b/src/Language/AST/Node.php @@ -3,78 +3,8 @@ namespace GraphQL\Language\AST; use GraphQL\Utils; -/** - * Class Node - * @package GraphQL\Language\AST - */ abstract class Node { - // constants from language/kinds.js: - - const NAME = 'Name'; - - // Document - - const DOCUMENT = 'Document'; - const OPERATION_DEFINITION = 'OperationDefinition'; - const VARIABLE_DEFINITION = 'VariableDefinition'; - const VARIABLE = 'Variable'; - const SELECTION_SET = 'SelectionSet'; - const FIELD = 'Field'; - const ARGUMENT = 'Argument'; - - // Fragments - - const FRAGMENT_SPREAD = 'FragmentSpread'; - const INLINE_FRAGMENT = 'InlineFragment'; - const FRAGMENT_DEFINITION = 'FragmentDefinition'; - - // Values - - const INT = 'IntValue'; - const FLOAT = 'FloatValue'; - const STRING = 'StringValue'; - const BOOLEAN = 'BooleanValue'; - const ENUM = 'EnumValue'; - const LST = 'ListValue'; - const OBJECT = 'ObjectValue'; - const OBJECT_FIELD = 'ObjectField'; - - // Directives - - const DIRECTIVE = 'Directive'; - - // Types - - const NAMED_TYPE = 'NamedType'; - const LIST_TYPE = 'ListType'; - const NON_NULL_TYPE = 'NonNullType'; - - // Type System Definitions - - const SCHEMA_DEFINITION = 'SchemaDefinition'; - const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition'; - - // Type Definitions - - const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition'; - const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition'; - const FIELD_DEFINITION = 'FieldDefinition'; - const INPUT_VALUE_DEFINITION = 'InputValueDefinition'; - const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition'; - const UNION_TYPE_DEFINITION = 'UnionTypeDefinition'; - const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition'; - const ENUM_VALUE_DEFINITION = 'EnumValueDefinition'; - const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition'; - - // Type Extensions - - const TYPE_EXTENSION_DEFINITION = 'TypeExtensionDefinition'; - - // Directive Definitions - - const DIRECTIVE_DEFINITION = 'DirectiveDefinition'; - /** type Node = Name | Document diff --git a/src/Language/AST/NodeType.php b/src/Language/AST/NodeType.php new file mode 100644 index 0000000..4b73d8e --- /dev/null +++ b/src/Language/AST/NodeType.php @@ -0,0 +1,72 @@ + diff --git a/src/Language/AST/OperationDefinition.php b/src/Language/AST/OperationDefinition.php index 7e05fec..2e32df5 100644 --- a/src/Language/AST/OperationDefinition.php +++ b/src/Language/AST/OperationDefinition.php @@ -6,7 +6,7 @@ class OperationDefinition extends Node implements Definition, HasSelectionSet /** * @var string */ - public $kind = Node::OPERATION_DEFINITION; + public $kind = NodeType::OPERATION_DEFINITION; /** * @var Name diff --git a/src/Language/AST/OperationTypeDefinition.php b/src/Language/AST/OperationTypeDefinition.php index 39be02d..d269442 100644 --- a/src/Language/AST/OperationTypeDefinition.php +++ b/src/Language/AST/OperationTypeDefinition.php @@ -6,7 +6,7 @@ class OperationTypeDefinition extends Node /** * @var string */ - public $kind = Node::OPERATION_TYPE_DEFINITION; + public $kind = NodeType::OPERATION_TYPE_DEFINITION; /** * One of 'query' | 'mutation' | 'subscription' diff --git a/src/Language/AST/ScalarTypeDefinition.php b/src/Language/AST/ScalarTypeDefinition.php index 82028ee..cfbf889 100644 --- a/src/Language/AST/ScalarTypeDefinition.php +++ b/src/Language/AST/ScalarTypeDefinition.php @@ -1,13 +1,12 @@ diff --git a/src/Language/AST/StringValue.php b/src/Language/AST/StringValue.php index f1e1c1c..dd5212c 100644 --- a/src/Language/AST/StringValue.php +++ b/src/Language/AST/StringValue.php @@ -1,13 +1,12 @@ [ - Node::NAME => function($node) { + NodeType::NAME => function($node) { return '' . $node->value; }, - Node::VARIABLE => function($node) { + NodeType::VARIABLE => function($node) { return '$' . $node->name; }, - Node::DOCUMENT => function(Document $node) { + NodeType::DOCUMENT => function(Document $node) { return $this->join($node->definitions, "\n\n") . "\n"; }, - Node::OPERATION_DEFINITION => function(OperationDefinition $node) { + NodeType::OPERATION_DEFINITION => function(OperationDefinition $node) { $op = $node->operation; $name = $node->name; $varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')'); @@ -65,28 +66,28 @@ class Printer ? $selectionSet : $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' '); }, - Node::VARIABLE_DEFINITION => function(VariableDefinition $node) { + NodeType::VARIABLE_DEFINITION => function(VariableDefinition $node) { return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue); }, - Node::SELECTION_SET => function(SelectionSet $node) { + NodeType::SELECTION_SET => function(SelectionSet $node) { return $this->block($node->selections); }, - Node::FIELD => function(Field $node) { + NodeType::FIELD => function(Field $node) { return $this->join([ $this->wrap('', $node->alias, ': ') . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'), $this->join($node->directives, ' '), $node->selectionSet ], ' '); }, - Node::ARGUMENT => function(Argument $node) { + NodeType::ARGUMENT => function(Argument $node) { return $node->name . ': ' . $node->value; }, // Fragments - Node::FRAGMENT_SPREAD => function(FragmentSpread $node) { + NodeType::FRAGMENT_SPREAD => function(FragmentSpread $node) { return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' ')); }, - Node::INLINE_FRAGMENT => function(InlineFragment $node) { + NodeType::INLINE_FRAGMENT => function(InlineFragment $node) { return $this->join([ "...", $this->wrap('on ', $node->typeCondition), @@ -94,46 +95,46 @@ class Printer $node->selectionSet ], ' '); }, - Node::FRAGMENT_DEFINITION => function(FragmentDefinition $node) { + NodeType::FRAGMENT_DEFINITION => function(FragmentDefinition $node) { return "fragment {$node->name} on {$node->typeCondition} " . $this->wrap('', $this->join($node->directives, ' '), ' ') . $node->selectionSet; }, // Value - Node::INT => function(IntValue $node) {return $node->value;}, - Node::FLOAT => function(FloatValue $node) {return $node->value;}, - Node::STRING => function(StringValue $node) {return json_encode($node->value);}, - Node::BOOLEAN => function(BooleanValue $node) {return $node->value ? 'true' : 'false';}, - Node::ENUM => function(EnumValue $node) {return $node->value;}, - Node::LST => function(ListValue $node) {return '[' . $this->join($node->values, ', ') . ']';}, - Node::OBJECT => function(ObjectValue $node) {return '{' . $this->join($node->fields, ', ') . '}';}, - Node::OBJECT_FIELD => function(ObjectField $node) {return $node->name . ': ' . $node->value;}, + NodeType::INT => function(IntValue $node) {return $node->value;}, + NodeType::FLOAT => function(FloatValue $node) {return $node->value;}, + NodeType::STRING => function(StringValue $node) {return json_encode($node->value);}, + NodeType::BOOLEAN => function(BooleanValue $node) {return $node->value ? 'true' : 'false';}, + NodeType::ENUM => function(EnumValue $node) {return $node->value;}, + NodeType::LST => function(ListValue $node) {return '[' . $this->join($node->values, ', ') . ']';}, + NodeType::OBJECT => function(ObjectValue $node) {return '{' . $this->join($node->fields, ', ') . '}';}, + NodeType::OBJECT_FIELD => function(ObjectField $node) {return $node->name . ': ' . $node->value;}, // Directive - Node::DIRECTIVE => function(Directive $node) { + NodeType::DIRECTIVE => function(Directive $node) { return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'); }, // Type - Node::NAMED_TYPE => function(NamedType $node) {return $node->name;}, - Node::LIST_TYPE => function(ListType $node) {return '[' . $node->type . ']';}, - Node::NON_NULL_TYPE => function(NonNullType $node) {return $node->type . '!';}, + NodeType::NAMED_TYPE => function(NamedType $node) {return $node->name;}, + NodeType::LIST_TYPE => function(ListType $node) {return '[' . $node->type . ']';}, + NodeType::NON_NULL_TYPE => function(NonNullType $node) {return $node->type . '!';}, // Type System Definitions - Node::SCHEMA_DEFINITION => function(SchemaDefinition $def) { + NodeType::SCHEMA_DEFINITION => function(SchemaDefinition $def) { return $this->join([ 'schema', $this->join($def->directives, ' '), $this->block($def->operationTypes) ], ' '); }, - Node::OPERATION_TYPE_DEFINITION => function(OperationTypeDefinition $def) {return $def->operation . ': ' . $def->type;}, + NodeType::OPERATION_TYPE_DEFINITION => function(OperationTypeDefinition $def) {return $def->operation . ': ' . $def->type;}, - Node::SCALAR_TYPE_DEFINITION => function(ScalarTypeDefinition $def) { + NodeType::SCALAR_TYPE_DEFINITION => function(ScalarTypeDefinition $def) { return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' '); }, - Node::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinition $def) { + NodeType::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinition $def) { return $this->join([ 'type', $def->name, @@ -142,20 +143,20 @@ class Printer $this->block($def->fields) ], ' '); }, - Node::FIELD_DEFINITION => function(FieldDefinition $def) { + NodeType::FIELD_DEFINITION => function(FieldDefinition $def) { return $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')') . ': ' . $def->type . $this->wrap(' ', $this->join($def->directives, ' ')); }, - Node::INPUT_VALUE_DEFINITION => function(InputValueDefinition $def) { + NodeType::INPUT_VALUE_DEFINITION => function(InputValueDefinition $def) { return $this->join([ $def->name . ': ' . $def->type, $this->wrap('= ', $def->defaultValue), $this->join($def->directives, ' ') ], ' '); }, - Node::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinition $def) { + NodeType::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinition $def) { return $this->join([ 'interface', $def->name, @@ -163,7 +164,7 @@ class Printer $this->block($def->fields) ], ' '); }, - Node::UNION_TYPE_DEFINITION => function(UnionTypeDefinition $def) { + NodeType::UNION_TYPE_DEFINITION => function(UnionTypeDefinition $def) { return $this->join([ 'union', $def->name, @@ -171,7 +172,7 @@ class Printer '= ' . $this->join($def->types, ' | ') ], ' '); }, - Node::ENUM_TYPE_DEFINITION => function(EnumTypeDefinition $def) { + NodeType::ENUM_TYPE_DEFINITION => function(EnumTypeDefinition $def) { return $this->join([ 'enum', $def->name, @@ -179,13 +180,13 @@ class Printer $this->block($def->values) ], ' '); }, - Node::ENUM_VALUE_DEFINITION => function(EnumValueDefinition $def) { + NodeType::ENUM_VALUE_DEFINITION => function(EnumValueDefinition $def) { return $this->join([ $def->name, $this->join($def->directives, ' ') ], ' '); }, - Node::INPUT_OBJECT_TYPE_DEFINITION => function(InputObjectTypeDefinition $def) { + NodeType::INPUT_OBJECT_TYPE_DEFINITION => function(InputObjectTypeDefinition $def) { return $this->join([ 'input', $def->name, @@ -193,10 +194,10 @@ class Printer $this->block($def->fields) ], ' '); }, - Node::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinition $def) { + NodeType::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinition $def) { return "extend {$def->definition}"; }, - Node::DIRECTIVE_DEFINITION => function(DirectiveDefinition $def) { + NodeType::DIRECTIVE_DEFINITION => function(DirectiveDefinition $def) { return 'directive @' . $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')') . ' on ' . $this->join($def->locations, ' | '); } diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index 040b030..589e720 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -2,6 +2,7 @@ namespace GraphQL\Language; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Utils\TypeInfo; class VisitorOperation @@ -48,44 +49,44 @@ class Visitor } public static $visitorKeys = [ - Node::NAME => [], - Node::DOCUMENT => ['definitions'], - Node::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'], - Node::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue'], - Node::VARIABLE => ['name'], - Node::SELECTION_SET => ['selections'], - Node::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], - Node::ARGUMENT => ['name', 'value'], - Node::FRAGMENT_SPREAD => ['name', 'directives'], - Node::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'], - Node::FRAGMENT_DEFINITION => ['name', 'typeCondition', 'directives', 'selectionSet'], + NodeType::NAME => [], + NodeType::DOCUMENT => ['definitions'], + NodeType::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'], + NodeType::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue'], + NodeType::VARIABLE => ['name'], + NodeType::SELECTION_SET => ['selections'], + NodeType::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], + NodeType::ARGUMENT => ['name', 'value'], + NodeType::FRAGMENT_SPREAD => ['name', 'directives'], + NodeType::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'], + NodeType::FRAGMENT_DEFINITION => ['name', 'typeCondition', 'directives', 'selectionSet'], - Node::INT => [], - Node::FLOAT => [], - Node::STRING => [], - Node::BOOLEAN => [], - Node::ENUM => [], - Node::LST => ['values'], - Node::OBJECT => ['fields'], - Node::OBJECT_FIELD => ['name', 'value'], - Node::DIRECTIVE => ['name', 'arguments'], - Node::NAMED_TYPE => ['name'], - Node::LIST_TYPE => ['type'], - Node::NON_NULL_TYPE => ['type'], + NodeType::INT => [], + NodeType::FLOAT => [], + NodeType::STRING => [], + NodeType::BOOLEAN => [], + NodeType::ENUM => [], + NodeType::LST => ['values'], + NodeType::OBJECT => ['fields'], + NodeType::OBJECT_FIELD => ['name', 'value'], + NodeType::DIRECTIVE => ['name', 'arguments'], + NodeType::NAMED_TYPE => ['name'], + NodeType::LIST_TYPE => ['type'], + NodeType::NON_NULL_TYPE => ['type'], - Node::SCHEMA_DEFINITION => ['directives', 'operationTypes'], - Node::OPERATION_TYPE_DEFINITION => ['type'], - Node::SCALAR_TYPE_DEFINITION => ['name', 'directives'], - Node::OBJECT_TYPE_DEFINITION => ['name', 'interfaces', 'directives', 'fields'], - Node::FIELD_DEFINITION => ['name', 'arguments', 'type', 'directives'], - Node::INPUT_VALUE_DEFINITION => ['name', 'type', 'defaultValue', 'directives'], - Node::INTERFACE_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ], - Node::UNION_TYPE_DEFINITION => [ 'name', 'directives', 'types' ], - Node::ENUM_TYPE_DEFINITION => [ 'name', 'directives', 'values' ], - Node::ENUM_VALUE_DEFINITION => [ 'name', 'directives' ], - Node::INPUT_OBJECT_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ], - Node::TYPE_EXTENSION_DEFINITION => [ 'definition' ], - Node::DIRECTIVE_DEFINITION => [ 'name', 'arguments', 'locations' ] + NodeType::SCHEMA_DEFINITION => ['directives', 'operationTypes'], + NodeType::OPERATION_TYPE_DEFINITION => ['type'], + NodeType::SCALAR_TYPE_DEFINITION => ['name', 'directives'], + NodeType::OBJECT_TYPE_DEFINITION => ['name', 'interfaces', 'directives', 'fields'], + NodeType::FIELD_DEFINITION => ['name', 'arguments', 'type', 'directives'], + NodeType::INPUT_VALUE_DEFINITION => ['name', 'type', 'defaultValue', 'directives'], + NodeType::INTERFACE_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ], + NodeType::UNION_TYPE_DEFINITION => [ 'name', 'directives', 'types' ], + NodeType::ENUM_TYPE_DEFINITION => [ 'name', 'directives', 'values' ], + NodeType::ENUM_VALUE_DEFINITION => [ 'name', 'directives' ], + NodeType::INPUT_OBJECT_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ], + NodeType::TYPE_EXTENSION_DEFINITION => [ 'definition' ], + NodeType::DIRECTIVE_DEFINITION => [ 'name', 'arguments', 'locations' ] ]; /** diff --git a/tests/Language/ParserTest.php b/tests/Language/ParserTest.php index fbc93b3..6977ba4 100644 --- a/tests/Language/ParserTest.php +++ b/tests/Language/ParserTest.php @@ -2,13 +2,10 @@ namespace GraphQL\Tests\Language; use GraphQL\Language\AST\Argument; -use GraphQL\Language\AST\Document; use GraphQL\Language\AST\Field; -use GraphQL\Language\AST\IntValue; -use GraphQL\Language\AST\Location; use GraphQL\Language\AST\Name; use GraphQL\Language\AST\Node; -use GraphQL\Language\AST\OperationDefinition; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\SelectionSet; use GraphQL\Language\AST\StringValue; use GraphQL\Language\Parser; @@ -270,39 +267,39 @@ fragment $fragmentName on Type { }; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'loc' => $loc(0, 41), 'definitions' => [ [ - 'kind' => Node::OPERATION_DEFINITION, + 'kind' => NodeType::OPERATION_DEFINITION, 'loc' => $loc(0, 40), 'operation' => 'query', 'name' => null, 'variableDefinitions' => null, 'directives' => [], 'selectionSet' => [ - 'kind' => Node::SELECTION_SET, + 'kind' => NodeType::SELECTION_SET, 'loc' => $loc(0, 40), 'selections' => [ [ - 'kind' => Node::FIELD, + 'kind' => NodeType::FIELD, 'loc' => $loc(4, 38), 'alias' => null, 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => $loc(4, 8), 'value' => 'node' ], 'arguments' => [ [ - 'kind' => Node::ARGUMENT, + 'kind' => NodeType::ARGUMENT, 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => $loc(9, 11), 'value' => 'id' ], 'value' => [ - 'kind' => Node::INT, + 'kind' => NodeType::INT, 'loc' => $loc(13, 14), 'value' => '4' ], @@ -311,15 +308,15 @@ fragment $fragmentName on Type { ], 'directives' => [], 'selectionSet' => [ - 'kind' => Node::SELECTION_SET, + 'kind' => NodeType::SELECTION_SET, 'loc' => $loc(16, 38), 'selections' => [ [ - 'kind' => Node::FIELD, + 'kind' => NodeType::FIELD, 'loc' => $loc(22, 24), 'alias' => null, 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => $loc(22, 24), 'value' => 'id' ], @@ -328,11 +325,11 @@ fragment $fragmentName on Type { 'selectionSet' => null ], [ - 'kind' => Node::FIELD, + 'kind' => NodeType::FIELD, 'loc' => $loc(30, 34), 'alias' => null, 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => $loc(30, 34), 'value' => 'name' ], @@ -402,16 +399,16 @@ fragment $fragmentName on Type { public function testParsesListValues() { $this->assertEquals([ - 'kind' => Node::LST, + 'kind' => NodeType::LST, 'loc' => ['start' => 0, 'end' => 11], 'values' => [ [ - 'kind' => Node::INT, + 'kind' => NodeType::INT, 'loc' => ['start' => 1, 'end' => 4], 'value' => '123' ], [ - 'kind' => Node::STRING, + 'kind' => NodeType::STRING, 'loc' => ['start' => 5, 'end' => 10], 'value' => 'abc' ] @@ -427,10 +424,10 @@ fragment $fragmentName on Type { public function testParsesWellKnownTypes() { $this->assertEquals([ - 'kind' => Node::NAMED_TYPE, + 'kind' => NodeType::NAMED_TYPE, 'loc' => ['start' => 0, 'end' => 6], 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => ['start' => 0, 'end' => 6], 'value' => 'String' ] @@ -443,10 +440,10 @@ fragment $fragmentName on Type { public function testParsesCustomTypes() { $this->assertEquals([ - 'kind' => Node::NAMED_TYPE, + 'kind' => NodeType::NAMED_TYPE, 'loc' => ['start' => 0, 'end' => 6], 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => ['start' => 0, 'end' => 6], 'value' => 'MyType' ] @@ -459,13 +456,13 @@ fragment $fragmentName on Type { public function testParsesListTypes() { $this->assertEquals([ - 'kind' => Node::LIST_TYPE, + 'kind' => NodeType::LIST_TYPE, 'loc' => ['start' => 0, 'end' => 8], 'type' => [ - 'kind' => Node::NAMED_TYPE, + 'kind' => NodeType::NAMED_TYPE, 'loc' => ['start' => 1, 'end' => 7], 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => ['start' => 1, 'end' => 7], 'value' => 'MyType' ] @@ -479,13 +476,13 @@ fragment $fragmentName on Type { public function testParsesNonNullTypes() { $this->assertEquals([ - 'kind' => Node::NON_NULL_TYPE, + 'kind' => NodeType::NON_NULL_TYPE, 'loc' => ['start' => 0, 'end' => 7], 'type' => [ - 'kind' => Node::NAMED_TYPE, + 'kind' => NodeType::NAMED_TYPE, 'loc' => ['start' => 0, 'end' => 6], 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => ['start' => 0, 'end' => 6], 'value' => 'MyType' ] @@ -499,16 +496,16 @@ fragment $fragmentName on Type { public function testParsesNestedTypes() { $this->assertEquals([ - 'kind' => Node::LIST_TYPE, + 'kind' => NodeType::LIST_TYPE, 'loc' => ['start' => 0, 'end' => 9], 'type' => [ - 'kind' => Node::NON_NULL_TYPE, + 'kind' => NodeType::NON_NULL_TYPE, 'loc' => ['start' => 1, 'end' => 8], 'type' => [ - 'kind' => Node::NAMED_TYPE, + 'kind' => NodeType::NAMED_TYPE, 'loc' => ['start' => 1, 'end' => 7], 'name' => [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'loc' => ['start' => 1, 'end' => 7], 'value' => 'MyType' ] diff --git a/tests/Language/SchemaParserTest.php b/tests/Language/SchemaParserTest.php index 2f64594..f6baf47 100644 --- a/tests/Language/SchemaParserTest.php +++ b/tests/Language/SchemaParserTest.php @@ -14,6 +14,7 @@ use GraphQL\Language\AST\Location; use GraphQL\Language\AST\Name; use GraphQL\Language\AST\NamedType; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\NonNullType; use GraphQL\Language\AST\ObjectTypeDefinition; use GraphQL\Language\AST\ScalarTypeDefinition; @@ -39,10 +40,10 @@ type Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'interfaces' => [], 'directives' => [], @@ -76,12 +77,12 @@ extend type Hello { return TestUtils::locArray($start, $end); }; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::TYPE_EXTENSION_DEFINITION, + 'kind' => NodeType::TYPE_EXTENSION_DEFINITION, 'definition' => [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(13, 18)), 'interfaces' => [], 'directives' => [], @@ -117,10 +118,10 @@ type Hello { $doc = Parser::parse($body); $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6,11)), 'interfaces' => [], 'directives' => [], @@ -128,7 +129,7 @@ type Hello { $this->fieldNode( $this->nameNode('world', $loc(16, 21)), [ - 'kind' => Node::NON_NULL_TYPE, + 'kind' => NodeType::NON_NULL_TYPE, 'type' => $this->typeNode('String', $loc(23, 29)), 'loc' => $loc(23, 30) ], @@ -154,10 +155,10 @@ type Hello { $doc = Parser::parse($body); $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(5, 10)), 'interfaces' => [ $this->typeNode('World', $loc(22, 27)) @@ -183,10 +184,10 @@ type Hello { $doc = Parser::parse($body); $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(5, 10)), 'interfaces' => [ $this->typeNode('Wo', $loc(22,24)), @@ -213,10 +214,10 @@ type Hello { $doc = Parser::parse($body); $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::ENUM_TYPE_DEFINITION, + 'kind' => NodeType::ENUM_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(5, 10)), 'directives' => [], 'values' => [$this->enumValueNode('WORLD', $loc(13, 18))], @@ -239,10 +240,10 @@ type Hello { $doc = Parser::parse($body); $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::ENUM_TYPE_DEFINITION, + 'kind' => NodeType::ENUM_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(5, 10)), 'directives' => [], 'values' => [ @@ -271,10 +272,10 @@ interface Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::INTERFACE_TYPE_DEFINITION, + 'kind' => NodeType::INTERFACE_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(11, 16)), 'directives' => [], 'fields' => [ @@ -305,10 +306,10 @@ type Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'interfaces' => [], 'directives' => [], @@ -349,10 +350,10 @@ type Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'interfaces' => [], 'directives' => [], @@ -364,7 +365,7 @@ type Hello { $this->inputValueNode( $this->nameNode('flag', $loc(22, 26)), $this->typeNode('Boolean', $loc(28, 35)), - ['kind' => Node::BOOLEAN, 'value' => true, 'loc' => $loc(38, 42)], + ['kind' => NodeType::BOOLEAN, 'value' => true, 'loc' => $loc(38, 42)], $loc(22, 42) ) ], @@ -392,10 +393,10 @@ type Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'interfaces' => [], 'directives' => [], @@ -406,7 +407,7 @@ type Hello { [ $this->inputValueNode( $this->nameNode('things', $loc(22,28)), - ['kind' => Node::LIST_TYPE, 'type' => $this->typeNode('String', $loc(31, 37)), 'loc' => $loc(30, 38)], + ['kind' => NodeType::LIST_TYPE, 'type' => $this->typeNode('String', $loc(31, 37)), 'loc' => $loc(30, 38)], null, $loc(22, 38) ) @@ -436,10 +437,10 @@ type Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'interfaces' => [], 'directives' => [], @@ -482,10 +483,10 @@ type Hello { $doc = Parser::parse($body); $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::UNION_TYPE_DEFINITION, + 'kind' => NodeType::UNION_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'directives' => [], 'types' => [$this->typeNode('World', $loc(14, 19))], @@ -508,10 +509,10 @@ type Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::UNION_TYPE_DEFINITION, + 'kind' => NodeType::UNION_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(6, 11)), 'directives' => [], 'types' => [ @@ -535,10 +536,10 @@ type Hello { $doc = Parser::parse($body); $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::SCALAR_TYPE_DEFINITION, + 'kind' => NodeType::SCALAR_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(7, 12)), 'directives' => [], 'loc' => $loc(0, 12) @@ -562,10 +563,10 @@ input Hello { $loc = function($start, $end) {return TestUtils::locArray($start, $end);}; $expected = [ - 'kind' => Node::DOCUMENT, + 'kind' => NodeType::DOCUMENT, 'definitions' => [ [ - 'kind' => Node::INPUT_OBJECT_TYPE_DEFINITION, + 'kind' => NodeType::INPUT_OBJECT_TYPE_DEFINITION, 'name' => $this->nameNode('Hello', $loc(7, 12)), 'directives' => [], 'fields' => [ @@ -600,8 +601,8 @@ input Hello { private function typeNode($name, $loc) { return [ - 'kind' => Node::NAMED_TYPE, - 'name' => ['kind' => Node::NAME, 'value' => $name, 'loc' => $loc], + 'kind' => NodeType::NAMED_TYPE, + 'name' => ['kind' => NodeType::NAME, 'value' => $name, 'loc' => $loc], 'loc' => $loc ]; } @@ -609,7 +610,7 @@ input Hello { private function nameNode($name, $loc) { return [ - 'kind' => Node::NAME, + 'kind' => NodeType::NAME, 'value' => $name, 'loc' => $loc ]; @@ -623,7 +624,7 @@ input Hello { private function fieldNodeWithArgs($name, $type, $args, $loc) { return [ - 'kind' => Node::FIELD_DEFINITION, + 'kind' => NodeType::FIELD_DEFINITION, 'name' => $name, 'arguments' => $args, 'type' => $type, @@ -635,7 +636,7 @@ input Hello { private function enumValueNode($name, $loc) { return [ - 'kind' => Node::ENUM_VALUE_DEFINITION, + 'kind' => NodeType::ENUM_VALUE_DEFINITION, 'name' => $this->nameNode($name, $loc), 'directives' => [], 'loc' => $loc @@ -645,7 +646,7 @@ input Hello { private function inputValueNode($name, $type, $defaultValue, $loc) { return [ - 'kind' => Node::INPUT_VALUE_DEFINITION, + 'kind' => NodeType::INPUT_VALUE_DEFINITION, 'name' => $name, 'type' => $type, 'defaultValue' => $defaultValue,