Fix: type safety for TypeInfo

This commit is contained in:
Vladimir Razuvaev 2017-07-04 13:25:01 +07:00
parent 4c96193027
commit 32376dd6ee
2 changed files with 13 additions and 16 deletions

View File

@ -388,12 +388,7 @@ class TypeInfo
switch ($node->kind) { switch ($node->kind) {
case NodeKind::SELECTION_SET: case NodeKind::SELECTION_SET:
$namedType = Type::getNamedType($this->getType()); $namedType = Type::getNamedType($this->getType());
$compositeType = null; $this->parentTypeStack[] = Type::isCompositeType($namedType) ? $namedType : null;
if (Type::isCompositeType($namedType)) {
// isCompositeType is a type refining predicate, so this is safe.
$compositeType = $namedType;
}
$this->parentTypeStack[] = $compositeType; // push
break; break;
case NodeKind::FIELD: case NodeKind::FIELD:
@ -426,12 +421,12 @@ class TypeInfo
case NodeKind::FRAGMENT_DEFINITION: case NodeKind::FRAGMENT_DEFINITION:
$typeConditionNode = $node->typeCondition; $typeConditionNode = $node->typeCondition;
$outputType = $typeConditionNode ? self::typeFromAST($schema, $typeConditionNode) : $this->getType(); $outputType = $typeConditionNode ? self::typeFromAST($schema, $typeConditionNode) : $this->getType();
$this->typeStack[] = $outputType; // push $this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null; // push
break; break;
case NodeKind::VARIABLE_DEFINITION: case NodeKind::VARIABLE_DEFINITION:
$inputType = self::typeFromAST($schema, $node->type); $inputType = self::typeFromAST($schema, $node->type);
$this->inputTypeStack[] = $inputType; // push $this->inputTypeStack[] = Type::isInputType($inputType) ? $inputType : null; // push
break; break;
case NodeKind::ARGUMENT: case NodeKind::ARGUMENT:

View File

@ -7,6 +7,7 @@ use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Printer; use GraphQL\Language\Printer;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Utils\TypeInfo;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
class FragmentsOnCompositeTypes class FragmentsOnCompositeTypes
@ -25,17 +26,18 @@ class FragmentsOnCompositeTypes
{ {
return [ return [
NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) use ($context) { NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) use ($context) {
$type = $context->getType(); if ($node->typeCondition) {
$type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition);
if ($node->typeCondition && $type && !Type::isCompositeType($type)) { if ($type && !Type::isCompositeType($type)) {
$context->reportError(new Error( $context->reportError(new Error(
static::inlineFragmentOnNonCompositeErrorMessage($type), static::inlineFragmentOnNonCompositeErrorMessage($type),
[$node->typeCondition] [$node->typeCondition]
)); ));
}
} }
}, },
NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $node) use ($context) { NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $node) use ($context) {
$type = $context->getType(); $type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition);
if ($type && !Type::isCompositeType($type)) { if ($type && !Type::isCompositeType($type)) {
$context->reportError(new Error( $context->reportError(new Error(