Completed
Push — master ( 8085b1...79cabf )
by David
17s
created

TypeResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mapNameToType() 0 12 3
A registerSchema() 0 3 1
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Types;
5
6
use GraphQL\Type\Definition\Type;
7
use GraphQL\Type\Schema;
8
use RuntimeException;
9
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException;
10
11
/**
12
 * Resolves a type by its GraphQL name.
13
 *
14
 * Unlike the TypeMappers, this class can resolve standard GraphQL types (like String, ID, etc...)
15
 */
16
class TypeResolver
17
{
18
    /**
19
     * @var Schema
20
     */
21
    private $schema;
22
23
    public function registerSchema(Schema $schema)
24
    {
25
        $this->schema = $schema;
26
    }
27
28
    /**
29
     * @param string $typeName
30
     * @return Type
31
     * @throws CannotMapTypeException
32
     */
33
    public function mapNameToType(string $typeName): Type
34
    {
35
        if ($this->schema === null) {
36
            throw new RuntimeException('You must register a schema first before resolving types.');
37
        }
38
        
39
        $type = $this->schema->getType($typeName);
40
        if ($type === null) {
41
            throw CannotMapTypeException::createForName($typeName);
42
        }
43
44
        return $type;
45
    }
46
}
47