@@ -138,8 +138,7 @@ def add_reverse_edges(schema: GraphQLSchema):
138
138
continue
139
139
140
140
for field_name , field_type in _type .fields .items ():
141
- inner_field_type = get_named_type (field_type .type )
142
- if is_scalar_type (inner_field_type ) or is_enum_type (inner_field_type ):
141
+ if is_enum_or_scalar (get_named_type (field_type .type )):
143
142
continue
144
143
145
144
# Reverse edge
@@ -516,47 +515,76 @@ def add_object_type_filters(schema: GraphQLSchema):
516
515
return schema
517
516
518
517
519
- def add_create_edge_objects (schema : GraphQLSchema ):
518
+ def get_field_annotations (field : GraphQLField ):
519
+ annotation_fields = []
520
+ for arg , arg_type in field .args .items ():
521
+ if arg == 'filter' :
522
+ continue
523
+ if not is_enum_or_scalar (get_named_type (arg_type .type )):
524
+ raise Exception ("Input object fields are not supported." )
525
+ annotation_fields .append (f'{ arg } : { arg_type .type } ' )
526
+ return " " .join (annotation_fields )
527
+
528
+
529
+ def add_edge_objects (schema : GraphQLSchema ):
520
530
make = ''
521
531
for _type in schema .type_map .values ():
522
532
if not is_schema_defined_type (_type ) or is_interface_type (_type ):
523
533
continue
524
-
534
+ connected_types = schema . get_possible_types ( _type ) if is_interface_type ( _type ) else [ _type ]
525
535
for field_name , field in _type .fields .items ():
526
- if field_name [0 ] == '_' :
527
- continue
528
- field_type = field .type
529
- inner_field_type = get_named_type (field_type )
530
- if is_scalar_type (inner_field_type ) or is_enum_type (inner_field_type ):
536
+ inner_field_type = get_named_type (field .type )
537
+ if field_name .startswith ('_' ) or is_enum_or_scalar (inner_field_type ):
531
538
continue
539
+ for t in connected_types :
540
+ edge_from = f'{ capitalize (field_name )} EdgeFrom{ t .name } '
541
+ annotations = get_field_annotations (field )
542
+ make += f'type _{ edge_from } {{id:ID! source: { t .name } ! target: { inner_field_type } ! { annotations } }}\n '
543
+
544
+ schema = add_to_schema (schema , make )
545
+ return schema
532
546
533
- if is_interface_type (_type ):
534
- connected_types = schema .get_possible_types (_type )
535
- else :
536
- connected_types = [_type ]
537
547
548
+ def add_input_to_create_edge_objects (schema : GraphQLSchema ):
549
+ make = ''
550
+ for _type in schema .type_map .values ():
551
+ if not is_schema_defined_type (_type ) or is_interface_type (_type ):
552
+ continue
553
+ connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
554
+ for field_name , field in _type .fields .items ():
555
+ inner_field_type = get_named_type (field .type )
556
+ if field_name .startswith ('_' ) or is_enum_or_scalar (inner_field_type ):
557
+ continue
538
558
for t in connected_types :
539
559
edge_from = f'{ capitalize (field_name )} EdgeFrom{ t .name } '
540
- edge_create = f'create{ edge_from } '
541
560
edge_input = f'_InputToCreate{ edge_from } '
542
561
annotate_input = f'_InputToAnnotate{ edge_from } '
543
-
544
- annotation_fields = []
545
- for arg , arg_type in field .args .items ():
546
- if arg == 'filter' :
547
- continue
548
- # if not is_scalar_type(arg_type.type.name) and not is_enum_type(arg_type.type.name):
549
- # raise Exception("Fields cannot be object types")
550
- annotation_fields .append (f'{ arg } : { arg_type .type } ' )
551
- annotations = " " .join (annotation_fields )
562
+ annotations = get_field_annotations (field )
552
563
553
564
if len (annotations ) > 0 :
554
565
make += f'input { edge_input } {{sourceID: ID! targetID: ID! annotations: { annotate_input } }}\n '
555
566
make += f'input { annotate_input } {{{ annotations } }}\n '
556
- make += f'type _{ edge_from } {{id:ID! source: { t .name } ! target: { inner_field_type } ! { annotations } }}\n '
557
567
else :
558
568
make += f'input { edge_input } {{sourceID: ID! targetID: ID!}}\n '
559
- make += f'type _{ edge_from } {{id: ID! source: { t .name } ! target: { inner_field_type } !}}\n '
569
+
570
+ schema = add_to_schema (schema , make )
571
+ return schema
572
+
573
+
574
+ def add_mutation_create_edge_objects (schema : GraphQLSchema ):
575
+ make = ''
576
+ for _type in schema .type_map .values ():
577
+ if not is_schema_defined_type (_type ) or is_interface_type (_type ):
578
+ continue
579
+ connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
580
+ for field_name , field in _type .fields .items ():
581
+ inner_field_type = get_named_type (field .type )
582
+ if field_name .startswith ('_' ) or is_enum_or_scalar (inner_field_type ):
583
+ continue
584
+ for t in connected_types :
585
+ edge_from = f'{ capitalize (field_name )} EdgeFrom{ t .name } '
586
+ edge_create = f'create{ edge_from } '
587
+ edge_input = f'_InputToCreate{ edge_from } '
560
588
make += f'extend type Mutation{{{ edge_create } (data: { edge_input } ):_{ edge_from } }}\n '
561
589
562
590
schema = add_to_schema (schema , make )
0 commit comments