1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Railt package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Railt\SDL\Backend; |
13
|
|
|
|
14
|
|
|
use GraphQL\Contracts\TypeSystem\DirectiveInterface; |
15
|
|
|
use GraphQL\Contracts\TypeSystem\SchemaInterface; |
16
|
|
|
use GraphQL\Contracts\TypeSystem\Type\NamedTypeInterface; |
17
|
|
|
use GraphQL\Contracts\TypeSystem\Type\TypeInterface; |
18
|
|
|
use Railt\SDL\Backend\Context\TypeLocatorInterface; |
19
|
|
|
use Railt\SDL\Backend\NameResolver\NameResolverInterface; |
20
|
|
|
use Railt\TypeSystem\Schema; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ExecutionContext |
24
|
|
|
*/ |
25
|
|
|
class ExecutionContext implements ContextInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array|TypeLocatorInterface[] |
29
|
|
|
*/ |
30
|
|
|
public array $types; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array|TypeLocatorInterface[] |
34
|
|
|
*/ |
35
|
|
|
public array $directives; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Schema |
39
|
|
|
*/ |
40
|
|
|
private Schema $schema; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var NameResolverInterface |
44
|
|
|
*/ |
45
|
|
|
private NameResolverInterface $resolver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* GlobalContext constructor. |
49
|
|
|
* |
50
|
|
|
* @param NameResolverInterface $resolver |
51
|
|
|
* @param Schema $schema |
52
|
|
|
*/ |
53
|
|
|
public function __construct(NameResolverInterface $resolver, Schema $schema) |
54
|
|
|
{ |
55
|
|
|
$this->schema = $schema; |
56
|
|
|
$this->resolver = $resolver; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Schema $schema |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function setSchema(Schema $schema): void |
64
|
|
|
{ |
65
|
|
|
$this->schema = $schema; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return SchemaInterface |
70
|
|
|
*/ |
71
|
|
|
public function getSchema(): SchemaInterface |
72
|
|
|
{ |
73
|
|
|
return $this->schema; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param TypeLocatorInterface $context |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function addType(TypeLocatorInterface $context): void |
81
|
|
|
{ |
82
|
|
|
if ($context->getGenericArguments() === []) { |
83
|
|
|
/** @var NamedTypeInterface $type */ |
84
|
|
|
$type = $context->build([]); |
85
|
|
|
|
86
|
|
|
$this->schema->addType($type); |
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->types[$context->getName()] = $context; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $type |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function hasType(string $type): bool |
99
|
|
|
{ |
100
|
|
|
return isset($this->types[$type]) |
101
|
|
|
|| $this->schema->hasType($type); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $type |
106
|
|
|
* @param array|string[] $args |
107
|
|
|
* @return TypeInterface |
108
|
|
|
*/ |
109
|
|
|
public function fetchType(string $type, array $args = []): TypeInterface |
110
|
|
|
{ |
111
|
|
|
if ($this->schema->hasType($type)) { |
112
|
|
|
return $this->schema->getType($type); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->types[$type]->build($args); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param TypeLocatorInterface $context |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function addDirective(TypeLocatorInterface $context): void |
123
|
|
|
{ |
124
|
|
|
if ($context->getGenericArguments() === []) { |
125
|
|
|
/** @var DirectiveInterface $directive */ |
126
|
|
|
$directive = $context->build([]); |
127
|
|
|
|
128
|
|
|
$this->schema->addDirective($directive); |
129
|
|
|
|
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->directives[$context->getName()] = $context; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $type |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function hasDirective(string $type): bool |
141
|
|
|
{ |
142
|
|
|
return isset($this->directives[$type]) |
143
|
|
|
|| $this->schema->getDirective($type); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $type |
148
|
|
|
* @param array $args |
149
|
|
|
* @return DirectiveInterface|NamedTypeInterface |
150
|
|
|
*/ |
151
|
|
|
public function fetchDirective(string $type, array $args = []): DirectiveInterface |
152
|
|
|
{ |
153
|
|
|
if ($directive = $this->schema->getDirective($type)) { |
154
|
|
|
return $directive; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->directives[$type]->build($args); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|