Passed
Pull Request — master (#209)
by Christoffer
02:51
created

TypeASTConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B convert() 0 19 6
1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
use Digia\GraphQL\Error\ConversionException;
6
use Digia\GraphQL\Error\InvalidTypeException;
7
use Digia\GraphQL\Error\InvariantException;
8
use Digia\GraphQL\Language\Node\ListTypeNode;
9
use Digia\GraphQL\Language\Node\NamedTypeNode;
10
use Digia\GraphQL\Language\Node\NonNullTypeNode;
11
use Digia\GraphQL\Language\Node\TypeNodeInterface;
12
use Digia\GraphQL\Schema\SchemaInterface;
13
use Digia\GraphQL\Type\Definition\TypeInterface;
14
use function Digia\GraphQL\Type\newList;
15
use function Digia\GraphQL\Type\newNonNull;
16
17
class TypeASTConverter
18
{
19
    /**
20
     * Given a Schema and an AST node describing a type, return a GraphQLType
21
     * definition which applies to that type. For example, if provided the parsed
22
     * AST node for `[User]`, a GraphQLList instance will be returned, containing
23
     * the type called "User" found in the schema. If a type called "User" is not
24
     * found in the schema, then undefined will be returned.
25
     *
26
     * @param SchemaInterface   $schema
27
     * @param TypeNodeInterface $typeNode
28
     * @return TypeInterface|null
29
     * @throws InvariantException
30
     * @throws ConversionException
31
     */
32
    public function convert(SchemaInterface $schema, TypeNodeInterface $typeNode): ?TypeInterface
33
    {
34
        $innerType = null;
35
36
        if ($typeNode instanceof ListTypeNode) {
37
            $innerType = $this->convert($schema, $typeNode->getType());
38
            return null !== $innerType ? newList($innerType) : null;
39
        }
40
41
        if ($typeNode instanceof NonNullTypeNode) {
42
            $innerType = $this->convert($schema, $typeNode->getType());
43
            return null !== $innerType ? newNonNull($innerType) : null;
44
        }
45
46
        if ($typeNode instanceof NamedTypeNode) {
47
            return $schema->getType($typeNode->getNameValue());
48
        }
49
50
        throw new ConversionException(sprintf('Unexpected type kind: %s', $typeNode->getKind()));
51
    }
52
}
53