51
51
*/
52
52
class Schema
53
53
{
54
- /** @var SchemaConfig */
55
- private $ config ;
54
+ private SchemaConfig $ config ;
56
55
57
56
/**
58
57
* Contains currently resolved schema types
59
58
*
60
- * @var Type[]
59
+ * @var array<string, Type>
61
60
*/
62
- private $ resolvedTypes = [];
61
+ private array $ resolvedTypes = [];
63
62
64
63
/**
65
64
* Lazily initialised.
66
65
*
67
66
* @var array<string, InterfaceImplementations>
68
67
*/
69
- private $ implementationsMap ;
68
+ private array $ implementationsMap ;
70
69
71
70
/**
72
- * True when $resolvedTypes contain all possible schema types
73
- *
74
- * @var bool
71
+ * True when $resolvedTypes contains all possible schema types.
75
72
*/
76
- private $ fullyLoaded = false ;
73
+ private bool $ fullyLoaded = false ;
77
74
78
- /** @var Error[] */
79
- private $ validationErrors ;
75
+ /** @var array<int, Error> */
76
+ private array $ validationErrors ;
80
77
81
78
/** @var array<int, SchemaTypeExtensionNode> */
82
79
public $ extensionASTNodes = [];
83
80
84
81
/**
85
- * @param mixed[]| SchemaConfig $config
82
+ * @param SchemaConfig|array<string, mixed> $config
86
83
*
87
84
* @api
88
85
*/
@@ -128,31 +125,38 @@ public function __construct($config)
128
125
$ this ->config = $ config ;
129
126
$ this ->extensionASTNodes = $ config ->extensionASTNodes ;
130
127
131
- if ($ config ->query !== null ) {
132
- $ this ->resolvedTypes [$ config ->query ->name ] = $ config ->query ;
128
+ // TODO can we make the following assumption hold true?
129
+ // No need to check for the existence of the root query type
130
+ // since we already validated the schema thus it must exist.
131
+ $ query = $ config ->query ;
132
+ if ($ query !== null ) {
133
+ $ this ->resolvedTypes [$ query ->name ] = $ query ;
133
134
}
134
135
135
- if ($ config ->mutation !== null ) {
136
- $ this ->resolvedTypes [$ config ->mutation ->name ] = $ config ->mutation ;
136
+ $ mutation = $ config ->mutation ;
137
+ if ($ mutation !== null ) {
138
+ $ this ->resolvedTypes [$ mutation ->name ] = $ mutation ;
137
139
}
138
140
139
- if ($ config ->subscription !== null ) {
140
- $ this ->resolvedTypes [$ config ->subscription ->name ] = $ config ->subscription ;
141
+ $ subscription = $ config ->subscription ;
142
+ if ($ subscription !== null ) {
143
+ $ this ->resolvedTypes [$ subscription ->name ] = $ subscription ;
141
144
}
142
145
143
146
if (is_array ($ this ->config ->types )) {
144
147
foreach ($ this ->resolveAdditionalTypes () as $ type ) {
145
- if (isset ($ this ->resolvedTypes [$ type ->name ])) {
148
+ $ typeName = $ type ->name ;
149
+ if (isset ($ this ->resolvedTypes [$ typeName ])) {
146
150
Utils::invariant (
147
- $ type === $ this ->resolvedTypes [$ type -> name ],
151
+ $ type === $ this ->resolvedTypes [$ typeName ],
148
152
sprintf (
149
153
'Schema must contain unique named types but contains multiple types named "%s" (see https://webonyx.github.io/graphql-php/type-definitions/#type-registry). ' ,
150
154
$ type
151
155
)
152
156
);
153
157
}
154
158
155
- $ this ->resolvedTypes [$ type -> name ] = $ type ;
159
+ $ this ->resolvedTypes [$ typeName ] = $ type ;
156
160
}
157
161
}
158
162
@@ -196,12 +200,11 @@ private function resolveAdditionalTypes(): Generator
196
200
}
197
201
198
202
/**
199
- * Returns array of all types in this schema. Keys of this array represent type names, values are instances
200
- * of corresponding type definitions
203
+ * Returns all types in this schema.
201
204
*
202
- * This operation requires full schema scan. Do not use in production environment.
205
+ * This operation requires a full schema scan. Do not use in production environment.
203
206
*
204
- * @return array<string, Type>
207
+ * @return array<string, Type> Keys represent type names, values are instances of corresponding type definitions
205
208
*
206
209
* @api
207
210
*/
@@ -216,9 +219,9 @@ public function getTypeMap(): array
216
219
}
217
220
218
221
/**
219
- * @return Type[]
222
+ * @return array< Type>
220
223
*/
221
- private function collectAllTypes ()
224
+ private function collectAllTypes (): array
222
225
{
223
226
$ typeMap = [];
224
227
foreach ($ this ->resolvedTypes as $ type ) {
@@ -246,21 +249,16 @@ private function collectAllTypes()
246
249
/**
247
250
* Returns a list of directives supported by this schema
248
251
*
249
- * @return Directive[]
252
+ * @return array< Directive>
250
253
*
251
254
* @api
252
255
*/
253
- public function getDirectives ()
256
+ public function getDirectives (): array
254
257
{
255
258
return $ this ->config ->directives ?? GraphQL::getStandardDirectives ();
256
259
}
257
260
258
- /**
259
- * @param string $operation
260
- *
261
- * @return ObjectType|null
262
- */
263
- public function getOperationType ($ operation )
261
+ public function getOperationType (string $ operation ): ?ObjectType
264
262
{
265
263
switch ($ operation ) {
266
264
case 'query ' :
@@ -278,53 +276,45 @@ public function getOperationType($operation)
278
276
}
279
277
280
278
/**
281
- * Returns schema query type
282
- *
283
- * @return ObjectType|null
279
+ * Returns root query type.
284
280
*
285
281
* @api
286
282
*/
287
- public function getQueryType (): ?Type
283
+ public function getQueryType (): ?ObjectType
288
284
{
289
285
return $ this ->config ->query ;
290
286
}
291
287
292
288
/**
293
- * Returns schema mutation type
294
- *
295
- * @return ObjectType|null
289
+ * Returns root mutation type.
296
290
*
297
291
* @api
298
292
*/
299
- public function getMutationType (): ?Type
293
+ public function getMutationType (): ?ObjectType
300
294
{
301
295
return $ this ->config ->mutation ;
302
296
}
303
297
304
298
/**
305
299
* Returns schema subscription
306
300
*
307
- * @return ObjectType|null
308
- *
309
301
* @api
310
302
*/
311
- public function getSubscriptionType (): ?Type
303
+ public function getSubscriptionType (): ?ObjectType
312
304
{
313
305
return $ this ->config ->subscription ;
314
306
}
315
307
316
308
/**
317
- * @return SchemaConfig
318
- *
319
309
* @api
320
310
*/
321
- public function getConfig ()
311
+ public function getConfig (): SchemaConfig
322
312
{
323
313
return $ this ->config ;
324
314
}
325
315
326
316
/**
327
- * Returns type by its name
317
+ * Returns a type by name.
328
318
*
329
319
* @api
330
320
*/
@@ -534,15 +524,15 @@ public function getAstNode(): ?SchemaDefinitionNode
534
524
}
535
525
536
526
/**
537
- * Validates schema.
527
+ * Throws if the schema is not valid .
538
528
*
539
- * This operation requires full schema scan. Do not use in production environment.
529
+ * This operation requires a full schema scan. Do not use in production environment.
540
530
*
541
531
* @throws InvariantViolation
542
532
*
543
533
* @api
544
534
*/
545
- public function assertValid ()
535
+ public function assertValid (): void
546
536
{
547
537
$ errors = $ this ->validate ();
548
538
@@ -574,18 +564,18 @@ public function assertValid()
574
564
}
575
565
576
566
/**
577
- * Validates schema.
567
+ * Validate the schema and return any errors .
578
568
*
579
- * This operation requires full schema scan. Do not use in production environment.
569
+ * This operation requires a full schema scan. Do not use in production environment.
580
570
*
581
- * @return InvariantViolation[]| Error[]
571
+ * @return array<int, Error>
582
572
*
583
573
* @api
584
574
*/
585
- public function validate ()
575
+ public function validate (): array
586
576
{
587
577
// If this Schema has already been validated, return the previous results.
588
- if ($ this ->validationErrors !== null ) {
578
+ if (isset ( $ this ->validationErrors ) ) {
589
579
return $ this ->validationErrors ;
590
580
}
591
581
0 commit comments