1919use GraphQL \Schema ;
2020use GraphQL \Type \Definition \ObjectType ;
2121use GraphQL \Type \Definition \Type ;
22+ use GraphQL \Type \Resolution ;
2223use GraphQL \Validator \DocumentValidator ;
2324use GraphQL \Validator \Rules \QueryComplexity ;
2425use Yii ;
3637class GraphQL
3738{
3839 /**
39- * @var array query类型配置信息
40+ * @var array query map config
4041 */
4142 public $ queries = [];
4243 /**
43- * @var array mutation类型的配置信息
44+ * @var array mutation map config
4445 */
4546 public $ mutations = [];
4647 /**
47- * @var array type类型的配置信息
48+ * @var array type map config
4849 */
4950 public $ types = [];
5051
51- protected $ typesInstances = [];
52-
5352 public $ errorFormatter ;
5453
5554 private $ currentDocument ;
55+ /**
56+ * @var TypeResolution
57+ */
58+ private $ typeResolution ;
59+
60+ function __construct ()
61+ {
62+ }
63+
64+ /**
65+ * get TypeResolution
66+ * @return TypeResolution
67+ */
68+ public function getTypeResolution ()
69+ {
70+ if (!$ this ->typeResolution ) {
71+ $ this ->typeResolution = new TypeResolution ();
72+ }
73+ return $ this ->typeResolution ;
74+ }
5675
5776 /**
5877 * 接收schema数据,并入的配置信息
@@ -76,7 +95,7 @@ public function schema($schema = null)
7695 $ schemaTypes = ArrayHelper::getValue ($ schema , 'types ' , []);
7796 $ this ->queries += $ schemaQuery ;
7897 $ this ->mutations += $ schemaMutation ;
79- $ this ->types += $ schemaTypes ;
98+ $ this ->getTypeResolution ()-> setAlias ( $ schemaTypes) ;
8099 }
81100 }
82101
@@ -99,110 +118,35 @@ public function buildSchema($schema = null)
99118 $ types = [];
100119 if (sizeof ($ schemaTypes )) {
101120 foreach ($ schemaTypes as $ name => $ type ) {
102- $ types [] = $ this ->getType ( $ name );
121+ $ types [] = $ this ->getTypeResolution ()-> parseType ( $ name, true );
103122 }
104123 }
105124 //graqhql的validator要求query必须有
106- $ query = $ this ->objectType ($ schemaQuery , [
125+ $ query = $ this ->getTypeResolution ()-> objectType ($ schemaQuery , [
107126 'name ' => 'Query '
108127 ]);
109128
110129 $ mutation = null ;
111130 if (!empty ($ schemaMutation )) {
112- $ mutation = $ this ->objectType ($ schemaMutation , [
131+ $ mutation = $ this ->getTypeResolution ()-> objectType ($ schemaMutation , [
113132 'name ' => 'Mutation '
114133 ]);
115134 }
116135
136+ $ this ->getTypeResolution ()->initTypes ([$ query , $ mutation ], $ schema == null );
137+
117138 $ result = new Schema ([
118139 'query ' => $ query ,
119140 'mutation ' => $ mutation ,
120- 'types ' => $ types
141+ 'types ' => $ types ,
142+ 'typeResolution ' => $ this ->getTypeResolution (),
121143 ]);
122144 return $ result ;
123145 }
124146
125- /**
126- * 获取指定类型GraphQL的ObjectType实例
127- * @param $type
128- * @param array $opts
129- * @return ObjectType|null
130- */
131- public function objectType ($ type , $ opts = [])
132- {
133- // If it's already an ObjectType, just update properties and return it.
134- // If it's an array, assume it's an array of fields and build ObjectType
135- // from it. Otherwise, build it from a string or an instance.
136- $ objectType = null ;
137- if ($ type instanceof ObjectType) {
138- $ objectType = $ type ;
139- foreach ($ opts as $ key => $ value ) {
140- if (property_exists ($ objectType , $ key )) {
141- $ objectType ->{$ key } = $ value ;
142- }
143- if (isset ($ objectType ->config [$ key ])) {
144- $ objectType ->config [$ key ] = $ value ;
145- }
146- }
147- } elseif (is_array ($ type )) {
148- $ objectType = $ this ->buildObjectTypeFromFields ($ type , $ opts );
149- } else {
150- $ objectType = $ this ->buildObjectTypeFromClass ($ type , $ opts );
151- }
152-
153- return $ objectType ;
154- }
155-
156- /**
157- * build ObjectType from classname config
158- * @param Object|array $type 能够转换为ObjectType的类实例或者类配置
159- * @param array $opts
160- * @return object
161- * @throws InvalidConfigException
162- */
163- protected function buildObjectTypeFromClass ($ type , $ opts = [])
164- {
165- if (!is_object ($ type )) {
166- $ type = Yii::createObject ($ type );
167- }
168-
169- foreach ($ opts as $ key => $ value ) {
170- $ type ->{$ key } = $ value ;
171- }
172-
173- return $ type ->toType ();
174- }
175-
176- /**
177- * 通过graphql声明配置构建GraphQL ObjectType
178- * @param array $fields use standard graphql declare.
179- * @param array $opts
180- * @return ObjectType
181- * @throws InvalidConfigException
182- */
183- protected function buildObjectTypeFromFields ($ fields , $ opts = [])
184- {
185- $ typeFields = [];
186- foreach ($ fields as $ name => $ field ) {
187- if (is_string ($ field )) {
188- $ field = Yii::createObject ($ field );
189- $ name = is_numeric ($ name ) ? $ field ->name : $ name ;
190- $ field ['name ' ] = $ name ;
191- $ field = $ field ->toArray ();
192- } else {
193- $ name = is_numeric ($ name ) ? $ field ['name ' ] : $ name ;
194- $ field ['name ' ] = $ name ;
195- }
196- $ typeFields [$ name ] = $ field ;
197- }
198-
199- return new ObjectType (array_merge ([
200- 'fields ' => $ typeFields
201- ], $ opts ));
202- }
203147
204148 /**
205- * 查询入口,主要通过该方法返回数据
149+ * query access
206150 * @param $requestString
207151 * @param null $rootValue
208152 * @param null $contextValue
@@ -317,76 +261,18 @@ public function parseRequestQuery($requestString)
317261 }
318262
319263 /**
320- * 通过名称获取GraphQL的类型系统实例
321- * @param $name
264+ * Type manager access
265+ * @param string|Type $name
266+ * @param bool $byAlias if use alias
322267 * @return mixed
323268 */
324- public static function type ($ name )
269+ public static function type ($ name, $ byAlias = false )
325270 {
326271 /** @var GraphQLModuleTrait $module */
327272 $ module = Yii::$ app ->controller ? Yii::$ app ->controller ->module : Yii::$ app ->getModule ('graphql ' );
328273 $ gql = $ module ->getGraphQL ();
329274
330- return $ gql ->getType ($ name );
331- }
332-
333- /**
334- * get type by name,this method is use in Type definition class for TypeSystem
335- * @param $name
336- * @return ObjectType|null
337- * @throws TypeNotFound
338- */
339- public function getType ($ name )
340- {
341- $ class = $ name ;
342- if (is_object ($ class )) {
343- $ name = get_class ($ class );
344- }
345- if (isset ($ this ->types [$ name ])) {
346- $ class = $ this ->types [$ name ];
347-
348- if (is_object ($ class )) {
349- return $ class ;
350- }
351- }
352-
353- //class is string or not found;
354- if (is_string ($ class )) {
355- if (strpos ($ class , '\\' ) !== false && !class_exists ($ class )) {
356- throw new TypeNotFound ('Type ' . $ name . ' not found. ' );
357- }
358-
359- } elseif (!is_object ($ class )) {
360- throw new TypeNotFound ('Type ' . $ name . ' not found. ' );
361- }
362- $ type = $ this ->buildType ($ class );
363- $ this ->types [$ name ] = $ type ;
364- return $ type ;
365- }
366-
367- /**
368- * @param string $type type name
369- * @param array $opts return Type's attribute set
370- * @return ObjectType|Type|GraphQLField
371- * @throws InvalidConfigException
372- * @throws NotSupportedException
373- */
374- protected function buildType ($ type )
375- {
376- if (!is_object ($ type )) {
377- $ type = Yii::createObject ($ type );
378- }
379- if ($ type instanceof Type) {
380- return $ type ;
381- } elseif ($ type instanceof GraphQLType) {
382- //transfer ObjectType
383- return $ type ->toType ();
384- } elseif ($ type instanceof GraphQLField) {
385- //field is not need transfer to ObjectType,it just need config array
386- return $ type ;
387- }
388-
389- throw new NotSupportedException ("Type: {$ type } is not support translate to Graph Type " );
275+ return $ gql ->getTypeResolution ()->parseType ($ name , $ byAlias );
390276 }
391277
392278 /**
0 commit comments