From d8ca5f4183d4705f8612ad4def07a2d63f31a7df Mon Sep 17 00:00:00 2001 From: Andreas Heiberg Date: Thu, 10 Nov 2016 22:20:19 +0000 Subject: [PATCH] move to NodeType enum --- src/Executor/Executor.php | 11 ++++++----- src/Validator/DocumentValidator.php | 3 ++- src/Validator/Rules/AbstractQuerySecurity.php | 7 ++++--- src/Validator/Rules/ArgumentsOfCorrectType.php | 3 ++- src/Validator/Rules/DefaultValuesOfCorrectType.php | 7 ++++--- src/Validator/Rules/FieldsOnCorrectType.php | 3 ++- src/Validator/Rules/FragmentsOnCompositeTypes.php | 5 +++-- src/Validator/Rules/KnownArgumentNames.php | 7 ++++--- src/Validator/Rules/KnownDirectives.php | 13 +++++++------ src/Validator/Rules/KnownFragmentNames.php | 3 ++- src/Validator/Rules/KnownTypeNames.php | 10 +++++----- src/Validator/Rules/LoneAnonymousOperation.php | 7 ++++--- src/Validator/Rules/NoFragmentCycles.php | 5 +++-- src/Validator/Rules/NoUndefinedVariables.php | 5 +++-- src/Validator/Rules/NoUnusedFragments.php | 7 ++++--- src/Validator/Rules/NoUnusedVariables.php | 5 +++-- .../Rules/OverlappingFieldsCanBeMerged.php | 9 +++++---- src/Validator/Rules/PossibleFragmentSpreads.php | 5 +++-- src/Validator/Rules/ProvidedNonNullArguments.php | 5 +++-- src/Validator/Rules/QueryComplexity.php | 13 +++++++------ src/Validator/Rules/QueryDepth.php | 8 ++++---- src/Validator/Rules/ScalarLeafs.php | 3 ++- src/Validator/Rules/UniqueArgumentNames.php | 7 ++++--- src/Validator/Rules/UniqueFragmentNames.php | 5 +++-- src/Validator/Rules/UniqueInputFieldNames.php | 5 +++-- src/Validator/Rules/UniqueOperationNames.php | 5 +++-- src/Validator/Rules/UniqueVariableNames.php | 5 +++-- src/Validator/Rules/VariablesAreInputTypes.php | 3 ++- src/Validator/Rules/VariablesInAllowedPosition.php | 5 +++-- src/Validator/ValidationContext.php | 9 +++++---- tests/Validator/TestCase.php | 7 +++++-- 31 files changed, 113 insertions(+), 82 deletions(-) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 7722530..f81212a 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -7,6 +7,7 @@ use GraphQL\Language\AST\Document; use GraphQL\Language\AST\Field; use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\SelectionSet; use GraphQL\Schema; @@ -119,7 +120,7 @@ class Executor foreach ($documentAst->definitions as $definition) { switch ($definition->kind) { - case Node::OPERATION_DEFINITION: + case NodeType::OPERATION_DEFINITION: if (!$operationName && $operation) { throw new Error( 'Must provide operation name if query contains multiple operations.' @@ -130,7 +131,7 @@ class Executor $operation = $definition; } break; - case Node::FRAGMENT_DEFINITION: + case NodeType::FRAGMENT_DEFINITION: $fragments[$definition->name->value] = $definition; break; default: @@ -271,7 +272,7 @@ class Executor { foreach ($selectionSet->selections as $selection) { switch ($selection->kind) { - case Node::FIELD: + case NodeType::FIELD: if (!self::shouldIncludeNode($exeContext, $selection->directives)) { continue; } @@ -281,7 +282,7 @@ class Executor } $fields[$name][] = $selection; break; - case Node::INLINE_FRAGMENT: + case NodeType::INLINE_FRAGMENT: if (!self::shouldIncludeNode($exeContext, $selection->directives) || !self::doesFragmentConditionMatch($exeContext, $selection, $runtimeType) ) { @@ -295,7 +296,7 @@ class Executor $visitedFragmentNames ); break; - case Node::FRAGMENT_SPREAD: + case NodeType::FRAGMENT_SPREAD: $fragName = $selection->name->value; if (!empty($visitedFragmentNames[$fragName]) || !self::shouldIncludeNode($exeContext, $selection->directives)) { continue; diff --git a/src/Validator/DocumentValidator.php b/src/Validator/DocumentValidator.php index c55a5b0..a890838 100644 --- a/src/Validator/DocumentValidator.php +++ b/src/Validator/DocumentValidator.php @@ -7,6 +7,7 @@ use GraphQL\Language\AST\ListValue; use GraphQL\Language\AST\Document; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\Value; use GraphQL\Language\AST\Variable; use GraphQL\Language\Printer; @@ -195,7 +196,7 @@ class DocumentValidator // Input objects check each defined field and look for undefined fields. if ($type instanceof InputObjectType) { - if ($valueAST->kind !== Node::OBJECT) { + if ($valueAST->kind !== NodeType::OBJECT) { return [ "Expected \"{$type->name}\", found not an object." ]; } diff --git a/src/Validator/Rules/AbstractQuerySecurity.php b/src/Validator/Rules/AbstractQuerySecurity.php index 7c61e58..1d804e6 100644 --- a/src/Validator/Rules/AbstractQuerySecurity.php +++ b/src/Validator/Rules/AbstractQuerySecurity.php @@ -7,6 +7,7 @@ use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\SelectionSet; use GraphQL\Type\Definition\Type; use GraphQL\Type\Introspection; @@ -99,7 +100,7 @@ abstract class AbstractQuerySecurity foreach ($selectionSet->selections as $selection) { switch ($selection->kind) { - case Node::FIELD: + case NodeType::FIELD: /* @var Field $selection */ $fieldName = $selection->name->value; $fieldDef = null; @@ -126,7 +127,7 @@ abstract class AbstractQuerySecurity // create field context $_astAndDefs[$responseName][] = [$selection, $fieldDef]; break; - case Node::INLINE_FRAGMENT: + case NodeType::INLINE_FRAGMENT: /* @var InlineFragment $selection */ $_astAndDefs = $this->collectFieldASTsAndDefs( $context, @@ -136,7 +137,7 @@ abstract class AbstractQuerySecurity $_astAndDefs ); break; - case Node::FRAGMENT_SPREAD: + case NodeType::FRAGMENT_SPREAD: /* @var FragmentSpread $selection */ $fragName = $selection->name->value; diff --git a/src/Validator/Rules/ArgumentsOfCorrectType.php b/src/Validator/Rules/ArgumentsOfCorrectType.php index b89d26c..feead4a 100644 --- a/src/Validator/Rules/ArgumentsOfCorrectType.php +++ b/src/Validator/Rules/ArgumentsOfCorrectType.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\Argument; use GraphQL\Language\AST\Field; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Printer; use GraphQL\Language\Visitor; use GraphQL\Type\Definition\NonNull; @@ -25,7 +26,7 @@ class ArgumentsOfCorrectType public function __invoke(ValidationContext $context) { return [ - Node::ARGUMENT => function(Argument $argAST) use ($context) { + NodeType::ARGUMENT => function(Argument $argAST) use ($context) { $argDef = $context->getArgument(); if ($argDef) { $errors = DocumentValidator::isValidLiteralValue($argDef->getType(), $argAST->value); diff --git a/src/Validator/Rules/DefaultValuesOfCorrectType.php b/src/Validator/Rules/DefaultValuesOfCorrectType.php index a6978ed..012ff7b 100644 --- a/src/Validator/Rules/DefaultValuesOfCorrectType.php +++ b/src/Validator/Rules/DefaultValuesOfCorrectType.php @@ -4,6 +4,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\VariableDefinition; use GraphQL\Language\Printer; use GraphQL\Language\Visitor; @@ -29,7 +30,7 @@ class DefaultValuesOfCorrectType public function __invoke(ValidationContext $context) { return [ - Node::VARIABLE_DEFINITION => function(VariableDefinition $varDefAST) use ($context) { + NodeType::VARIABLE_DEFINITION => function(VariableDefinition $varDefAST) use ($context) { $name = $varDefAST->variable->name->value; $defaultValue = $varDefAST->defaultValue; $type = $context->getInputType(); @@ -51,8 +52,8 @@ class DefaultValuesOfCorrectType } return Visitor::skipNode(); }, - Node::SELECTION_SET => function() {return Visitor::skipNode();}, - Node::FRAGMENT_DEFINITION => function() {return Visitor::skipNode();} + NodeType::SELECTION_SET => function() {return Visitor::skipNode();}, + NodeType::FRAGMENT_DEFINITION => function() {return Visitor::skipNode();} ]; } } diff --git a/src/Validator/Rules/FieldsOnCorrectType.php b/src/Validator/Rules/FieldsOnCorrectType.php index aed5acc..dc30662 100644 --- a/src/Validator/Rules/FieldsOnCorrectType.php +++ b/src/Validator/Rules/FieldsOnCorrectType.php @@ -5,6 +5,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Field; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Schema; use GraphQL\Type\Definition\AbstractType; use GraphQL\Utils; @@ -36,7 +37,7 @@ class FieldsOnCorrectType public function __invoke(ValidationContext $context) { return [ - Node::FIELD => function(Field $node) use ($context) { + NodeType::FIELD => function(Field $node) use ($context) { $type = $context->getParentType(); if ($type) { $fieldDef = $context->getFieldDef(); diff --git a/src/Validator/Rules/FragmentsOnCompositeTypes.php b/src/Validator/Rules/FragmentsOnCompositeTypes.php index cdb6502..e34cd4b 100644 --- a/src/Validator/Rules/FragmentsOnCompositeTypes.php +++ b/src/Validator/Rules/FragmentsOnCompositeTypes.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Printer; use GraphQL\Type\Definition\CompositeType; use GraphQL\Type\Definition\Type; @@ -27,7 +28,7 @@ class FragmentsOnCompositeTypes public function __invoke(ValidationContext $context) { return [ - Node::INLINE_FRAGMENT => function(InlineFragment $node) use ($context) { + NodeType::INLINE_FRAGMENT => function(InlineFragment $node) use ($context) { $type = $context->getType(); if ($node->typeCondition && $type && !Type::isCompositeType($type)) { @@ -37,7 +38,7 @@ class FragmentsOnCompositeTypes )); } }, - Node::FRAGMENT_DEFINITION => function(FragmentDefinition $node) use ($context) { + NodeType::FRAGMENT_DEFINITION => function(FragmentDefinition $node) use ($context) { $type = $context->getType(); if ($type && !Type::isCompositeType($type)) { diff --git a/src/Validator/Rules/KnownArgumentNames.php b/src/Validator/Rules/KnownArgumentNames.php index 963eda6..d52f1a7 100644 --- a/src/Validator/Rules/KnownArgumentNames.php +++ b/src/Validator/Rules/KnownArgumentNames.php @@ -5,6 +5,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Argument; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Utils; use GraphQL\Validator\Messages; use GraphQL\Validator\ValidationContext; @@ -24,9 +25,9 @@ class KnownArgumentNames public function __invoke(ValidationContext $context) { return [ - Node::ARGUMENT => function(Argument $node, $key, $parent, $path, $ancestors) use ($context) { + NodeType::ARGUMENT => function(Argument $node, $key, $parent, $path, $ancestors) use ($context) { $argumentOf = $ancestors[count($ancestors) - 1]; - if ($argumentOf->kind === Node::FIELD) { + if ($argumentOf->kind === NodeType::FIELD) { $fieldDef = $context->getFieldDef(); if ($fieldDef) { @@ -46,7 +47,7 @@ class KnownArgumentNames )); } } - } else if ($argumentOf->kind === Node::DIRECTIVE) { + } else if ($argumentOf->kind === NodeType::DIRECTIVE) { $directive = $context->getDirective(); if ($directive) { $directiveArgDef = null; diff --git a/src/Validator/Rules/KnownDirectives.php b/src/Validator/Rules/KnownDirectives.php index 3c544c8..1a44d28 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -9,6 +9,7 @@ use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Validator\Messages; use GraphQL\Validator\ValidationContext; @@ -29,7 +30,7 @@ class KnownDirectives public function __invoke(ValidationContext $context) { return [ - Node::DIRECTIVE => function (Directive $node, $key, $parent, $path, $ancestors) use ($context) { + NodeType::DIRECTIVE => function (Directive $node, $key, $parent, $path, $ancestors) use ($context) { $directiveDef = null; foreach ($context->getSchema()->getDirectives() as $def) { if ($def->name === $node->name->value) { @@ -66,17 +67,17 @@ class KnownDirectives private function getLocationForAppliedNode(Node $appliedTo) { switch ($appliedTo->kind) { - case Node::OPERATION_DEFINITION: + case NodeType::OPERATION_DEFINITION: switch ($appliedTo->operation) { case 'query': return DirectiveDef::LOCATION_QUERY; case 'mutation': return DirectiveDef::LOCATION_MUTATION; case 'subscription': return DirectiveDef::LOCATION_SUBSCRIPTION; } break; - case Node::FIELD: return DirectiveDef::LOCATION_FIELD; - case Node::FRAGMENT_SPREAD: return DirectiveDef::LOCATION_FRAGMENT_SPREAD; - case Node::INLINE_FRAGMENT: return DirectiveDef::LOCATION_INLINE_FRAGMENT; - case Node::FRAGMENT_DEFINITION: return DirectiveDef::LOCATION_FRAGMENT_DEFINITION; + case NodeType::FIELD: return DirectiveDef::LOCATION_FIELD; + case NodeType::FRAGMENT_SPREAD: return DirectiveDef::LOCATION_FRAGMENT_SPREAD; + case NodeType::INLINE_FRAGMENT: return DirectiveDef::LOCATION_INLINE_FRAGMENT; + case NodeType::FRAGMENT_DEFINITION: return DirectiveDef::LOCATION_FRAGMENT_DEFINITION; } } } diff --git a/src/Validator/Rules/KnownFragmentNames.php b/src/Validator/Rules/KnownFragmentNames.php index 9d238ed..ca616b4 100644 --- a/src/Validator/Rules/KnownFragmentNames.php +++ b/src/Validator/Rules/KnownFragmentNames.php @@ -5,6 +5,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Validator\ValidationContext; class KnownFragmentNames @@ -17,7 +18,7 @@ class KnownFragmentNames public function __invoke(ValidationContext $context) { return [ - Node::FRAGMENT_SPREAD => function(FragmentSpread $node) use ($context) { + NodeType::FRAGMENT_SPREAD => function(FragmentSpread $node) use ($context) { $fragmentName = $node->name->value; $fragment = $context->getFragment($fragmentName); if (!$fragment) { diff --git a/src/Validator/Rules/KnownTypeNames.php b/src/Validator/Rules/KnownTypeNames.php index affe328..7683ae5 100644 --- a/src/Validator/Rules/KnownTypeNames.php +++ b/src/Validator/Rules/KnownTypeNames.php @@ -22,12 +22,12 @@ class KnownTypeNames $skip = function() {return Visitor::skipNode();}; return [ - Node::OBJECT_TYPE_DEFINITION => $skip, - Node::INTERFACE_TYPE_DEFINITION => $skip, - Node::UNION_TYPE_DEFINITION => $skip, - Node::INPUT_OBJECT_TYPE_DEFINITION => $skip, + NodeType::OBJECT_TYPE_DEFINITION => $skip, + NodeType::INTERFACE_TYPE_DEFINITION => $skip, + NodeType::UNION_TYPE_DEFINITION => $skip, + NodeType::INPUT_OBJECT_TYPE_DEFINITION => $skip, - Node::NAMED_TYPE => function(NamedType $node, $key) use ($context) { + NodeType::NAMED_TYPE => function(NamedType $node, $key) use ($context) { $typeName = $node->name->value; $type = $context->getSchema()->getType($typeName); if (!$type) { diff --git a/src/Validator/Rules/LoneAnonymousOperation.php b/src/Validator/Rules/LoneAnonymousOperation.php index df8620f..9c4afa5 100644 --- a/src/Validator/Rules/LoneAnonymousOperation.php +++ b/src/Validator/Rules/LoneAnonymousOperation.php @@ -4,6 +4,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Document; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Utils; use GraphQL\Validator\ValidationContext; @@ -25,16 +26,16 @@ class LoneAnonymousOperation { $operationCount = 0; return [ - Node::DOCUMENT => function(Document $node) use (&$operationCount) { + NodeType::DOCUMENT => function(Document $node) use (&$operationCount) { $tmp = Utils::filter( $node->definitions, function ($definition) { - return $definition->kind === Node::OPERATION_DEFINITION; + return $definition->kind === NodeType::OPERATION_DEFINITION; } ); $operationCount = count($tmp); }, - Node::OPERATION_DEFINITION => function(OperationDefinition $node) use (&$operationCount, $context) { + NodeType::OPERATION_DEFINITION => function(OperationDefinition $node) use (&$operationCount, $context) { if (!$node->name && $operationCount > 1) { $context->reportError( new Error(self::anonOperationNotAloneMessage(), [$node]) diff --git a/src/Validator/Rules/NoFragmentCycles.php b/src/Validator/Rules/NoFragmentCycles.php index 56bc585..6911cd8 100644 --- a/src/Validator/Rules/NoFragmentCycles.php +++ b/src/Validator/Rules/NoFragmentCycles.php @@ -13,6 +13,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Visitor; use GraphQL\Utils; use GraphQL\Validator\ValidationContext; @@ -44,10 +45,10 @@ class NoFragmentCycles $this->spreadPathIndexByName = []; return [ - Node::OPERATION_DEFINITION => function () { + NodeType::OPERATION_DEFINITION => function () { return Visitor::skipNode(); }, - Node::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use ($context) { + NodeType::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use ($context) { if (!isset($this->visitedFrags[$node->name->value])) { $this->detectCycleRecursive($node, $context); } diff --git a/src/Validator/Rules/NoUndefinedVariables.php b/src/Validator/Rules/NoUndefinedVariables.php index 66da7d1..0e6a69b 100644 --- a/src/Validator/Rules/NoUndefinedVariables.php +++ b/src/Validator/Rules/NoUndefinedVariables.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\Variable; use GraphQL\Language\AST\VariableDefinition; @@ -35,7 +36,7 @@ class NoUndefinedVariables $variableNameDefined = []; return [ - Node::OPERATION_DEFINITION => [ + NodeType::OPERATION_DEFINITION => [ 'enter' => function() use (&$variableNameDefined) { $variableNameDefined = []; }, @@ -58,7 +59,7 @@ class NoUndefinedVariables } } ], - Node::VARIABLE_DEFINITION => function(VariableDefinition $def) use (&$variableNameDefined) { + NodeType::VARIABLE_DEFINITION => function(VariableDefinition $def) use (&$variableNameDefined) { $variableNameDefined[$def->variable->name->value] = true; } ]; diff --git a/src/Validator/Rules/NoUnusedFragments.php b/src/Validator/Rules/NoUnusedFragments.php index a944da0..fe8e915 100644 --- a/src/Validator/Rules/NoUnusedFragments.php +++ b/src/Validator/Rules/NoUnusedFragments.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Visitor; use GraphQL\Validator\Messages; use GraphQL\Validator\ValidationContext; @@ -27,15 +28,15 @@ class NoUnusedFragments $this->fragmentDefs = []; return [ - Node::OPERATION_DEFINITION => function($node) { + NodeType::OPERATION_DEFINITION => function($node) { $this->operationDefs[] = $node; return Visitor::skipNode(); }, - Node::FRAGMENT_DEFINITION => function(FragmentDefinition $def) { + NodeType::FRAGMENT_DEFINITION => function(FragmentDefinition $def) { $this->fragmentDefs[] = $def; return Visitor::skipNode(); }, - Node::DOCUMENT => [ + NodeType::DOCUMENT => [ 'leave' => function() use ($context) { $fragmentNameUsed = []; diff --git a/src/Validator/Rules/NoUnusedVariables.php b/src/Validator/Rules/NoUnusedVariables.php index 7e9b8d7..3d00551 100644 --- a/src/Validator/Rules/NoUnusedVariables.php +++ b/src/Validator/Rules/NoUnusedVariables.php @@ -4,6 +4,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\Visitor; use GraphQL\Validator\Messages; @@ -25,7 +26,7 @@ class NoUnusedVariables $this->variableDefs = []; return [ - Node::OPERATION_DEFINITION => [ + NodeType::OPERATION_DEFINITION => [ 'enter' => function() { $this->variableDefs = []; }, @@ -51,7 +52,7 @@ class NoUnusedVariables } } ], - Node::VARIABLE_DEFINITION => function($def) { + NodeType::VARIABLE_DEFINITION => function($def) { $this->variableDefs[] = $def; } ]; diff --git a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php index 713d7d9..4670e49 100644 --- a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php +++ b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php @@ -9,6 +9,7 @@ use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\NamedType; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\SelectionSet; use GraphQL\Language\Printer; use GraphQL\Type\Definition\ListOfType; @@ -52,7 +53,7 @@ class OverlappingFieldsCanBeMerged $this->comparedSet = new PairSet(); return [ - Node::SELECTION_SET => [ + NodeType::SELECTION_SET => [ // Note: we validate on the reverse traversal so deeper conflicts will be // caught first, for clearer error messages. 'leave' => function(SelectionSet $selectionSet) use ($context) { @@ -316,7 +317,7 @@ class OverlappingFieldsCanBeMerged $selection = $selectionSet->selections[$i]; switch ($selection->kind) { - case Node::FIELD: + case NodeType::FIELD: $fieldName = $selection->name->value; $fieldDef = null; if ($parentType && method_exists($parentType, 'getFields')) { @@ -332,7 +333,7 @@ class OverlappingFieldsCanBeMerged } $_astAndDefs[$responseName][] = [$parentType, $selection, $fieldDef]; break; - case Node::INLINE_FRAGMENT: + case NodeType::INLINE_FRAGMENT: $typeCondition = $selection->typeCondition; $inlineFragmentType = $typeCondition ? TypeInfo::typeFromAST($context->getSchema(), $typeCondition) @@ -346,7 +347,7 @@ class OverlappingFieldsCanBeMerged $_astAndDefs ); break; - case Node::FRAGMENT_SPREAD: + case NodeType::FRAGMENT_SPREAD: /** @var FragmentSpread $selection */ $fragName = $selection->name->value; if (!empty($_visitedFragmentNames[$fragName])) { diff --git a/src/Validator/Rules/PossibleFragmentSpreads.php b/src/Validator/Rules/PossibleFragmentSpreads.php index 7fb8940..f5bcb35 100644 --- a/src/Validator/Rules/PossibleFragmentSpreads.php +++ b/src/Validator/Rules/PossibleFragmentSpreads.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Utils; use GraphQL\Validator\ValidationContext; use GraphQL\Utils\TypeInfo; @@ -25,7 +26,7 @@ class PossibleFragmentSpreads public function __invoke(ValidationContext $context) { return [ - Node::INLINE_FRAGMENT => function(InlineFragment $node) use ($context) { + NodeType::INLINE_FRAGMENT => function(InlineFragment $node) use ($context) { $fragType = $context->getType(); $parentType = $context->getParentType(); @@ -36,7 +37,7 @@ class PossibleFragmentSpreads )); } }, - Node::FRAGMENT_SPREAD => function(FragmentSpread $node) use ($context) { + NodeType::FRAGMENT_SPREAD => function(FragmentSpread $node) use ($context) { $fragName = $node->name->value; $fragType = $this->getFragmentType($context, $fragName); $parentType = $context->getParentType(); diff --git a/src/Validator/Rules/ProvidedNonNullArguments.php b/src/Validator/Rules/ProvidedNonNullArguments.php index 4ea7692..57fabcc 100644 --- a/src/Validator/Rules/ProvidedNonNullArguments.php +++ b/src/Validator/Rules/ProvidedNonNullArguments.php @@ -6,6 +6,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\Directive; use GraphQL\Language\AST\Field; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Visitor; use GraphQL\Type\Definition\NonNull; use GraphQL\Utils; @@ -26,7 +27,7 @@ class ProvidedNonNullArguments public function __invoke(ValidationContext $context) { return [ - Node::FIELD => [ + NodeType::FIELD => [ 'leave' => function(Field $fieldAST) use ($context) { $fieldDef = $context->getFieldDef(); @@ -50,7 +51,7 @@ class ProvidedNonNullArguments } } ], - Node::DIRECTIVE => [ + NodeType::DIRECTIVE => [ 'leave' => function(Directive $directiveAST) use ($context) { $directiveDef = $context->getDirective(); if (!$directiveDef) { diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index 75d79d8..9646897 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -8,6 +8,7 @@ use GraphQL\Language\AST\Field; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\SelectionSet; use GraphQL\Language\Visitor; @@ -77,7 +78,7 @@ class QueryComplexity extends AbstractQuerySecurity return $this->invokeIfNeeded( $context, [ - Node::SELECTION_SET => function (SelectionSet $selectionSet) use ($context) { + NodeType::SELECTION_SET => function (SelectionSet $selectionSet) use ($context) { $this->fieldAstAndDefs = $this->collectFieldASTsAndDefs( $context, $context->getParentType(), @@ -86,11 +87,11 @@ class QueryComplexity extends AbstractQuerySecurity $this->fieldAstAndDefs ); }, - Node::VARIABLE_DEFINITION => function ($def) { + NodeType::VARIABLE_DEFINITION => function ($def) { $this->variableDefs[] = $def; return Visitor::skipNode(); }, - Node::OPERATION_DEFINITION => [ + NodeType::OPERATION_DEFINITION => [ 'leave' => function (OperationDefinition $operationDefinition) use ($context, &$complexity) { $complexity = $this->fieldComplexity($operationDefinition, $complexity); @@ -119,7 +120,7 @@ class QueryComplexity extends AbstractQuerySecurity private function nodeComplexity(Node $node, $complexity = 0) { switch ($node->kind) { - case Node::FIELD: + case NodeType::FIELD: /* @var Field $node */ // default values $args = []; @@ -147,7 +148,7 @@ class QueryComplexity extends AbstractQuerySecurity $complexity += call_user_func_array($complexityFn, [$childrenComplexity, $args]); break; - case Node::INLINE_FRAGMENT: + case NodeType::INLINE_FRAGMENT: /* @var InlineFragment $node */ // node has children? if (isset($node->selectionSet)) { @@ -155,7 +156,7 @@ class QueryComplexity extends AbstractQuerySecurity } break; - case Node::FRAGMENT_SPREAD: + case NodeType::FRAGMENT_SPREAD: /* @var FragmentSpread $node */ $fragment = $this->getFragment($node); diff --git a/src/Validator/Rules/QueryDepth.php b/src/Validator/Rules/QueryDepth.php index 04f08ad..d3f4ec3 100644 --- a/src/Validator/Rules/QueryDepth.php +++ b/src/Validator/Rules/QueryDepth.php @@ -49,7 +49,7 @@ class QueryDepth extends AbstractQuerySecurity return $this->invokeIfNeeded( $context, [ - Node::OPERATION_DEFINITION => [ + NodeType::OPERATION_DEFINITION => [ 'leave' => function (OperationDefinition $operationDefinition) use ($context) { $maxDepth = $this->fieldDepth($operationDefinition); @@ -83,7 +83,7 @@ class QueryDepth extends AbstractQuerySecurity private function nodeDepth(Node $node, $depth = 0, $maxDepth = 0) { switch ($node->kind) { - case Node::FIELD: + case NodeType::FIELD: /* @var Field $node */ // node has children? if (null !== $node->selectionSet) { @@ -95,7 +95,7 @@ class QueryDepth extends AbstractQuerySecurity } break; - case Node::INLINE_FRAGMENT: + case NodeType::INLINE_FRAGMENT: /* @var InlineFragment $node */ // node has children? if (null !== $node->selectionSet) { @@ -103,7 +103,7 @@ class QueryDepth extends AbstractQuerySecurity } break; - case Node::FRAGMENT_SPREAD: + case NodeType::FRAGMENT_SPREAD: /* @var FragmentSpread $node */ $fragment = $this->getFragment($node); diff --git a/src/Validator/Rules/ScalarLeafs.php b/src/Validator/Rules/ScalarLeafs.php index cfa73e1..ef13f36 100644 --- a/src/Validator/Rules/ScalarLeafs.php +++ b/src/Validator/Rules/ScalarLeafs.php @@ -5,6 +5,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Field; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Type\Definition\Type; use GraphQL\Validator\Messages; use GraphQL\Validator\ValidationContext; @@ -24,7 +25,7 @@ class ScalarLeafs public function __invoke(ValidationContext $context) { return [ - Node::FIELD => function(Field $node) use ($context) { + NodeType::FIELD => function(Field $node) use ($context) { $type = $context->getType(); if ($type) { if (Type::isLeafType($type)) { diff --git a/src/Validator/Rules/UniqueArgumentNames.php b/src/Validator/Rules/UniqueArgumentNames.php index 09bd622..bc78f72 100644 --- a/src/Validator/Rules/UniqueArgumentNames.php +++ b/src/Validator/Rules/UniqueArgumentNames.php @@ -4,6 +4,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Argument; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Visitor; use GraphQL\Validator\ValidationContext; @@ -21,13 +22,13 @@ class UniqueArgumentNames $this->knownArgNames = []; return [ - Node::FIELD => function () { + NodeType::FIELD => function () { $this->knownArgNames = [];; }, - Node::DIRECTIVE => function () { + NodeType::DIRECTIVE => function () { $this->knownArgNames = []; }, - Node::ARGUMENT => function (Argument $node) use ($context) { + NodeType::ARGUMENT => function (Argument $node) use ($context) { $argName = $node->name->value; if (!empty($this->knownArgNames[$argName])) { $context->reportError(new Error( diff --git a/src/Validator/Rules/UniqueFragmentNames.php b/src/Validator/Rules/UniqueFragmentNames.php index c4457f4..24f09f2 100644 --- a/src/Validator/Rules/UniqueFragmentNames.php +++ b/src/Validator/Rules/UniqueFragmentNames.php @@ -5,6 +5,7 @@ use GraphQL\Error\Error; use GraphQL\Language\AST\Argument; use GraphQL\Language\AST\FragmentDefinition; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Visitor; use GraphQL\Validator\ValidationContext; @@ -22,10 +23,10 @@ class UniqueFragmentNames $this->knownFragmentNames = []; return [ - Node::OPERATION_DEFINITION => function () { + NodeType::OPERATION_DEFINITION => function () { return Visitor::skipNode(); }, - Node::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use ($context) { + NodeType::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use ($context) { $fragmentName = $node->name->value; if (!empty($this->knownFragmentNames[$fragmentName])) { $context->reportError(new Error( diff --git a/src/Validator/Rules/UniqueInputFieldNames.php b/src/Validator/Rules/UniqueInputFieldNames.php index 9d7d4c6..2b5427f 100644 --- a/src/Validator/Rules/UniqueInputFieldNames.php +++ b/src/Validator/Rules/UniqueInputFieldNames.php @@ -3,6 +3,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\ObjectField; use GraphQL\Language\Visitor; use GraphQL\Validator\ValidationContext; @@ -23,7 +24,7 @@ class UniqueInputFieldNames $this->knownNameStack = []; return [ - Node::OBJECT => [ + NodeType::OBJECT => [ 'enter' => function() { $this->knownNameStack[] = $this->knownNames; $this->knownNames = []; @@ -32,7 +33,7 @@ class UniqueInputFieldNames $this->knownNames = array_pop($this->knownNameStack); } ], - Node::OBJECT_FIELD => function(ObjectField $node) use ($context) { + NodeType::OBJECT_FIELD => function(ObjectField $node) use ($context) { $fieldName = $node->name->value; if (!empty($this->knownNames[$fieldName])) { diff --git a/src/Validator/Rules/UniqueOperationNames.php b/src/Validator/Rules/UniqueOperationNames.php index dc63059..3d8c4e0 100644 --- a/src/Validator/Rules/UniqueOperationNames.php +++ b/src/Validator/Rules/UniqueOperationNames.php @@ -3,6 +3,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\Visitor; use GraphQL\Validator\ValidationContext; @@ -21,7 +22,7 @@ class UniqueOperationNames $this->knownOperationNames = []; return [ - Node::OPERATION_DEFINITION => function(OperationDefinition $node) use ($context) { + NodeType::OPERATION_DEFINITION => function(OperationDefinition $node) use ($context) { $operationName = $node->name; if ($operationName) { @@ -36,7 +37,7 @@ class UniqueOperationNames } return Visitor::skipNode(); }, - Node::FRAGMENT_DEFINITION => function() { + NodeType::FRAGMENT_DEFINITION => function() { return Visitor::skipNode(); } ]; diff --git a/src/Validator/Rules/UniqueVariableNames.php b/src/Validator/Rules/UniqueVariableNames.php index 8482e7a..2ec5201 100644 --- a/src/Validator/Rules/UniqueVariableNames.php +++ b/src/Validator/Rules/UniqueVariableNames.php @@ -3,6 +3,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\VariableDefinition; use GraphQL\Validator\ValidationContext; @@ -20,10 +21,10 @@ class UniqueVariableNames $this->knownVariableNames = []; return [ - Node::OPERATION_DEFINITION => function() { + NodeType::OPERATION_DEFINITION => function() { $this->knownVariableNames = []; }, - Node::VARIABLE_DEFINITION => function(VariableDefinition $node) use ($context) { + NodeType::VARIABLE_DEFINITION => function(VariableDefinition $node) use ($context) { $variableName = $node->variable->name->value; if (!empty($this->knownVariableNames[$variableName])) { $context->reportError(new Error( diff --git a/src/Validator/Rules/VariablesAreInputTypes.php b/src/Validator/Rules/VariablesAreInputTypes.php index a454ae8..0db91bc 100644 --- a/src/Validator/Rules/VariablesAreInputTypes.php +++ b/src/Validator/Rules/VariablesAreInputTypes.php @@ -4,6 +4,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\VariableDefinition; use GraphQL\Language\Printer; use GraphQL\Type\Definition\InputType; @@ -21,7 +22,7 @@ class VariablesAreInputTypes public function __invoke(ValidationContext $context) { return [ - Node::VARIABLE_DEFINITION => function(VariableDefinition $node) use ($context) { + NodeType::VARIABLE_DEFINITION => function(VariableDefinition $node) use ($context) { $type = Utils\TypeInfo::typeFromAST($context->getSchema(), $node->type); // If the variable type is not an input type, return an error. diff --git a/src/Validator/Rules/VariablesInAllowedPosition.php b/src/Validator/Rules/VariablesInAllowedPosition.php index 78d81a9..5cef130 100644 --- a/src/Validator/Rules/VariablesInAllowedPosition.php +++ b/src/Validator/Rules/VariablesInAllowedPosition.php @@ -5,6 +5,7 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\Variable; use GraphQL\Language\AST\VariableDefinition; @@ -28,7 +29,7 @@ class VariablesInAllowedPosition public function __invoke(ValidationContext $context) { return [ - Node::OPERATION_DEFINITION => [ + NodeType::OPERATION_DEFINITION => [ 'enter' => function () { $this->varDefMap = []; }, @@ -60,7 +61,7 @@ class VariablesInAllowedPosition } } ], - Node::VARIABLE_DEFINITION => function (VariableDefinition $varDefAST) { + NodeType::VARIABLE_DEFINITION => function (VariableDefinition $varDefAST) { $this->varDefMap[$varDefAST->variable->name->value] = $varDefAST; } ]; diff --git a/src/Validator/ValidationContext.php b/src/Validator/ValidationContext.php index 58f0a6c..2dd6894 100644 --- a/src/Validator/ValidationContext.php +++ b/src/Validator/ValidationContext.php @@ -3,6 +3,7 @@ namespace GraphQL\Validator; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\HasSelectionSet; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\Variable; use GraphQL\Language\Visitor; @@ -132,7 +133,7 @@ class ValidationContext if (!$fragments) { $this->fragments = $fragments = array_reduce($this->getDocument()->definitions, function($frags, $statement) { - if ($statement->kind === Node::FRAGMENT_DEFINITION) { + if ($statement->kind === NodeType::FRAGMENT_DEFINITION) { $frags[$statement->name->value] = $statement; } return $frags; @@ -156,7 +157,7 @@ class ValidationContext for ($i = 0; $i < count($set->selections); $i++) { $selection = $set->selections[$i]; - if ($selection->kind === Node::FRAGMENT_SPREAD) { + if ($selection->kind === NodeType::FRAGMENT_SPREAD) { $spreads[] = $selection; } else if ($selection->selectionSet) { $setsToVisit[] = $selection->selectionSet; @@ -213,10 +214,10 @@ class ValidationContext $newUsages = []; $typeInfo = new TypeInfo($this->schema); Visitor::visit($node, Visitor::visitWithTypeInfo($typeInfo, [ - Node::VARIABLE_DEFINITION => function () { + NodeType::VARIABLE_DEFINITION => function () { return false; }, - Node::VARIABLE => function (Variable $variable) use (&$newUsages, $typeInfo) { + NodeType::VARIABLE => function (Variable $variable) use (&$newUsages, $typeInfo) { $newUsages[] = ['node' => $variable, 'type' => $typeInfo->getInputType()]; } ])); diff --git a/tests/Validator/TestCase.php b/tests/Validator/TestCase.php index c58d51f..994c131 100644 --- a/tests/Validator/TestCase.php +++ b/tests/Validator/TestCase.php @@ -2,6 +2,7 @@ namespace GraphQL\Tests\Validator; use GraphQL\GraphQL; +use GraphQL\Language\Lexer; use GraphQL\Language\Parser; use GraphQL\Schema; use GraphQL\Type\Definition\Directive; @@ -308,16 +309,18 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase function expectValid($schema, $rules, $queryString) { + $parser = new Parser(new Lexer()); $this->assertEquals( [], - DocumentValidator::validate($schema, Parser::parse($queryString), $rules), + DocumentValidator::validate($schema, $parser->parse($queryString), $rules), 'Should validate' ); } function expectInvalid($schema, $rules, $queryString, $expectedErrors) { - $errors = DocumentValidator::validate($schema, Parser::parse($queryString), $rules); + $parser = new Parser(new Lexer()); + $errors = DocumentValidator::validate($schema, $parser->parse($queryString), $rules); $this->assertNotEmpty($errors, 'GraphQL should not validate'); $this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors));