@@ -22,8 +22,16 @@ public function __construct(TypeResolver $typeResolver, bool $enableValidation =
22
22
$ this ->enableValidation = $ enableValidation ;
23
23
}
24
24
25
- public function getBuilder (string $ name , ?string $ queryAlias , ?string $ mutationAlias = null , ?string $ subscriptionAlias = null , array $ types = []): Closure
26
- {
25
+ /**
26
+ * @param string[] $types
27
+ */
28
+ public function getBuilder (
29
+ string $ name ,
30
+ ?string $ queryAlias ,
31
+ ?string $ mutationAlias = null ,
32
+ ?string $ subscriptionAlias = null ,
33
+ array $ types = []
34
+ ): Closure {
27
35
return function () use ($ name , $ queryAlias , $ mutationAlias , $ subscriptionAlias , $ types ): ExtensibleSchema {
28
36
static $ schema = null ;
29
37
if (null === $ schema ) {
@@ -37,14 +45,22 @@ public function getBuilder(string $name, ?string $queryAlias, ?string $mutationA
37
45
/**
38
46
* @param string[] $types
39
47
*/
40
- public function create (string $ name , ?string $ queryAlias , ?string $ mutationAlias = null , ?string $ subscriptionAlias = null , array $ types = []): ExtensibleSchema
41
- {
48
+ public function create (
49
+ string $ name ,
50
+ ?string $ queryAlias ,
51
+ ?string $ mutationAlias = null ,
52
+ ?string $ subscriptionAlias = null ,
53
+ array $ types = []
54
+ ): ExtensibleSchema {
42
55
$ this ->typeResolver ->setCurrentSchemaName ($ name );
43
56
$ query = $ this ->typeResolver ->resolve ($ queryAlias );
44
57
$ mutation = $ this ->typeResolver ->resolve ($ mutationAlias );
45
58
$ subscription = $ this ->typeResolver ->resolve ($ subscriptionAlias );
46
59
47
- $ schema = new ExtensibleSchema ($ this ->buildSchemaArguments ($ name , $ query , $ mutation , $ subscription , $ types ));
60
+ /** @var class-string<ExtensibleSchema> $class */
61
+ $ class = $ this ->getSchemaClass ();
62
+
63
+ $ schema = new $ class ($ this ->buildSchemaArguments ($ name , $ query , $ mutation , $ subscription , $ types ));
48
64
$ extensions = [];
49
65
50
66
if ($ this ->enableValidation ) {
@@ -55,22 +71,53 @@ public function create(string $name, ?string $queryAlias, ?string $mutationAlias
55
71
return $ schema ;
56
72
}
57
73
58
- private function buildSchemaArguments (string $ schemaName , Type $ query , ?Type $ mutation , ?Type $ subscription , array $ types = []): array
59
- {
74
+ /**
75
+ * @param string[] $types
76
+ *
77
+ * @return array<string,mixed>
78
+ */
79
+ protected function buildSchemaArguments (
80
+ string $ schemaName ,
81
+ ?Type $ query ,
82
+ ?Type $ mutation ,
83
+ ?Type $ subscription ,
84
+ array $ types = []
85
+ ): array {
60
86
return [
61
87
'query ' => $ query ,
62
88
'mutation ' => $ mutation ,
63
89
'subscription ' => $ subscription ,
64
- 'typeLoader ' => function ($ name ) use ($ schemaName ) {
65
- $ this ->typeResolver ->setCurrentSchemaName ($ schemaName );
90
+ 'typeLoader ' => $ this ->createTypeLoaderClosure ($ schemaName ),
91
+ 'types ' => $ this ->createTypesClosure ($ schemaName , $ types ),
92
+ ];
93
+ }
66
94
67
- return $ this -> typeResolver -> resolve ( $ name );
68
- },
69
- ' types ' => function () use ($ types , $ schemaName ) {
70
- $ this ->typeResolver ->setCurrentSchemaName ($ schemaName );
95
+ protected function createTypeLoaderClosure ( string $ schemaName ): Closure
96
+ {
97
+ return function ($ name ) use ($ schemaName ): ? Type {
98
+ $ this ->typeResolver ->setCurrentSchemaName ($ schemaName );
71
99
72
- return array_map ([$ this ->typeResolver , 'resolve ' ], $ types );
73
- },
74
- ];
100
+ return $ this ->typeResolver ->resolve ($ name );
101
+ };
102
+ }
103
+
104
+ /**
105
+ * @param string[] $types
106
+ */
107
+ protected function createTypesClosure (string $ schemaName , array $ types ): Closure
108
+ {
109
+ return function () use ($ types , $ schemaName ): array {
110
+ $ this ->typeResolver ->setCurrentSchemaName ($ schemaName );
111
+
112
+ return array_map (fn (string $ x ): ?Type => $ this ->typeResolver ->resolve ($ x ), $ types );
113
+ };
114
+ }
115
+
116
+ /**
117
+ * @return class-string<ExtensibleSchema>
118
+ */
119
+ protected function getSchemaClass (): string
120
+ {
121
+ return ExtensibleSchema::class;
75
122
}
76
123
}
0 commit comments