Moved typeFromAST() to AST util

This commit is contained in:
Vladimir Razuvaev 2017-08-17 02:14:55 +07:00
parent a50c9a4c1f
commit b4d767bad6
2 changed files with 30 additions and 14 deletions

View File

@ -8,8 +8,12 @@ use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FloatValueNode;
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\NonNullTypeNode;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectFieldNode;
use GraphQL\Language\AST\ObjectValueNode;
@ -24,6 +28,8 @@ use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\LeafType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;
/**
@ -321,6 +327,26 @@ class AST
throw new InvariantViolation('Must be input type');
}
/**
* @param Schema $schema
* @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
* @return Type
* @throws InvariantViolation
*/
public static function typeFromAST(Schema $schema, $inputTypeNode)
{
if ($inputTypeNode instanceof ListTypeNode) {
$innerType = self::typeFromAST($schema, $inputTypeNode->type);
return $innerType ? new ListOfType($innerType) : null;
}
if ($inputTypeNode instanceof NonNullTypeNode) {
$innerType = self::typeFromAST($schema, $inputTypeNode->type);
return $innerType ? new NonNull($innerType) : null;
}
Utils::invariant($inputTypeNode && $inputTypeNode instanceof NamedTypeNode, 'Must be a named type');
return $schema->getType($inputTypeNode->name->value);
}
/**
* Returns true if the provided valueNode is a variable which is not defined

View File

@ -1,6 +1,7 @@
<?php
namespace GraphQL\Utils;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\ListTypeNode;
@ -55,26 +56,15 @@ class TypeInfo
return TypeComparators::doTypesOverlap($schema, $typeA, $typeB);
}
/**
* @param Schema $schema
* @param $inputTypeAST
* @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
* @return Type
* @throws \Exception
* @throws InvariantViolation
*/
public static function typeFromAST(Schema $schema, $inputTypeNode)
{
if ($inputTypeNode instanceof ListTypeNode) {
$innerType = self::typeFromAST($schema, $inputTypeNode->type);
return $innerType ? new ListOfType($innerType) : null;
}
if ($inputTypeNode instanceof NonNullTypeNode) {
$innerType = self::typeFromAST($schema, $inputTypeNode->type);
return $innerType ? new NonNull($innerType) : null;
}
Utils::invariant($inputTypeNode && $inputTypeNode instanceof NamedTypeNode, 'Must be a named type');
return $schema->getType($inputTypeNode->name->value);
return AST::typeFromAST($schema, $inputTypeNode);
}
/**