Completed
Pull Request — master (#34)
by David
02:32 queued 55s
created

Schema::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
use GraphQL\Type\Definition\ObjectType;
7
use GraphQL\Type\SchemaConfig;
8
use TheCodingMachine\GraphQL\Controllers\Registry\Registry;
9
10
/**
11
 * A GraphQL schema that takes into constructor argument a QueryProvider.
12
 *
13
 * TODO: turn this into a SchemaFactory (cleaner than extending a class)
14
 */
15
class Schema extends \GraphQL\Type\Schema
16
{
17
    public function __construct(QueryProviderInterface $queryProvider, Registry $registry, SchemaConfig $config = null)
18
    {
19
        if ($config === null) {
20
            $config = SchemaConfig::create();
21
        }
22
23
        $query = new ObjectType([
24
            'name' => 'Query',
25
            'fields' => function() use ($queryProvider) {
26
                return $queryProvider->getQueries();
27
            }
28
        ]);
29
        $mutation = new ObjectType([
30
            'name' => 'Mutation',
31
            'fields' => function() use ($queryProvider) {
32
                return $queryProvider->getMutations();
33
            }
34
        ]);
35
36
        $config->setQuery($query);
37
        $config->setMutation($mutation);
38
        $config->setTypeLoader(function(string $name) use ($registry) {
39
            return $registry->get($name);
40
        });
41
42
        parent::__construct($config);
43
    }
44
}
45