1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Schema; |
12
|
|
|
|
13
|
|
|
use GraphQL\Type\Schema; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
18
|
|
|
use Ynlo\GraphQLBundle\Type\Types; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* GraphQL Schema compiler |
22
|
|
|
* |
23
|
|
|
* Compile all definitions into graphql-php schema |
24
|
|
|
*/ |
25
|
|
|
class SchemaCompiler implements ContainerAwareInterface |
26
|
|
|
{ |
27
|
|
|
use ContainerAwareTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DefinitionRegistry |
31
|
|
|
*/ |
32
|
|
|
protected $registry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Endpoint |
36
|
|
|
*/ |
37
|
|
|
protected $endpoint; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SchemaCompiler constructor. |
41
|
|
|
* |
42
|
|
|
* @param DefinitionRegistry $registry |
43
|
|
|
*/ |
44
|
20 |
|
public function __construct(DefinitionRegistry $registry) |
45
|
|
|
{ |
46
|
20 |
|
$this->registry = $registry; |
47
|
20 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Schema |
51
|
|
|
*/ |
52
|
20 |
|
public function compile(): Schema |
53
|
|
|
{ |
54
|
20 |
|
$this->endpoint = $this->registry->getEndpoint(); |
55
|
20 |
|
Types::setUp($this->container, $this->endpoint); |
56
|
|
|
|
57
|
|
|
//automatically create all interface implementors |
58
|
|
|
//to avoid empty interfaces |
59
|
20 |
|
foreach ($this->endpoint->allInterfaces() as $type) { |
60
|
20 |
|
foreach ($type->getImplementors() as $implementor) { |
61
|
20 |
|
if (!Types::has($implementor)) { |
62
|
20 |
|
Types::create($implementor); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$config = [ |
68
|
20 |
|
'types' => Types::all(), |
69
|
20 |
|
'typeLoader' => function ($name) { |
70
|
20 |
|
return Types::get($name); |
71
|
20 |
|
}, |
72
|
|
|
]; |
73
|
|
|
|
74
|
20 |
|
if ($this->endpoint->allQueries()) { |
75
|
20 |
|
$config['query'] = Types::get('Query'); |
76
|
|
|
} |
77
|
|
|
|
78
|
20 |
|
if ($this->endpoint->allMutations()) { |
79
|
20 |
|
$config['mutation'] = Types::get('Mutation'); |
80
|
|
|
} |
81
|
|
|
|
82
|
20 |
|
if (isset($config['query']) || isset($config['mutation'])) { |
83
|
20 |
|
return new Schema($config); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new \RuntimeException('Your GraphQL schema is empty. Create your first object and query and try again'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|