31
31
GraphQLEnumType ,
32
32
GraphQLEnumValue ,
33
33
GraphQLField ,
34
+ GraphQLFieldMap ,
34
35
GraphQLIncludeDirective ,
35
36
GraphQLInputType ,
36
37
GraphQLInputField ,
38
+ GraphQLInputFieldMap ,
37
39
GraphQLInputObjectType ,
40
+ GraphQLInterfaceList ,
38
41
GraphQLInterfaceType ,
39
42
GraphQLList ,
40
43
GraphQLNamedType ,
46
49
GraphQLSchema ,
47
50
GraphQLSkipDirective ,
48
51
GraphQLType ,
52
+ GraphQLTypeList ,
49
53
GraphQLUnionType ,
54
+ Thunk ,
50
55
introspection_types ,
51
56
specified_scalar_types ,
52
57
)
@@ -216,21 +221,17 @@ def _build_wrapped_type(self, type_node: TypeNode) -> GraphQLType:
216
221
)
217
222
return self .build_type (cast (NamedTypeNode , type_node ))
218
223
219
- def build_directive (
220
- self , directive_node : DirectiveDefinitionNode
221
- ) -> GraphQLDirective :
224
+ def build_directive (self , directive : DirectiveDefinitionNode ) -> GraphQLDirective :
225
+ locations = [ DirectiveLocation [ node . value ] for node in directive . locations ]
226
+
222
227
return GraphQLDirective (
223
- name = directive_node .name .value ,
224
- description = directive_node .description .value
225
- if directive_node .description
226
- else None ,
227
- locations = [
228
- DirectiveLocation [node .value ] for node in directive_node .locations
229
- ],
230
- args = self ._make_args (directive_node .arguments )
231
- if directive_node .arguments
232
- else None ,
233
- ast_node = directive_node ,
228
+ name = directive .name .value ,
229
+ description = directive .description .value if directive .description else None ,
230
+ locations = locations ,
231
+ args = {
232
+ arg .name .value : self .build_arg (arg ) for arg in directive .arguments or []
233
+ },
234
+ ast_node = directive ,
234
235
)
235
236
236
237
def build_field (self , field : FieldDefinitionNode ) -> GraphQLField :
@@ -242,11 +243,24 @@ def build_field(self, field: FieldDefinitionNode) -> GraphQLField:
242
243
return GraphQLField (
243
244
type_ = type_ ,
244
245
description = field .description .value if field .description else None ,
245
- args = self ._make_args ( field . arguments ) if field .arguments else None ,
246
+ args = { arg . name . value : self .build_arg ( arg ) for arg in field .arguments or []} ,
246
247
deprecation_reason = get_deprecation_reason (field ),
247
248
ast_node = field ,
248
249
)
249
250
251
+ def build_arg (self , value : InputValueDefinitionNode ) -> GraphQLArgument :
252
+ # Note: While this could make assertions to get the correctly typed value, that
253
+ # would throw immediately while type system validation with `validate_schema()`
254
+ # will produce more actionable results.
255
+ type_ = self ._build_wrapped_type (value .type )
256
+ type_ = cast (GraphQLInputType , type_ )
257
+ return GraphQLArgument (
258
+ type_ = type_ ,
259
+ description = value .description .value if value .description else None ,
260
+ default_value = value_from_ast (value .default_value , type_ ),
261
+ ast_node = value ,
262
+ )
263
+
250
264
def build_input_field (self , value : InputValueDefinitionNode ) -> GraphQLInputField :
251
265
# Note: While this could make assertions to get the correctly typed value, that
252
266
# would throw immediately while type system validation with `validate_schema()`
@@ -268,137 +282,142 @@ def build_enum_value(value: EnumValueDefinitionNode) -> GraphQLEnumValue:
268
282
ast_node = value ,
269
283
)
270
284
271
- def _make_schema_def (self , type_def : TypeDefinitionNode ) -> GraphQLNamedType :
285
+ def _make_schema_def (self , ast_node : TypeDefinitionNode ) -> GraphQLNamedType :
272
286
method = {
273
287
"object_type_definition" : self ._make_type_def ,
274
288
"interface_type_definition" : self ._make_interface_def ,
275
289
"enum_type_definition" : self ._make_enum_def ,
276
290
"union_type_definition" : self ._make_union_def ,
277
291
"scalar_type_definition" : self ._make_scalar_def ,
278
292
"input_object_type_definition" : self ._make_input_object_def ,
279
- }.get (type_def .kind )
293
+ }.get (ast_node .kind )
280
294
if not method :
281
- raise TypeError (f"Type kind '{ type_def .kind } ' not supported." )
282
- return method (type_def ) # type: ignore
283
-
284
- def _make_type_def (self , type_def : ObjectTypeDefinitionNode ) -> GraphQLObjectType :
285
- interfaces = type_def .interfaces
286
- return GraphQLObjectType (
287
- name = type_def .name .value ,
288
- description = type_def .description .value if type_def .description else None ,
289
- fields = lambda : self ._make_field_def_map (type_def ),
290
- # Note: While this could make early assertions to get the correctly typed
291
- # values, that would throw immediately while type system validation with
292
- # `validate_schema()` will produce more actionable results.
293
- interfaces = (
294
- lambda : [self .build_type (ref ) for ref in interfaces ] # type: ignore
295
- )
296
- if interfaces
297
- else [],
298
- ast_node = type_def ,
295
+ raise TypeError (f"Type kind '{ ast_node .kind } ' not supported." )
296
+ return method (ast_node ) # type: ignore
297
+
298
+ def _make_type_def (self , ast_node : ObjectTypeDefinitionNode ) -> GraphQLObjectType :
299
+ interface_nodes = ast_node .interfaces
300
+ field_nodes = ast_node .fields
301
+
302
+ # Note: While this could make early assertions to get the correctly typed
303
+ # values, that would throw immediately while type system validation with
304
+ # `validate_schema()` will produce more actionable results.
305
+ interfaces = cast (
306
+ Thunk [GraphQLInterfaceList ],
307
+ (
308
+ (lambda : [self .build_type (ref ) for ref in interface_nodes ])
309
+ if interface_nodes
310
+ else []
311
+ ),
299
312
)
300
313
301
- def _make_field_def_map (
302
- self , type_def : Union [ObjectTypeDefinitionNode , InterfaceTypeDefinitionNode ]
303
- ) -> Dict [str , GraphQLField ]:
304
- fields = type_def .fields
305
- return (
306
- {field .name .value : self .build_field (field ) for field in fields }
307
- if fields
308
- else {}
314
+ fields = cast (
315
+ Thunk [GraphQLFieldMap ],
316
+ (
317
+ (
318
+ lambda : {
319
+ field .name .value : self .build_field (field )
320
+ for field in field_nodes
321
+ }
322
+ )
323
+ if field_nodes
324
+ else {}
325
+ ),
309
326
)
310
327
311
- def _make_arg (self , value_node : InputValueDefinitionNode ) -> GraphQLArgument :
312
- # Note: While this could make assertions to get the correctly typed value, that
313
- # would throw immediately while type system validation with `validate_schema()`
314
- # will produce more actionable results.
315
- type_ = self ._build_wrapped_type (value_node .type )
316
- type_ = cast (GraphQLInputType , type_ )
317
- return GraphQLArgument (
318
- type_ = type_ ,
319
- description = value_node .description .value
320
- if value_node .description
321
- else None ,
322
- default_value = value_from_ast (value_node .default_value , type_ ),
323
- ast_node = value_node ,
328
+ return GraphQLObjectType (
329
+ name = ast_node .name .value ,
330
+ description = ast_node .description .value if ast_node .description else None ,
331
+ fields = fields ,
332
+ interfaces = interfaces ,
333
+ ast_node = ast_node ,
324
334
)
325
335
326
- def _make_args (
327
- self , values : List [InputValueDefinitionNode ]
328
- ) -> Dict [str , GraphQLArgument ]:
329
- return {value .name .value : self ._make_arg (value ) for value in values }
330
-
331
- def _make_input_fields (
332
- self , values : List [InputValueDefinitionNode ]
333
- ) -> Dict [str , GraphQLInputField ]:
334
- return {value .name .value : self .build_input_field (value ) for value in values }
335
-
336
336
def _make_interface_def (
337
- self , type_def : InterfaceTypeDefinitionNode
337
+ self , ast_node : InterfaceTypeDefinitionNode
338
338
) -> GraphQLInterfaceType :
339
+ field_nodes = ast_node .fields
340
+
341
+ fields = cast (
342
+ Thunk [GraphQLFieldMap ],
343
+ (
344
+ lambda : {
345
+ field .name .value : self .build_field (field ) for field in field_nodes
346
+ }
347
+ )
348
+ if field_nodes
349
+ else {},
350
+ )
351
+
339
352
return GraphQLInterfaceType (
340
- name = type_def .name .value ,
341
- description = type_def .description .value if type_def .description else None ,
342
- fields = lambda : self . _make_field_def_map ( type_def ) ,
343
- ast_node = type_def ,
353
+ name = ast_node .name .value ,
354
+ description = ast_node .description .value if ast_node .description else None ,
355
+ fields = fields ,
356
+ ast_node = ast_node ,
344
357
)
345
358
346
- def _make_enum_def (self , type_def : EnumTypeDefinitionNode ) -> GraphQLEnumType :
359
+ def _make_enum_def (self , ast_node : EnumTypeDefinitionNode ) -> GraphQLEnumType :
360
+ value_nodes = ast_node .values or []
361
+
347
362
return GraphQLEnumType (
348
- name = type_def .name .value ,
349
- description = type_def .description .value if type_def .description else None ,
350
- values = self ._make_value_def_map (type_def ),
351
- ast_node = type_def ,
363
+ name = ast_node .name .value ,
364
+ description = ast_node .description .value if ast_node .description else None ,
365
+ values = {
366
+ value .name .value : self .build_enum_value (value ) for value in value_nodes
367
+ },
368
+ ast_node = ast_node ,
352
369
)
353
370
354
- def _make_value_def_map (
355
- self , type_def : EnumTypeDefinitionNode
356
- ) -> Dict [str , GraphQLEnumValue ]:
357
- return (
358
- {
359
- value .name .value : self .build_enum_value (value )
360
- for value in type_def .values
361
- }
362
- if type_def .values
363
- else {}
371
+ def _make_union_def (self , type_def : UnionTypeDefinitionNode ) -> GraphQLUnionType :
372
+ type_nodes = type_def .types
373
+
374
+ # Note: While this could make assertions to get the correctly typed values
375
+ # below, that would throw immediately while type system validation with
376
+ # `validate_schema()` will get more actionable results.
377
+ types = cast (
378
+ Thunk [GraphQLTypeList ],
379
+ (lambda : [self .build_type (ref ) for ref in type_nodes ])
380
+ if type_nodes
381
+ else [],
364
382
)
365
383
366
- def _make_union_def (self , type_def : UnionTypeDefinitionNode ) -> GraphQLUnionType :
367
- types = type_def .types
368
384
return GraphQLUnionType (
369
385
name = type_def .name .value ,
370
386
description = type_def .description .value if type_def .description else None ,
371
- # Note: While this could make assertions to get the correctly typed values
372
- # below, that would throw immediately while type system validation with
373
- # `validate_schema()` will get more actionable results.
374
- types = (lambda : [self .build_type (ref ) for ref in types ]) # type: ignore
375
- if types
376
- else [],
387
+ types = types ,
377
388
ast_node = type_def ,
378
389
)
379
390
380
391
@staticmethod
381
- def _make_scalar_def (type_def : ScalarTypeDefinitionNode ) -> GraphQLScalarType :
392
+ def _make_scalar_def (ast_node : ScalarTypeDefinitionNode ) -> GraphQLScalarType :
382
393
return GraphQLScalarType (
383
- name = type_def .name .value ,
384
- description = type_def .description .value if type_def .description else None ,
385
- ast_node = type_def ,
394
+ name = ast_node .name .value ,
395
+ description = ast_node .description .value if ast_node .description else None ,
396
+ ast_node = ast_node ,
386
397
serialize = lambda value : value ,
387
398
)
388
399
389
400
def _make_input_object_def (
390
401
self , type_def : InputObjectTypeDefinitionNode
391
402
) -> GraphQLInputObjectType :
403
+ field_nodes = type_def .fields
404
+
405
+ fields = cast (
406
+ Thunk [GraphQLInputFieldMap ],
407
+ (
408
+ lambda : {
409
+ field .name .value : self .build_input_field (field )
410
+ for field in field_nodes
411
+ }
412
+ )
413
+ if field_nodes
414
+ else {},
415
+ )
416
+
392
417
return GraphQLInputObjectType (
393
418
name = type_def .name .value ,
394
419
description = type_def .description .value if type_def .description else None ,
395
- fields = (
396
- lambda : self ._make_input_fields (
397
- cast (List [InputValueDefinitionNode ], type_def .fields )
398
- )
399
- )
400
- if type_def .fields
401
- else cast (Dict [str , GraphQLInputField ], {}),
420
+ fields = fields ,
402
421
ast_node = type_def ,
403
422
)
404
423
0 commit comments