move to NodeType enum

This commit is contained in:
Andreas Heiberg 2016-11-10 22:20:19 +00:00 committed by vladar
parent 816fa067b3
commit d8ca5f4183
31 changed files with 113 additions and 82 deletions

View File

@ -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;

View File

@ -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." ];
}

View File

@ -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;

View File

@ -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);

View File

@ -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();}
];
}
}

View File

@ -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();

View File

@ -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)) {

View File

@ -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;

View File

@ -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;
}
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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])

View File

@ -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);
}

View File

@ -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;
}
];

View File

@ -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 = [];

View File

@ -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;
}
];

View File

@ -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])) {

View File

@ -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();

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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)) {

View File

@ -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(

View File

@ -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(

View File

@ -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])) {

View File

@ -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();
}
];

View File

@ -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(

View File

@ -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.

View File

@ -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;
}
];

View File

@ -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()];
}
]));

View File

@ -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));