1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Type; |
6
|
|
|
|
7
|
|
|
use GraphQL\Language\AST\SchemaDefinitionNode; |
8
|
|
|
use GraphQL\Language\AST\SchemaTypeExtensionNode; |
9
|
|
|
use GraphQL\Type\Definition\Directive; |
10
|
|
|
use GraphQL\Type\Definition\ObjectType; |
11
|
|
|
use GraphQL\Type\Definition\Type; |
12
|
|
|
use GraphQL\Utils\Utils; |
13
|
|
|
use function is_callable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Schema configuration class. |
17
|
|
|
* Could be passed directly to schema constructor. List of options accepted by **create** method is |
18
|
|
|
* [described in docs](type-system/schema.md#configuration-options). |
19
|
|
|
* |
20
|
|
|
* Usage example: |
21
|
|
|
* |
22
|
|
|
* $config = SchemaConfig::create() |
23
|
|
|
* ->setQuery($myQueryType) |
24
|
|
|
* ->setTypeLoader($myTypeLoader); |
25
|
|
|
* |
26
|
|
|
* $schema = new Schema($config); |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
class SchemaConfig |
30
|
|
|
{ |
31
|
|
|
/** @var ObjectType */ |
32
|
|
|
public $query; |
33
|
|
|
|
34
|
|
|
/** @var ObjectType */ |
35
|
|
|
public $mutation; |
36
|
|
|
|
37
|
|
|
/** @var ObjectType */ |
38
|
|
|
public $subscription; |
39
|
|
|
|
40
|
|
|
/** @var Type[]|callable */ |
41
|
|
|
public $types; |
42
|
|
|
|
43
|
|
|
/** @var Directive[] */ |
44
|
|
|
public $directives; |
45
|
|
|
|
46
|
|
|
/** @var callable */ |
47
|
|
|
public $typeLoader; |
48
|
|
|
|
49
|
|
|
/** @var SchemaDefinitionNode */ |
50
|
|
|
public $astNode; |
51
|
|
|
|
52
|
|
|
/** @var bool */ |
53
|
|
|
public $assumeValid; |
54
|
|
|
|
55
|
|
|
/** @var SchemaTypeExtensionNode[] */ |
56
|
|
|
public $extensionASTNodes; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Converts an array of options to instance of SchemaConfig |
60
|
|
|
* (or just returns empty config when array is not passed). |
61
|
|
|
* |
62
|
|
|
* @api |
63
|
|
|
* @param mixed[] $options |
64
|
|
|
* @return SchemaConfig |
65
|
|
|
*/ |
66
|
791 |
|
public static function create(array $options = []) |
67
|
|
|
{ |
68
|
791 |
|
$config = new static(); |
69
|
|
|
|
70
|
791 |
|
if (! empty($options)) { |
71
|
789 |
|
if (isset($options['query'])) { |
72
|
780 |
|
$config->setQuery($options['query']); |
73
|
|
|
} |
74
|
|
|
|
75
|
789 |
|
if (isset($options['mutation'])) { |
76
|
76 |
|
$config->setMutation($options['mutation']); |
77
|
|
|
} |
78
|
|
|
|
79
|
789 |
|
if (isset($options['subscription'])) { |
80
|
29 |
|
$config->setSubscription($options['subscription']); |
81
|
|
|
} |
82
|
|
|
|
83
|
789 |
|
if (isset($options['types'])) { |
84
|
187 |
|
$config->setTypes($options['types']); |
85
|
|
|
} |
86
|
|
|
|
87
|
789 |
|
if (isset($options['directives'])) { |
88
|
467 |
|
$config->setDirectives($options['directives']); |
89
|
|
|
} |
90
|
|
|
|
91
|
789 |
|
if (isset($options['typeLoader'])) { |
92
|
123 |
|
Utils::invariant( |
93
|
123 |
|
is_callable($options['typeLoader']), |
94
|
123 |
|
'Schema type loader must be callable if provided but got: %s', |
95
|
123 |
|
Utils::printSafe($options['typeLoader']) |
96
|
|
|
); |
97
|
122 |
|
$config->setTypeLoader($options['typeLoader']); |
98
|
|
|
} |
99
|
|
|
|
100
|
788 |
|
if (isset($options['astNode'])) { |
101
|
16 |
|
$config->setAstNode($options['astNode']); |
102
|
|
|
} |
103
|
|
|
|
104
|
788 |
|
if (isset($options['assumeValid'])) { |
105
|
|
|
$config->setAssumeValid((bool) $options['assumeValid']); |
106
|
|
|
} |
107
|
|
|
|
108
|
788 |
|
if (isset($options['extensionASTNodes'])) { |
109
|
|
|
$config->setExtensionASTNodes($options['extensionASTNodes']); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
790 |
|
return $config; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return SchemaDefinitionNode |
118
|
|
|
*/ |
119
|
6 |
|
public function getAstNode() |
120
|
|
|
{ |
121
|
6 |
|
return $this->astNode; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return SchemaConfig |
126
|
|
|
*/ |
127
|
16 |
|
public function setAstNode(SchemaDefinitionNode $astNode) |
128
|
|
|
{ |
129
|
16 |
|
$this->astNode = $astNode; |
130
|
|
|
|
131
|
16 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @api |
136
|
|
|
* @return ObjectType |
137
|
|
|
*/ |
138
|
|
|
public function getQuery() |
139
|
|
|
{ |
140
|
|
|
return $this->query; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @api |
145
|
|
|
* @param ObjectType $query |
146
|
|
|
* @return SchemaConfig |
147
|
|
|
*/ |
148
|
780 |
|
public function setQuery($query) |
149
|
|
|
{ |
150
|
780 |
|
$this->query = $query; |
151
|
|
|
|
152
|
780 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @api |
157
|
|
|
* @return ObjectType |
158
|
|
|
*/ |
159
|
|
|
public function getMutation() |
160
|
|
|
{ |
161
|
|
|
return $this->mutation; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @api |
166
|
|
|
* @param ObjectType $mutation |
167
|
|
|
* @return SchemaConfig |
168
|
|
|
*/ |
169
|
76 |
|
public function setMutation($mutation) |
170
|
|
|
{ |
171
|
76 |
|
$this->mutation = $mutation; |
172
|
|
|
|
173
|
76 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @api |
178
|
|
|
* @return ObjectType |
179
|
|
|
*/ |
180
|
|
|
public function getSubscription() |
181
|
|
|
{ |
182
|
|
|
return $this->subscription; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @api |
187
|
|
|
* @param ObjectType $subscription |
188
|
|
|
* @return SchemaConfig |
189
|
|
|
*/ |
190
|
29 |
|
public function setSubscription($subscription) |
191
|
|
|
{ |
192
|
29 |
|
$this->subscription = $subscription; |
193
|
|
|
|
194
|
29 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @api |
199
|
|
|
* @return Type[] |
200
|
|
|
*/ |
201
|
|
|
public function getTypes() |
202
|
|
|
{ |
203
|
|
|
return $this->types ?: []; |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @api |
208
|
|
|
* @param Type[]|callable $types |
209
|
|
|
* @return SchemaConfig |
210
|
|
|
*/ |
211
|
187 |
|
public function setTypes($types) |
212
|
|
|
{ |
213
|
187 |
|
$this->types = $types; |
214
|
|
|
|
215
|
187 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @api |
220
|
|
|
* @return Directive[] |
221
|
|
|
*/ |
222
|
|
|
public function getDirectives() |
223
|
|
|
{ |
224
|
|
|
return $this->directives ?: []; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @api |
229
|
|
|
* @param Directive[] $directives |
230
|
|
|
* @return SchemaConfig |
231
|
|
|
*/ |
232
|
467 |
|
public function setDirectives(array $directives) |
233
|
|
|
{ |
234
|
467 |
|
$this->directives = $directives; |
235
|
|
|
|
236
|
467 |
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @api |
241
|
|
|
* @return callable |
242
|
|
|
*/ |
243
|
|
|
public function getTypeLoader() |
244
|
|
|
{ |
245
|
|
|
return $this->typeLoader; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @api |
250
|
|
|
* @return SchemaConfig |
251
|
|
|
*/ |
252
|
122 |
|
public function setTypeLoader(callable $typeLoader) |
253
|
|
|
{ |
254
|
122 |
|
$this->typeLoader = $typeLoader; |
255
|
|
|
|
256
|
122 |
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
790 |
|
public function getAssumeValid() |
263
|
|
|
{ |
264
|
790 |
|
return $this->assumeValid; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param bool $assumeValid |
269
|
|
|
* @return SchemaConfig |
270
|
|
|
*/ |
271
|
|
|
public function setAssumeValid($assumeValid) |
272
|
|
|
{ |
273
|
|
|
$this->assumeValid = $assumeValid; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return SchemaTypeExtensionNode[] |
280
|
|
|
*/ |
281
|
|
|
public function getExtensionASTNodes() |
282
|
|
|
{ |
283
|
|
|
return $this->extensionASTNodes; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param SchemaTypeExtensionNode[] $extensionASTNodes |
288
|
|
|
*/ |
289
|
|
|
public function setExtensionASTNodes(array $extensionASTNodes) |
290
|
|
|
{ |
291
|
|
|
$this->extensionASTNodes = $extensionASTNodes; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|