@@ -112,7 +112,7 @@ def add_id_to_types(schema: GraphQLSchema):
112
112
"""
113
113
make = ''
114
114
for _type in schema .type_map .values ():
115
- if not is_db_schema_defined_type (_type ):
115
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
116
116
continue
117
117
if is_interface_type (_type ):
118
118
make += f'extend interface { _type .name } {{ id: ID! }} '
@@ -129,7 +129,7 @@ def add_creation_date_to_types(schema: GraphQLSchema):
129
129
"""
130
130
make = ''
131
131
for _type in schema .type_map .values ():
132
- if not is_schema_defined_type (_type ):
132
+ if not is_schema_defined_type (_type ) or is_union_type ( _type ) :
133
133
continue
134
134
135
135
if is_interface_type (_type ):
@@ -147,7 +147,7 @@ def add_last_update_date_to_types(schema: GraphQLSchema):
147
147
"""
148
148
make = ''
149
149
for _type in schema .type_map .values ():
150
- if not is_schema_defined_type (_type ):
150
+ if not is_schema_defined_type (_type ) or is_union_type ( _type ) :
151
151
continue
152
152
if is_interface_type (_type ):
153
153
make += f'extend interface { _type .name } {{ _lastUpdateDate: DateTime }} '
@@ -192,7 +192,7 @@ def add_reverse_edges(schema: GraphQLSchema):
192
192
"""
193
193
make = ''
194
194
for _type in schema .type_map .values ():
195
- if not is_db_schema_defined_type (_type ):
195
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
196
196
continue
197
197
198
198
for field_name , field_type in _type .fields .items ():
@@ -226,6 +226,9 @@ def add_reverse_edges(schema: GraphQLSchema):
226
226
make += 'extend interface {0} {{ {1}: {2} {3} }}\n ' .format (edge_from , edge_name , edge_to , directive_to_add )
227
227
for implementing_type in schema .get_possible_types (edge_from ):
228
228
make += 'extend type {0} {{ {1}: {2} }}\n ' .format (implementing_type , edge_name , edge_to )
229
+ elif is_union_type (edge_from ):
230
+ for sub_type in edge_from .types :
231
+ make += 'extend type {0} {{ {1}: {2} }}\n ' .format (sub_type , edge_name , edge_to )
229
232
else :
230
233
make += 'extend type {0} {{ {1}: {2} {3} }}\n ' .format (edge_from , edge_name , edge_to , directive_to_add )
231
234
schema = add_to_schema (schema , make )
@@ -242,15 +245,15 @@ def add_input_to_create(schema: GraphQLSchema):
242
245
# add create types (placeholders)
243
246
make = ''
244
247
for _type in schema .type_map .values ():
245
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
248
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
246
249
continue
247
250
make += f'input _InputToCreate{ _type .name } '
248
251
schema = add_to_schema (schema , make )
249
252
250
253
# add fields to create types
251
254
make = ''
252
255
for _type in schema .type_map .values ():
253
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
256
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
254
257
continue
255
258
make += f'\n extend input _InputToCreate{ _type .name } {{\n '
256
259
num_fields = 0
@@ -348,7 +351,6 @@ def extend_connect(schema: GraphQLSchema, _type: GraphQLType, field_type: GraphQ
348
351
:param field_name:
349
352
:return:
350
353
"""
351
- create_name = f'_InputToCreate{ field_type .name } '
352
354
connect_name = f'_InputToConnect{ capitalize (field_name )} Of{ _type .name } '
353
355
make = f'input { connect_name } {{ '
354
356
make += 'connect: ID '
@@ -360,7 +362,14 @@ def extend_connect(schema: GraphQLSchema, _type: GraphQLType, field_type: GraphQ
360
362
create_field = f'create{ implementing_type .name } '
361
363
create_implementing_type = f'_InputToCreate{ implementing_type .name } '
362
364
make += f'{ create_field } : { create_implementing_type } '
365
+ elif is_union_type (field_type ):
366
+ # add fields for types in the union
367
+ for _sub_type in field_type .types :
368
+ create_field = f'create{ _sub_type .name } '
369
+ create_sub_type = f'_InputToCreate{ _sub_type .name } '
370
+ make += f'{ create_field } : { create_sub_type } '
363
371
else :
372
+ create_name = f'_InputToCreate{ field_type .name } '
364
373
make += f'create: { create_name } '
365
374
366
375
# Get annotations
@@ -389,7 +398,7 @@ def add_input_update(schema: GraphQLSchema):
389
398
# Create update inputs
390
399
make = ''
391
400
for _type in schema .type_map .values ():
392
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
401
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
393
402
continue
394
403
update_name = f'_InputToUpdate{ _type .name } '
395
404
make += f'input { update_name } '
@@ -398,24 +407,21 @@ def add_input_update(schema: GraphQLSchema):
398
407
# Add fields to update type
399
408
make = ''
400
409
for _type in schema .type_map .values ():
401
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
410
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
402
411
continue
403
412
num_fields = 0
404
413
update_name = f'_InputToUpdate{ _type .name } '
405
414
for field_name , field in _type .fields .items ():
406
415
if field_name == 'id' or field_name [0 ] == '_' :
407
416
continue
408
- num_fields += 1
417
+
409
418
f_type = get_nullable_type (field .type )
410
419
inner_field_type = get_named_type (f_type )
411
420
412
421
if is_enum_or_scalar (inner_field_type ):
422
+ num_fields += 1
413
423
make += f'extend input { update_name } {{ { field_name } : { f_type } }} \n '
414
- else :
415
- # add create or connect field
416
- connect_name = f'_InputToConnect{ capitalize (field_name )} Of{ _type .name } '
417
- connect = copy_wrapper_structure (schema .get_type (connect_name ), f_type )
418
- make += f'extend input { update_name } {{ { field_name } : { connect } }} \n '
424
+
419
425
if num_fields == 0 :
420
426
make += f'extend input { update_name } {{ _dummy: String }} \n '
421
427
schema = add_to_schema (schema , make )
@@ -464,7 +470,7 @@ def add_list_of_types(schema: GraphQLSchema):
464
470
"""
465
471
make = ''
466
472
for _type in schema .type_map .values ():
467
- if not is_db_schema_defined_type (_type ):
473
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
468
474
continue
469
475
470
476
make += f'type _ListOf{ _type .name } s {{ ' \
@@ -484,7 +490,7 @@ def add_list_queries(schema: GraphQLSchema):
484
490
"""
485
491
make = ''
486
492
for _type in schema .type_map .values ():
487
- if not is_db_schema_defined_type (_type ):
493
+ if not is_db_schema_defined_type (_type ) or is_list_type ( _type ) or is_union_type ( _type ) :
488
494
continue
489
495
make += f'extend type Query {{ ' \
490
496
f' listOf{ _type .name } s(first:Int=10, after:ID="", filter:_FilterFor{ _type .name } ): _ListOf{ _type .name } s ' \
@@ -594,7 +600,7 @@ def add_type_filters(schema: GraphQLSchema):
594
600
"""
595
601
make = ''
596
602
for _type in schema .type_map .values ():
597
- if not is_db_schema_defined_type (_type ):
603
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
598
604
continue
599
605
600
606
make += f'input _FilterFor{ _type .name } {{ ' \
@@ -630,7 +636,7 @@ def add_object_type_filters(schema: GraphQLSchema):
630
636
:return:
631
637
"""
632
638
for _type in schema .type_map .values ():
633
- if not is_db_schema_defined_type (_type ):
639
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
634
640
continue
635
641
636
642
for field_name , field in _type .fields .items ():
@@ -674,7 +680,7 @@ def get_field_annotations(field: GraphQLField):
674
680
def add_edge_objects (schema : GraphQLSchema ):
675
681
make = ''
676
682
for _type in schema .type_map .values ():
677
- if not is_db_schema_defined_type (_type ):
683
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
678
684
continue
679
685
for field_name , field in _type .fields .items ():
680
686
inner_field_type = get_named_type (field .type )
@@ -704,7 +710,7 @@ def add_edge_objects(schema: GraphQLSchema):
704
710
def add_fields_for_edge_types (schema : GraphQLSchema , reverse ):
705
711
make = ''
706
712
for _type in schema .type_map .values ():
707
- if not is_db_schema_defined_type (_type ):
713
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
708
714
continue
709
715
for field_name , field in _type .fields .items ():
710
716
inner_field_type = get_named_type (field .type )
@@ -720,7 +726,7 @@ def add_fields_for_edge_types(schema: GraphQLSchema, reverse):
720
726
implementing_types = schema .get_possible_types (_type )
721
727
for imp_type in implementing_types :
722
728
make += f'extend type { imp_type .name } {{ _outgoing{ capitalize (field_name )} EdgesFrom{ _type .name } : { full_type } }}\n '
723
-
729
+
724
730
make += f'extend { type_type_str } { _type .name } {{ _outgoing{ capitalize (field_name )} EdgesFrom{ _type .name } : { full_type } }}\n '
725
731
726
732
if reverse :
@@ -741,9 +747,13 @@ def add_fields_for_edge_types(schema: GraphQLSchema, reverse):
741
747
inner_connected_types = schema .get_possible_types (inner_field_type ) if is_interface_type (inner_field_type ) else [inner_field_type ]
742
748
for inner_t in inner_connected_types :
743
749
type_type_str = 'type'
744
- if is_interface_type (inner_t ):
745
- type_type_str = 'interface'
746
- make += f'extend { type_type_str } { inner_t .name } {{ _incoming{ edge_from } : { edge_to } }}\n '
750
+ if is_union_type (inner_t ):
751
+ for sub_type in inner_t .types :
752
+ make += f'extend type { sub_type .name } {{ _incoming{ edge_from } : { edge_to } }}\n '
753
+ else :
754
+ if is_interface_type (inner_t ):
755
+ type_type_str = 'interface'
756
+ make += f'extend { type_type_str } { inner_t .name } {{ _incoming{ edge_from } : { edge_to } }}\n '
747
757
748
758
schema = add_to_schema (schema , make )
749
759
return schema
@@ -793,7 +803,7 @@ def add_filters_for_edge_types(schema: GraphQLSchema):
793
803
def add_input_to_create_edge_objects (schema : GraphQLSchema ):
794
804
make = ''
795
805
for _type in schema .type_map .values ():
796
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
806
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
797
807
continue
798
808
connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
799
809
for field_name , field in _type .fields .items ():
@@ -823,7 +833,7 @@ def add_input_to_create_edge_objects(schema: GraphQLSchema):
823
833
def add_input_to_update_edge_objects (schema : GraphQLSchema ):
824
834
make = ''
825
835
for _type in schema .type_map .values ():
826
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
836
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
827
837
continue
828
838
connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
829
839
for field_name , field in _type .fields .items ():
@@ -845,7 +855,7 @@ def add_input_to_update_edge_objects(schema: GraphQLSchema):
845
855
def add_mutation_create_edge_objects (schema : GraphQLSchema ):
846
856
make = ''
847
857
for _type in schema .type_map .values ():
848
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
858
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
849
859
continue
850
860
connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
851
861
for field_name , field in _type .fields .items ():
@@ -866,7 +876,7 @@ def add_mutation_update_edge_objects(schema: GraphQLSchema):
866
876
make = ''
867
877
868
878
for _type in schema .type_map .values ():
869
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
879
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
870
880
continue
871
881
connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
872
882
for field_name , field in _type .fields .items ():
@@ -889,7 +899,7 @@ def add_mutation_delete_edge_objects(schema: GraphQLSchema):
889
899
make = ''
890
900
891
901
for _type in schema .type_map .values ():
892
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
902
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
893
903
continue
894
904
connected_types = schema .get_possible_types (_type ) if is_interface_type (_type ) else [_type ]
895
905
for field_name , field in _type .fields .items ():
@@ -909,7 +919,7 @@ def add_mutation_delete_edge_objects(schema: GraphQLSchema):
909
919
def remove_field_arguments_for_types (schema : GraphQLSchema ):
910
920
keep_args = ['filter' ]
911
921
for _type in schema .type_map .values ():
912
- if not is_db_schema_defined_type (_type ):
922
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
913
923
continue
914
924
for field_name , field in _type .fields .items ():
915
925
args = {}
@@ -949,7 +959,7 @@ def add_create_mutations(schema: GraphQLSchema):
949
959
"""
950
960
make = ''
951
961
for _type in schema .type_map .values ():
952
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
962
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
953
963
continue
954
964
create = f'create{ _type .name } '
955
965
input_type = f'_InputToCreate{ _type .name } '
@@ -967,7 +977,7 @@ def add_update_mutations(schema: GraphQLSchema):
967
977
"""
968
978
make = ''
969
979
for _type in schema .type_map .values ():
970
- if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
980
+ if not is_db_schema_defined_type (_type ) or is_interface_type (_type ) or is_union_type ( _type ) :
971
981
continue
972
982
update = f'update{ capitalize (_type .name )} '
973
983
input_type = f'_InputToUpdate{ _type .name } '
@@ -985,7 +995,7 @@ def add_delete_mutations(schema: GraphQLSchema):
985
995
"""
986
996
make = ''
987
997
for _type in schema .type_map .values ():
988
- if not is_db_schema_defined_type (_type ):
998
+ if not is_db_schema_defined_type (_type ) or is_union_type ( _type ) :
989
999
continue
990
1000
delete = f'delete{ _type .name } '
991
1001
make += f'extend type Mutation {{ { delete } (id: ID!): { _type .name } }} '
@@ -1251,6 +1261,8 @@ def print_schema_with_directives(schema):
1251
1261
output += 'scalar ' + _type .name
1252
1262
elif is_input_type (_type ):
1253
1263
output += 'input ' + _type .name
1264
+ elif is_union_type (_type ):
1265
+ output += 'union ' + _type .name
1254
1266
else :
1255
1267
output += 'type ' + _type .name
1256
1268
if hasattr (_type , 'interfaces' ) and _type .interfaces :
@@ -1264,6 +1276,11 @@ def print_schema_with_directives(schema):
1264
1276
output += ' ' + value + '\n '
1265
1277
output += '}'
1266
1278
1279
+ elif is_union_type (_type ):
1280
+ # For enums we can get the invloved types directly and add them
1281
+ output += ' = '
1282
+ output += ' | ' .join ([inner_type .name for inner_type in _type .types ])
1283
+
1267
1284
elif not is_enum_or_scalar (_type ):
1268
1285
# This should be a type, or an interface
1269
1286
# Get directives on type
0 commit comments