11<?php
22namespace GraphQL ;
33
4- use GraphQL \Schema \Config ;
5- use GraphQL \Schema \Descriptor ;
4+ use GraphQL \Type \Descriptor ;
65use GraphQL \Type \Definition \AbstractType ;
76use GraphQL \Type \Definition \Directive ;
87use GraphQL \Type \Definition \InterfaceType ;
@@ -83,12 +82,13 @@ public function __construct($config = null)
8382 );
8483 list ($ queryType , $ mutationType , $ subscriptionType ) = func_get_args () + [null , null , null ];
8584
86- $ config = Config::create ()
87- ->setQuery ($ queryType )
88- ->setMutation ($ mutationType )
89- ->setSubscription ($ subscriptionType )
90- ;
91- } else if (is_array ($ config )) {
85+ $ config = [
86+ 'query ' => $ queryType ,
87+ 'mutation ' => $ mutationType ,
88+ 'subscription ' => $ subscriptionType
89+ ];
90+ }
91+ if (is_array ($ config )) {
9292 $ config = Config::create ($ config );
9393 }
9494
@@ -101,8 +101,8 @@ public function __construct($config = null)
101101 'subscription ' ,
102102 'types ' ,
103103 'directives ' ,
104- 'descriptor ' ,
105- 'typeLoader '
104+ 'typeLoader ' ,
105+ 'descriptor '
106106 ]),
107107 Utils::getVariableType ($ config )
108108 );
@@ -116,6 +116,8 @@ public function __construct($config = null)
116116 }
117117
118118 /**
119+ * Returns schema query type
120+ *
119121 * @return ObjectType
120122 */
121123 public function getQueryType ()
@@ -124,6 +126,8 @@ public function getQueryType()
124126 }
125127
126128 /**
129+ * Returns schema mutation type
130+ *
127131 * @return ObjectType|null
128132 */
129133 public function getMutationType ()
@@ -132,6 +136,8 @@ public function getMutationType()
132136 }
133137
134138 /**
139+ * Returns schema subscription
140+ *
135141 * @return ObjectType|null
136142 */
137143 public function getSubscriptionType ()
@@ -148,14 +154,16 @@ public function getConfig()
148154 }
149155
150156 /**
151- * Returns full map of types in this schema.
157+ * Returns array of all types in this schema. Keys of this array represent type names, values are instances
158+ * of corresponding type definitions
152159 *
153160 * @return Type[]
154161 */
155162 public function getTypeMap ()
156163 {
157164 if (!$ this ->fullyLoaded ) {
158165 if ($ this ->config ->descriptor && $ this ->config ->typeLoader ) {
166+ // Following is still faster than $this->collectAllTypes() because it won't init fields
159167 $ typesToResolve = array_diff_key ($ this ->config ->descriptor ->typeMap , $ this ->resolvedTypes );
160168 foreach ($ typesToResolve as $ typeName => $ _ ) {
161169 $ this ->resolvedTypes [$ typeName ] = $ this ->loadType ($ typeName );
@@ -185,22 +193,12 @@ public function getType($name)
185193 *
186194 * @return Descriptor
187195 */
188- public function getDescriptor ()
196+ public function describe ()
189197 {
190198 if ($ this ->descriptor ) {
191199 return $ this ->descriptor ;
192200 }
193- if ($ this ->config ->descriptor ) {
194- return $ this ->config ->descriptor ;
195- }
196- return $ this ->descriptor = $ this ->buildDescriptor ();
197- }
198201
199- /**
200- * @return Descriptor
201- */
202- public function buildDescriptor ()
203- {
204202 $ this ->resolvedTypes = $ this ->collectAllTypes ();
205203 $ this ->fullyLoaded = true ;
206204
@@ -243,6 +241,9 @@ private function collectAllTypes()
243241 }
244242
245243 /**
244+ * Returns all possible concrete types for given abstract type
245+ * (implementations for interfaces and members of union type for unions)
246+ *
246247 * @param AbstractType $abstractType
247248 * @return ObjectType[]
248249 */
@@ -253,7 +254,7 @@ public function getPossibleTypes(AbstractType $abstractType)
253254 }
254255
255256 /** @var InterfaceType $abstractType */
256- $ descriptor = $ this ->getDescriptor ();
257+ $ descriptor = $ this ->config -> descriptor ?: $ this -> describe ();
257258
258259 $ result = [];
259260 if (isset ($ descriptor ->possibleTypeMap [$ abstractType ->name ])) {
@@ -264,6 +265,13 @@ public function getPossibleTypes(AbstractType $abstractType)
264265 return $ result ;
265266 }
266267
268+ /**
269+ * Accepts name of type or type instance and returns type instance. If type with given name is not loaded yet -
270+ * will load it first.
271+ *
272+ * @param $typeOrName
273+ * @return Type
274+ */
267275 public function resolveType ($ typeOrName )
268276 {
269277 if ($ typeOrName instanceof Type) {
@@ -292,6 +300,9 @@ private function loadType($typeName)
292300 }
293301
294302 /**
303+ * Returns true if object type is concrete type of given abstract type
304+ * (implementation for interfaces and members of union type for unions)
305+ *
295306 * @param AbstractType $abstractType
296307 * @param ObjectType $possibleType
297308 * @return bool
@@ -311,6 +322,8 @@ public function isPossibleType(AbstractType $abstractType, ObjectType $possibleT
311322 }
312323
313324 /**
325+ * Returns a list of directives supported by this schema
326+ *
314327 * @return Directive[]
315328 */
316329 public function getDirectives ()
@@ -319,6 +332,8 @@ public function getDirectives()
319332 }
320333
321334 /**
335+ * Returns instance of directive by name
336+ *
322337 * @param $name
323338 * @return Directive
324339 */
0 commit comments