diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 837628a..7924783 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -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; diff --git a/src/Experimental/Executor/Collector.php b/src/Experimental/Executor/Collector.php index 34f1e0e..a342b89 100644 --- a/src/Experimental/Executor/Collector.php +++ b/src/Experimental/Executor/Collector.php @@ -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) { diff --git a/src/Language/AST/ListTypeNode.php b/src/Language/AST/ListTypeNode.php index 104810a..29e81f9 100644 --- a/src/Language/AST/ListTypeNode.php +++ b/src/Language/AST/ListTypeNode.php @@ -9,6 +9,6 @@ class ListTypeNode extends Node implements TypeNode /** @var string */ public $kind = NodeKind::LIST_TYPE; - /** @var Node */ + /** @var TypeNode */ public $type; } diff --git a/src/Language/AST/NonNullTypeNode.php b/src/Language/AST/NonNullTypeNode.php index f8b97c0..36b61e5 100644 --- a/src/Language/AST/NonNullTypeNode.php +++ b/src/Language/AST/NonNullTypeNode.php @@ -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; } diff --git a/src/Language/Printer.php b/src/Language/Printer.php index ff11d50..b948ec4 100644 --- a/src/Language/Printer.php +++ b/src/Language/Printer.php @@ -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; }, diff --git a/src/Utils/AST.php b/src/Utils/AST.php index 5ef4233..2b77530 100644 --- a/src/Utils/AST.php +++ b/src/Utils/AST.php @@ -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(); diff --git a/src/Utils/ASTDefinitionBuilder.php b/src/Utils/ASTDefinitionBuilder.php index 8e452e2..6796017 100644 --- a/src/Utils/ASTDefinitionBuilder.php +++ b/src/Utils/ASTDefinitionBuilder.php @@ -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)); diff --git a/src/Utils/BuildSchema.php b/src/Utils/BuildSchema.php index cf0eb7b..3039aef 100644 --- a/src/Utils/BuildSchema.php +++ b/src/Utils/BuildSchema.php @@ -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, diff --git a/src/Utils/SchemaExtender.php b/src/Utils/SchemaExtender.php index fab006e..87411f0 100644 --- a/src/Utils/SchemaExtender.php +++ b/src/Utils/SchemaExtender.php @@ -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 . '".', diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index b187511..a1d6be7 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -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; } diff --git a/src/Validator/Rules/KnownArgumentNames.php b/src/Validator/Rules/KnownArgumentNames.php index 96fd800..638f11c 100644 --- a/src/Validator/Rules/KnownArgumentNames.php +++ b/src/Validator/Rules/KnownArgumentNames.php @@ -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( diff --git a/src/Validator/Rules/KnownDirectives.php b/src/Validator/Rules/KnownDirectives.php index 927ce26..e986c09 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -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 diff --git a/src/Validator/Rules/LoneAnonymousOperation.php b/src/Validator/Rules/LoneAnonymousOperation.php index 40ff821..e875309 100644 --- a/src/Validator/Rules/LoneAnonymousOperation.php +++ b/src/Validator/Rules/LoneAnonymousOperation.php @@ -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; } ); diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index 6a3da59..ca4df0a 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -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) { diff --git a/src/Validator/Rules/QueryDepth.php b/src/Validator/Rules/QueryDepth.php index 5a35f0c..3f74edd 100644 --- a/src/Validator/Rules/QueryDepth.php +++ b/src/Validator/Rules/QueryDepth.php @@ -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) { diff --git a/src/Validator/Rules/QuerySecurityRule.php b/src/Validator/Rules/QuerySecurityRule.php index c5f0260..2837ed7 100644 --- a/src/Validator/Rules/QuerySecurityRule.php +++ b/src/Validator/Rules/QuerySecurityRule.php @@ -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; diff --git a/src/Validator/ValidationContext.php b/src/Validator/ValidationContext.php index 7163371..7564af4 100644 --- a/src/Validator/ValidationContext.php +++ b/src/Validator/ValidationContext.php @@ -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; } diff --git a/tests/Experimental/Executor/CollectorTest.php b/tests/Experimental/Executor/CollectorTest.php index c078cab..1339e57 100644 --- a/tests/Experimental/Executor/CollectorTest.php +++ b/tests/Experimental/Executor/CollectorTest.php @@ -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; diff --git a/tests/Language/VisitorTest.php b/tests/Language/VisitorTest.php index 4a04326..b04b4db 100644 --- a/tests/Language/VisitorTest.php +++ b/tests/Language/VisitorTest.php @@ -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(); } },