From 32376dd6eeab8379d03c27ece32a3b074f1b92a0 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 4 Jul 2017 13:25:01 +0700 Subject: [PATCH] Fix: type safety for TypeInfo --- src/Utils/TypeInfo.php | 11 +++-------- .../Rules/FragmentsOnCompositeTypes.php | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index 7205720..2857b0b 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -388,12 +388,7 @@ class TypeInfo switch ($node->kind) { case NodeKind::SELECTION_SET: $namedType = Type::getNamedType($this->getType()); - $compositeType = null; - if (Type::isCompositeType($namedType)) { - // isCompositeType is a type refining predicate, so this is safe. - $compositeType = $namedType; - } - $this->parentTypeStack[] = $compositeType; // push + $this->parentTypeStack[] = Type::isCompositeType($namedType) ? $namedType : null; break; case NodeKind::FIELD: @@ -426,12 +421,12 @@ class TypeInfo case NodeKind::FRAGMENT_DEFINITION: $typeConditionNode = $node->typeCondition; $outputType = $typeConditionNode ? self::typeFromAST($schema, $typeConditionNode) : $this->getType(); - $this->typeStack[] = $outputType; // push + $this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null; // push break; case NodeKind::VARIABLE_DEFINITION: $inputType = self::typeFromAST($schema, $node->type); - $this->inputTypeStack[] = $inputType; // push + $this->inputTypeStack[] = Type::isInputType($inputType) ? $inputType : null; // push break; case NodeKind::ARGUMENT: diff --git a/src/Validator/Rules/FragmentsOnCompositeTypes.php b/src/Validator/Rules/FragmentsOnCompositeTypes.php index a57aaf5..1d6e450 100644 --- a/src/Validator/Rules/FragmentsOnCompositeTypes.php +++ b/src/Validator/Rules/FragmentsOnCompositeTypes.php @@ -7,6 +7,7 @@ use GraphQL\Language\AST\InlineFragmentNode; use GraphQL\Language\AST\NodeKind; use GraphQL\Language\Printer; use GraphQL\Type\Definition\Type; +use GraphQL\Utils\TypeInfo; use GraphQL\Validator\ValidationContext; class FragmentsOnCompositeTypes @@ -25,17 +26,18 @@ class FragmentsOnCompositeTypes { return [ NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) use ($context) { - $type = $context->getType(); - - if ($node->typeCondition && $type && !Type::isCompositeType($type)) { - $context->reportError(new Error( - static::inlineFragmentOnNonCompositeErrorMessage($type), - [$node->typeCondition] - )); + if ($node->typeCondition) { + $type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition); + if ($type && !Type::isCompositeType($type)) { + $context->reportError(new Error( + static::inlineFragmentOnNonCompositeErrorMessage($type), + [$node->typeCondition] + )); + } } }, NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $node) use ($context) { - $type = $context->getType(); + $type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition); if ($type && !Type::isCompositeType($type)) { $context->reportError(new Error(