@@ -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,12 +515,93 @@ def add_object_type_filters(schema: GraphQLSchema):
516
515
return schema
517
516
518
517
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 ):
530
+ make = ''
531
+ for _type in schema .type_map .values ():
532
+ if not is_schema_defined_type (_type ) or is_interface_type (_type ):
533
+ continue
534
+ connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
535
+ for field_name , field in _type .fields .items ():
536
+ inner_field_type = get_named_type (field .type )
537
+ if field_name .startswith ('_' ) or is_enum_or_scalar (inner_field_type ):
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
546
+
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
558
+ for t in connected_types :
559
+ edge_from = f'{ capitalize (field_name )} EdgeFrom{ t .name } '
560
+ edge_input = f'_InputToCreate{ edge_from } '
561
+ annotate_input = f'_InputToAnnotate{ edge_from } '
562
+ annotations = get_field_annotations (field )
563
+
564
+ if len (annotations ) > 0 :
565
+ make += f'input { edge_input } {{sourceID: ID! targetID: ID! annotations: { annotate_input } }}\n '
566
+ make += f'input { annotate_input } {{{ annotations } }}\n '
567
+ else :
568
+ make += f'input { edge_input } {{sourceID: ID! targetID: ID!}}\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 } '
588
+ make += f'extend type Mutation{{{ edge_create } (data: { edge_input } ):_{ edge_from } }}\n '
589
+
590
+ schema = add_to_schema (schema , make )
591
+ return schema
592
+
593
+
519
594
def remove_field_arguments_for_types (schema : GraphQLSchema ):
595
+ keep_args = ['filter' ]
520
596
for _type in schema .type_map .values ():
521
597
if not is_schema_defined_type (_type ):
522
598
continue
523
599
for field_name , field in _type .fields .items ():
524
- field .args = {}
600
+ args = {}
601
+ for arg in field .args :
602
+ if arg in keep_args :
603
+ args [arg ] = field .args [arg ]
604
+ field .args = args
525
605
return schema
526
606
527
607
0 commit comments