Replace node kind checks by InstanceOf checks

This commit is contained in:
Simon Podlipsky 2018-10-06 00:30:05 +02:00
parent bdbb30c604
commit 90b51e1a5d
19 changed files with 177 additions and 157 deletions

View File

@ -138,8 +138,8 @@ class ReferenceExecutor implements ExecutorImplementation
$operation = null;
$hasMultipleAssumedOperations = false;
foreach ($documentNode->definitions as $definition) {
switch ($definition->kind) {
case NodeKind::OPERATION_DEFINITION:
switch (true) {
case $definition instanceof OperationDefinitionNode:
if (! $operationName && $operation) {
$hasMultipleAssumedOperations = true;
}
@ -148,7 +148,7 @@ class ReferenceExecutor implements ExecutorImplementation
$operation = $definition;
}
break;
case NodeKind::FRAGMENT_DEFINITION:
case $definition instanceof FragmentDefinitionNode:
$fragments[$definition->name->value] = $definition;
break;
}
@ -334,8 +334,8 @@ class ReferenceExecutor implements ExecutorImplementation
) {
$exeContext = $this->exeContext;
foreach ($selectionSet->selections as $selection) {
switch ($selection->kind) {
case NodeKind::FIELD:
switch (true) {
case $selection instanceof FieldNode:
if (! $this->shouldIncludeNode($selection)) {
break;
}
@ -345,7 +345,7 @@ class ReferenceExecutor implements ExecutorImplementation
}
$fields[$name][] = $selection;
break;
case NodeKind::INLINE_FRAGMENT:
case $selection instanceof InlineFragmentNode:
if (! $this->shouldIncludeNode($selection) ||
! $this->doesFragmentConditionMatch($selection, $runtimeType)
) {
@ -358,7 +358,7 @@ class ReferenceExecutor implements ExecutorImplementation
$visitedFragmentNames
);
break;
case NodeKind::FRAGMENT_SPREAD:
case $selection instanceof FragmentSpreadNode:
$fragName = $selection->name->value;
if (! empty($visitedFragmentNames[$fragName]) || ! $this->shouldIncludeNode($selection)) {
break;

View File

@ -64,7 +64,7 @@ class Collector
foreach ($documentNode->definitions as $definitionNode) {
/** @var DefinitionNode|Node $definitionNode */
if ($definitionNode->kind === NodeKind::OPERATION_DEFINITION) {
if ($definitionNode instanceof OperationDefinitionNode) {
/** @var OperationDefinitionNode $definitionNode */
if ($operationName === null && $this->operation !== null) {
$hasMultipleAssumedOperations = true;
@ -74,7 +74,7 @@ class Collector
) {
$this->operation = $definitionNode;
}
} elseif ($definitionNode->kind === NodeKind::FRAGMENT_DEFINITION) {
} elseif ($definitionNode instanceof FragmentDefinitionNode) {
/** @var FragmentDefinitionNode $definitionNode */
$this->fragments[$definitionNode->name->value] = $definitionNode;
}
@ -194,7 +194,7 @@ class Collector
}
}
if ($selection->kind === NodeKind::FIELD) {
if ($selection instanceof FieldNode) {
/** @var FieldNode $selection */
$resultName = $selection->alias ? $selection->alias->value : $selection->name->value;
@ -204,7 +204,7 @@ class Collector
}
$this->fields[$resultName][] = $selection;
} elseif ($selection->kind === NodeKind::FRAGMENT_SPREAD) {
} elseif ($selection instanceof FragmentSpreadNode) {
/** @var FragmentSpreadNode $selection */
$fragmentName = $selection->name->value;
@ -245,7 +245,7 @@ class Collector
}
$this->doCollectFields($runtimeType, $fragmentDefinition->selectionSet);
} elseif ($selection->kind === NodeKind::INLINE_FRAGMENT) {
} elseif ($selection instanceof InlineFragmentNode) {
/** @var InlineFragmentNode $selection */
if ($selection->typeCondition !== null) {

View File

@ -9,6 +9,6 @@ class ListTypeNode extends Node implements TypeNode
/** @var string */
public $kind = NodeKind::LIST_TYPE;
/** @var Node */
/** @var TypeNode */
public $type;
}

View File

@ -9,6 +9,6 @@ class NonNullTypeNode extends Node implements TypeNode
/** @var string */
public $kind = NodeKind::NON_NULL_TYPE;
/** @var NameNode | ListTypeNode */
/** @var NamedTypeNode | ListTypeNode */
public $type;
}

View File

@ -28,6 +28,7 @@ use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NonNullTypeNode;
@ -47,6 +48,7 @@ use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Utils\Utils;
use function count;
use function implode;
@ -97,11 +99,11 @@ class Printer
$ast,
[
'leave' => [
NodeKind::NAME => static function (Node $node) {
NodeKind::NAME => static function (NameNode $node) {
return '' . $node->value;
},
NodeKind::VARIABLE => static function ($node) {
NodeKind::VARIABLE => static function (VariableNode $node) {
return '$' . $node->name;
},

View File

@ -322,7 +322,7 @@ class AST
*
* @api
*/
public static function valueFromAST($valueNode, InputType $type, ?array $variables = null)
public static function valueFromAST($valueNode, Type $type, ?array $variables = null)
{
$undefined = Utils::undefined();

View File

@ -17,7 +17,6 @@ use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
@ -244,23 +243,20 @@ class ASTDefinitionBuilder
*
* @throws Error
*/
private function makeSchemaDef($def)
private function makeSchemaDef(Node $def)
{
if (! $def) {
throw new Error('def must be defined.');
}
switch ($def->kind) {
case NodeKind::OBJECT_TYPE_DEFINITION:
switch (true) {
case $def instanceof ObjectTypeDefinitionNode:
return $this->makeTypeDef($def);
case NodeKind::INTERFACE_TYPE_DEFINITION:
case $def instanceof InterfaceTypeDefinitionNode:
return $this->makeInterfaceDef($def);
case NodeKind::ENUM_TYPE_DEFINITION:
case $def instanceof EnumTypeDefinitionNode:
return $this->makeEnumDef($def);
case NodeKind::UNION_TYPE_DEFINITION:
case $def instanceof UnionTypeDefinitionNode:
return $this->makeUnionDef($def);
case NodeKind::SCALAR_TYPE_DEFINITION:
case $def instanceof ScalarTypeDefinitionNode:
return $this->makeScalarDef($def);
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
case $def instanceof InputObjectTypeDefinitionNode:
return $this->makeInputObjectDef($def);
default:
throw new Error(sprintf('Type kind of %s not supported.', $def->kind));
@ -430,62 +426,48 @@ class ASTDefinitionBuilder
}
/**
* @param ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode|EnumTypeExtensionNode|ScalarTypeDefinitionNode|InputObjectTypeDefinitionNode $def
* @param mixed[] $config
* @param mixed[] $config
*
* @return CustomScalarType|EnumType|InputObjectType|InterfaceType|ObjectType|UnionType
*
* @throws Error
*/
private function makeSchemaDefFromConfig($def, array $config)
private function makeSchemaDefFromConfig(Node $def, array $config)
{
if (! $def) {
throw new Error('def must be defined.');
}
switch ($def->kind) {
case NodeKind::OBJECT_TYPE_DEFINITION:
switch (true) {
case $def instanceof ObjectTypeDefinitionNode:
return new ObjectType($config);
case NodeKind::INTERFACE_TYPE_DEFINITION:
case $def instanceof InterfaceTypeDefinitionNode:
return new InterfaceType($config);
case NodeKind::ENUM_TYPE_DEFINITION:
case $def instanceof EnumTypeDefinitionNode:
return new EnumType($config);
case NodeKind::UNION_TYPE_DEFINITION:
case $def instanceof UnionTypeDefinitionNode:
return new UnionType($config);
case NodeKind::SCALAR_TYPE_DEFINITION:
case $def instanceof ScalarTypeDefinitionNode:
return new CustomScalarType($config);
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
case $def instanceof InputObjectTypeDefinitionNode:
return new InputObjectType($config);
default:
throw new Error(sprintf('Type kind of %s not supported.', $def->kind));
}
}
/**
* @param TypeNode|ListTypeNode|NonNullTypeNode $typeNode
*
* @return TypeNode
*/
private function getNamedTypeNode(TypeNode $typeNode)
private function getNamedTypeNode(TypeNode $typeNode) : TypeNode
{
$namedType = $typeNode;
while ($namedType->kind === NodeKind::LIST_TYPE || $namedType->kind === NodeKind::NON_NULL_TYPE) {
while ($namedType instanceof ListTypeNode || $namedType instanceof NonNullTypeNode) {
$namedType = $namedType->type;
}
return $namedType;
}
/**
* @param TypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
*
* @return Type
*/
private function buildWrappedType(Type $innerType, TypeNode $inputTypeNode)
private function buildWrappedType(Type $innerType, TypeNode $inputTypeNode) : Type
{
if ($inputTypeNode->kind === NodeKind::LIST_TYPE) {
if ($inputTypeNode instanceof ListTypeNode) {
return Type::listOf($this->buildWrappedType($innerType, $inputTypeNode->type));
}
if ($inputTypeNode->kind === NodeKind::NON_NULL_TYPE) {
if ($inputTypeNode instanceof NonNullTypeNode) {
$wrappedType = $this->buildWrappedType($innerType, $inputTypeNode->type);
return Type::nonNull(NonNull::assertNullableType($wrappedType));

View File

@ -5,10 +5,16 @@ declare(strict_types=1);
namespace GraphQL\Utils;
use GraphQL\Error\Error;
use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Type\Definition\Directive;
@ -95,39 +101,38 @@ class BuildSchema
public function buildSchema()
{
/** @var SchemaDefinitionNode $schemaDef */
$schemaDef = null;
$typeDefs = [];
$this->nodeMap = [];
$directiveDefs = [];
foreach ($this->ast->definitions as $d) {
switch ($d->kind) {
case NodeKind::SCHEMA_DEFINITION:
if ($schemaDef) {
foreach ($this->ast->definitions as $definition) {
switch (true) {
case $definition instanceof SchemaDefinitionNode:
if ($schemaDef !== null) {
throw new Error('Must provide only one schema definition.');
}
$schemaDef = $d;
$schemaDef = $definition;
break;
case NodeKind::SCALAR_TYPE_DEFINITION:
case NodeKind::OBJECT_TYPE_DEFINITION:
case NodeKind::INTERFACE_TYPE_DEFINITION:
case NodeKind::ENUM_TYPE_DEFINITION:
case NodeKind::UNION_TYPE_DEFINITION:
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
$typeName = $d->name->value;
case $definition instanceof ScalarTypeDefinitionNode:
case $definition instanceof ObjectTypeDefinitionNode:
case $definition instanceof InterfaceTypeDefinitionNode:
case $definition instanceof EnumTypeDefinitionNode:
case $definition instanceof UnionTypeDefinitionNode:
case $definition instanceof InputObjectTypeDefinitionNode:
$typeName = $definition->name->value;
if (! empty($this->nodeMap[$typeName])) {
throw new Error(sprintf('Type "%s" was defined more than once.', $typeName));
}
$typeDefs[] = $d;
$this->nodeMap[$typeName] = $d;
$typeDefs[] = $definition;
$this->nodeMap[$typeName] = $definition;
break;
case NodeKind::DIRECTIVE_DEFINITION:
$directiveDefs[] = $d;
case $definition instanceof DirectiveDefinitionNode:
$directiveDefs[] = $definition;
break;
}
}
$operationTypes = $schemaDef
$operationTypes = $schemaDef !== null
? $this->getOperationTypes($schemaDef)
: [
'query' => isset($this->nodeMap['Query']) ? 'Query' : null,

View File

@ -7,13 +7,16 @@ namespace GraphQL\Utils;
use GraphQL\Error\Error;
use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\AST\SchemaTypeExtensionNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\AST\TypeExtensionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType;
@ -74,8 +77,8 @@ class SchemaExtender
*/
protected static function checkExtensionNode(Type $type, Node $node) : void
{
switch ($node->kind) {
case NodeKind::OBJECT_TYPE_EXTENSION:
switch (true) {
case $node instanceof ObjectTypeExtensionNode:
if (! ($type instanceof ObjectType)) {
throw new Error(
'Cannot extend non-object type "' . $type->name . '".',
@ -83,7 +86,7 @@ class SchemaExtender
);
}
break;
case NodeKind::INTERFACE_TYPE_EXTENSION:
case $node instanceof InterfaceTypeExtensionNode:
if (! ($type instanceof InterfaceType)) {
throw new Error(
'Cannot extend non-interface type "' . $type->name . '".',
@ -91,7 +94,7 @@ class SchemaExtender
);
}
break;
case NodeKind::ENUM_TYPE_EXTENSION:
case $node instanceof EnumTypeExtensionNode:
if (! ($type instanceof EnumType)) {
throw new Error(
'Cannot extend non-enum type "' . $type->name . '".',
@ -99,7 +102,7 @@ class SchemaExtender
);
}
break;
case NodeKind::UNION_TYPE_EXTENSION:
case $node instanceof UnionTypeExtensionNode:
if (! ($type instanceof UnionType)) {
throw new Error(
'Cannot extend non-union type "' . $type->name . '".',
@ -107,7 +110,7 @@ class SchemaExtender
);
}
break;
case NodeKind::INPUT_OBJECT_TYPE_EXTENSION:
case $node instanceof InputObjectTypeExtensionNode:
if (! ($type instanceof InputObjectType)) {
throw new Error(
'Cannot extend non-input object type "' . $type->name . '".',

View File

@ -6,12 +6,21 @@ namespace GraphQL\Utils;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\ObjectFieldNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Type\Definition\CompositeType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType;
@ -255,13 +264,13 @@ class TypeInfo
// any assumptions of a valid schema to ensure runtime types are properly
// checked before continuing since TypeInfo is used as part of validation
// which occurs before guarantees of schema and document validity.
switch ($node->kind) {
case NodeKind::SELECTION_SET:
switch (true) {
case $node instanceof SelectionSetNode:
$namedType = Type::getNamedType($this->getType());
$this->parentTypeStack[] = Type::isCompositeType($namedType) ? $namedType : null;
break;
case NodeKind::FIELD:
case $node instanceof FieldNode:
$parentType = $this->getParentType();
$fieldDef = null;
if ($parentType) {
@ -275,11 +284,11 @@ class TypeInfo
$this->typeStack[] = Type::isOutputType($fieldType) ? $fieldType : null;
break;
case NodeKind::DIRECTIVE:
case $node instanceof DirectiveNode:
$this->directive = $schema->getDirective($node->name->value);
break;
case NodeKind::OPERATION_DEFINITION:
case $node instanceof OperationDefinitionNode:
$type = null;
if ($node->operation === 'query') {
$type = $schema->getQueryType();
@ -291,8 +300,8 @@ class TypeInfo
$this->typeStack[] = Type::isOutputType($type) ? $type : null;
break;
case NodeKind::INLINE_FRAGMENT:
case NodeKind::FRAGMENT_DEFINITION:
case $node instanceof InlineFragmentNode:
case $node instanceof FragmentDefinitionNode:
$typeConditionNode = $node->typeCondition;
$outputType = $typeConditionNode ? self::typeFromAST(
$schema,
@ -301,12 +310,12 @@ class TypeInfo
$this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null;
break;
case NodeKind::VARIABLE_DEFINITION:
case $node instanceof VariableDefinitionNode:
$inputType = self::typeFromAST($schema, $node->type);
$this->inputTypeStack[] = Type::isInputType($inputType) ? $inputType : null; // push
break;
case NodeKind::ARGUMENT:
case $node instanceof ArgumentNode:
$fieldOrDirective = $this->getDirective() ?: $this->getFieldDef();
$argDef = $argType = null;
if ($fieldOrDirective) {
@ -324,7 +333,7 @@ class TypeInfo
$this->inputTypeStack[] = Type::isInputType($argType) ? $argType : null;
break;
case NodeKind::LST:
case $node instanceof ListValueNode:
$listType = Type::getNullableType($this->getInputType());
$itemType = $listType instanceof ListOfType
? $listType->getWrappedType()
@ -332,7 +341,7 @@ class TypeInfo
$this->inputTypeStack[] = Type::isInputType($itemType) ? $itemType : null;
break;
case NodeKind::OBJECT_FIELD:
case $node instanceof ObjectFieldNode:
$objectType = Type::getNamedType($this->getInputType());
$fieldType = null;
$inputFieldType = null;
@ -344,7 +353,7 @@ class TypeInfo
$this->inputTypeStack[] = Type::isInputType($inputFieldType) ? $inputFieldType : null;
break;
case NodeKind::ENUM:
case $node instanceof EnumValueNode:
$enumType = Type::getNamedType($this->getInputType());
$enumValue = null;
if ($enumType instanceof EnumType) {
@ -458,37 +467,37 @@ class TypeInfo
public function leave(Node $node)
{
switch ($node->kind) {
case NodeKind::SELECTION_SET:
switch (true) {
case $node instanceof SelectionSetNode:
array_pop($this->parentTypeStack);
break;
case NodeKind::FIELD:
case $node instanceof FieldNode:
array_pop($this->fieldDefStack);
array_pop($this->typeStack);
break;
case NodeKind::DIRECTIVE:
case $node instanceof DirectiveNode:
$this->directive = null;
break;
case NodeKind::OPERATION_DEFINITION:
case NodeKind::INLINE_FRAGMENT:
case NodeKind::FRAGMENT_DEFINITION:
case $node instanceof OperationDefinitionNode:
case $node instanceof InlineFragmentNode:
case $node instanceof FragmentDefinitionNode:
array_pop($this->typeStack);
break;
case NodeKind::VARIABLE_DEFINITION:
case $node instanceof VariableDefinitionNode:
array_pop($this->inputTypeStack);
break;
case NodeKind::ARGUMENT:
case $node instanceof ArgumentNode:
$this->argument = null;
array_pop($this->inputTypeStack);
break;
case NodeKind::LST:
case NodeKind::OBJECT_FIELD:
case $node instanceof ListValueNode:
case $node instanceof ObjectFieldNode:
array_pop($this->inputTypeStack);
break;
case NodeKind::ENUM:
case $node instanceof EnumValueNode:
$this->enumValue = null;
break;
}

View File

@ -6,6 +6,8 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList;
@ -34,7 +36,7 @@ class KnownArgumentNames extends ValidationRule
}
$argumentOf = $ancestors[count($ancestors) - 1];
if ($argumentOf->kind === NodeKind::FIELD) {
if ($argumentOf instanceof FieldNode) {
$fieldDef = $context->getFieldDef();
$parentType = $context->getParentType();
if ($fieldDef && $parentType) {
@ -56,7 +58,7 @@ class KnownArgumentNames extends ValidationRule
[$node]
));
}
} elseif ($argumentOf->kind === NodeKind::DIRECTIVE) {
} elseif ($argumentOf instanceof DirectiveNode) {
$directive = $context->getDirective();
if ($directive) {
$context->reportError(new Error(

View File

@ -7,10 +7,31 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error;
use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\EnumValueDefinitionNode;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeExtensionNode;
use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\AST\SchemaTypeExtensionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Validator\ValidationContext;
use function array_map;
@ -96,8 +117,8 @@ class KnownDirectives extends ValidationRule
private function getDirectiveLocationForASTPath(array $ancestors)
{
$appliedTo = $ancestors[count($ancestors) - 1];
switch ($appliedTo->kind) {
case NodeKind::OPERATION_DEFINITION:
switch (true) {
case $appliedTo instanceof OperationDefinitionNode:
switch ($appliedTo->operation) {
case 'query':
return DirectiveLocation::QUERY;
@ -107,40 +128,40 @@ class KnownDirectives extends ValidationRule
return DirectiveLocation::SUBSCRIPTION;
}
break;
case NodeKind::FIELD:
case $appliedTo instanceof FieldNode:
return DirectiveLocation::FIELD;
case NodeKind::FRAGMENT_SPREAD:
case $appliedTo instanceof FragmentSpreadNode:
return DirectiveLocation::FRAGMENT_SPREAD;
case NodeKind::INLINE_FRAGMENT:
case $appliedTo instanceof InlineFragmentNode:
return DirectiveLocation::INLINE_FRAGMENT;
case NodeKind::FRAGMENT_DEFINITION:
case $appliedTo instanceof FragmentDefinitionNode:
return DirectiveLocation::FRAGMENT_DEFINITION;
case NodeKind::SCHEMA_DEFINITION:
case NodeKind::SCHEMA_EXTENSION:
case $appliedTo instanceof SchemaDefinitionNode:
case $appliedTo instanceof SchemaTypeExtensionNode:
return DirectiveLocation::SCHEMA;
case NodeKind::SCALAR_TYPE_DEFINITION:
case NodeKind::SCALAR_TYPE_EXTENSION:
case $appliedTo instanceof ScalarTypeDefinitionNode:
case $appliedTo instanceof ScalarTypeExtensionNode:
return DirectiveLocation::SCALAR;
case NodeKind::OBJECT_TYPE_DEFINITION:
case NodeKind::OBJECT_TYPE_EXTENSION:
case $appliedTo instanceof ObjectTypeDefinitionNode:
case $appliedTo instanceof ObjectTypeExtensionNode:
return DirectiveLocation::OBJECT;
case NodeKind::FIELD_DEFINITION:
case $appliedTo instanceof FieldDefinitionNode:
return DirectiveLocation::FIELD_DEFINITION;
case NodeKind::INTERFACE_TYPE_DEFINITION:
case NodeKind::INTERFACE_TYPE_EXTENSION:
case $appliedTo instanceof InterfaceTypeDefinitionNode:
case $appliedTo instanceof InterfaceTypeExtensionNode:
return DirectiveLocation::IFACE;
case NodeKind::UNION_TYPE_DEFINITION:
case NodeKind::UNION_TYPE_EXTENSION:
case $appliedTo instanceof UnionTypeDefinitionNode:
case $appliedTo instanceof UnionTypeExtensionNode:
return DirectiveLocation::UNION;
case NodeKind::ENUM_TYPE_DEFINITION:
case NodeKind::ENUM_TYPE_EXTENSION:
case $appliedTo instanceof EnumTypeDefinitionNode:
case $appliedTo instanceof EnumTypeExtensionNode:
return DirectiveLocation::ENUM;
case NodeKind::ENUM_VALUE_DEFINITION:
case $appliedTo instanceof EnumValueDefinitionNode:
return DirectiveLocation::ENUM_VALUE;
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
case NodeKind::INPUT_OBJECT_TYPE_EXTENSION:
case $appliedTo instanceof InputObjectTypeDefinitionNode:
case $appliedTo instanceof InputObjectTypeExtensionNode:
return DirectiveLocation::INPUT_OBJECT;
case NodeKind::INPUT_VALUE_DEFINITION:
case $appliedTo instanceof InputValueDefinitionNode:
$parentNode = $ancestors[count($ancestors) - 3];
return $parentNode instanceof InputObjectTypeDefinitionNode

View File

@ -30,7 +30,7 @@ class LoneAnonymousOperation extends ValidationRule
$tmp = Utils::filter(
$node->definitions,
static function (Node $definition) {
return $definition->kind === NodeKind::OPERATION_DEFINITION;
return $definition instanceof OperationDefinitionNode;
}
);

View File

@ -110,9 +110,8 @@ class QueryComplexity extends QuerySecurityRule
private function nodeComplexity(Node $node, $complexity = 0)
{
switch ($node->kind) {
case NodeKind::FIELD:
/** @var FieldNode $node */
switch (true) {
case $node instanceof FieldNode:
// default values
$args = [];
$complexityFn = FieldDefinition::DEFAULT_COMPLEXITY_FN;
@ -143,16 +142,14 @@ class QueryComplexity extends QuerySecurityRule
$complexity += call_user_func_array($complexityFn, [$childrenComplexity, $args]);
break;
case NodeKind::INLINE_FRAGMENT:
/** @var InlineFragmentNode $node */
case $node instanceof InlineFragmentNode:
// node has children?
if (isset($node->selectionSet)) {
$complexity = $this->fieldComplexity($node, $complexity);
}
break;
case NodeKind::FRAGMENT_SPREAD:
/** @var FragmentSpreadNode $node */
case $node instanceof FragmentSpreadNode:
$fragment = $this->getFragment($node);
if ($fragment !== null) {

View File

@ -5,6 +5,9 @@ declare(strict_types=1);
namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode;
@ -57,9 +60,8 @@ class QueryDepth extends QuerySecurityRule
private function nodeDepth(Node $node, $depth = 0, $maxDepth = 0)
{
switch ($node->kind) {
case NodeKind::FIELD:
/** @var FieldNode $node */
switch (true) {
case $node instanceof FieldNode:
// node has children?
if ($node->selectionSet !== null) {
// update maxDepth if needed
@ -70,16 +72,14 @@ class QueryDepth extends QuerySecurityRule
}
break;
case NodeKind::INLINE_FRAGMENT:
/** @var InlineFragmentNode $node */
case $node instanceof InlineFragmentNode:
// node has children?
if ($node->selectionSet !== null) {
$maxDepth = $this->fieldDepth($node, $depth, $maxDepth);
}
break;
case NodeKind::FRAGMENT_SPREAD:
/** @var FragmentSpreadNode $node */
case $node instanceof FragmentSpreadNode:
$fragment = $this->getFragment($node);
if ($fragment !== null) {

View File

@ -9,7 +9,6 @@ use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Introspection;
@ -114,8 +113,8 @@ abstract class QuerySecurityRule extends ValidationRule
$_astAndDefs = $astAndDefs ?: new ArrayObject();
foreach ($selectionSet->selections as $selection) {
switch ($selection->kind) {
case NodeKind::FIELD:
switch (true) {
case $selection instanceof FieldNode:
/** @var FieldNode $selection */
$fieldName = $selection->name->value;
$fieldDef = null;
@ -142,7 +141,7 @@ abstract class QuerySecurityRule extends ValidationRule
// create field context
$_astAndDefs[$responseName][] = [$selection, $fieldDef];
break;
case NodeKind::INLINE_FRAGMENT:
case $selection instanceof InlineFragmentNode:
/** @var InlineFragmentNode $selection */
$_astAndDefs = $this->collectFieldASTsAndDefs(
$context,
@ -152,7 +151,7 @@ abstract class QuerySecurityRule extends ValidationRule
$_astAndDefs
);
break;
case NodeKind::FRAGMENT_SPREAD:
case $selection instanceof FragmentSpreadNode:
/** @var FragmentSpreadNode $selection */
$fragName = $selection->name->value;

View File

@ -199,7 +199,7 @@ class ValidationContext
for ($i = 0, $selectionCount = count($set->selections); $i < $selectionCount; $i++) {
$selection = $set->selections[$i];
if ($selection->kind === NodeKind::FRAGMENT_SPREAD) {
if ($selection instanceof FragmentSpreadNode) {
$spreads[] = $selection;
} elseif ($selection->selectionSet) {
$setsToVisit[] = $selection->selectionSet;
@ -223,7 +223,7 @@ class ValidationContext
if (! $fragments) {
$fragments = [];
foreach ($this->getDocument()->definitions as $statement) {
if ($statement->kind !== NodeKind::FRAGMENT_DEFINITION) {
if (! ($statement instanceof FragmentDefinitionNode)) {
continue;
}

View File

@ -362,7 +362,7 @@ class CollectorTest extends TestCase
$operationName = null;
foreach ($documentNode->definitions as $definitionNode) {
/** @var Node $definitionNode */
if ($definitionNode->kind === NodeKind::OPERATION_DEFINITION) {
if ($definitionNode instanceof OperationDefinitionNode) {
/** @var OperationDefinitionNode $definitionNode */
self::assertNotNull($definitionNode->name);
$operationName = $definitionNode->name->value;

View File

@ -384,7 +384,7 @@ class VisitorTest extends ValidatorTestCase
$this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, $node->value ?? null];
if ($node->kind === NodeKind::NAME && $node->value === 'x') {
if ($node instanceof NameNode && $node->value === 'x') {
return Visitor::stop();
}
},