17
17
GraphQLObjectType ,
18
18
GraphQLSchema ,
19
19
GraphQLWrappingType ,
20
+ InlineFragmentNode ,
20
21
ListTypeNode ,
21
22
ListValueNode ,
22
23
NamedTypeNode ,
@@ -407,6 +408,10 @@ class DSLField:
407
408
method.
408
409
"""
409
410
411
+ _type : Union [GraphQLObjectType , GraphQLInterfaceType ]
412
+ ast_field : FieldNode
413
+ field : GraphQLField
414
+
410
415
def __init__ (
411
416
self ,
412
417
name : str ,
@@ -423,11 +428,9 @@ def __init__(
423
428
:param graphql_type: the GraphQL type definition from the schema
424
429
:param graphql_field: the GraphQL field definition from the schema
425
430
"""
426
- self ._type : Union [GraphQLObjectType , GraphQLInterfaceType ] = graphql_type
427
- self .field : GraphQLField = graphql_field
428
- self .ast_field : FieldNode = FieldNode (
429
- name = NameNode (value = name ), arguments = FrozenList ()
430
- )
431
+ self ._type = graphql_type
432
+ self .field = graphql_field
433
+ self .ast_field = FieldNode (name = NameNode (value = name ), arguments = FrozenList ())
431
434
log .debug (f"Creating { self !r} " )
432
435
433
436
@staticmethod
@@ -585,7 +588,25 @@ def __str__(self) -> str:
585
588
return print_ast (self .ast_field )
586
589
587
590
def __repr__ (self ) -> str :
588
- return (
589
- f"<{ self .__class__ .__name__ } { self ._type .name } "
590
- f"::{ self .ast_field .name .value } >"
591
+ name = self ._type .name
592
+ try :
593
+ name += f"::{ self .ast_field .name .value } "
594
+ except AttributeError :
595
+ pass
596
+ return f"<{ self .__class__ .__name__ } { name } >"
597
+
598
+
599
+ class DSLFragment (DSLField ):
600
+ def __init__ (
601
+ self , type_condition : Optional [DSLType ] = None ,
602
+ ):
603
+ self .ast_field = InlineFragmentNode () # type: ignore
604
+ if type_condition :
605
+ self .on (type_condition )
606
+
607
+ def on (self , type_condition : DSLType ):
608
+ self ._type = type_condition ._type
609
+ self .ast_field .type_condition = NamedTypeNode ( # type: ignore
610
+ name = NameNode (value = self ._type .name )
591
611
)
612
+ return self
0 commit comments