@@ -247,20 +247,21 @@ def add_input_to_create(schema: GraphQLSchema):
247
247
for _type in schema .type_map .values ():
248
248
if not is_db_schema_defined_type (_type ) or is_interface_type (_type ):
249
249
continue
250
- make += f'\n extend input _InputToCreate{ _type .name } {{'
250
+ make += f'\n extend input _InputToCreate{ _type .name } {{\n '
251
251
for field_name , field in _type .fields .items ():
252
252
if field_name == 'id' or field_name [0 ] == '_' :
253
253
continue
254
+
254
255
inner_field_type = get_named_type (field .type )
255
256
256
257
if is_enum_or_scalar (inner_field_type ):
257
- make += f' { field_name } : { field .type } '
258
+ make += f' { field_name } : { field .type } \n '
258
259
else :
259
260
schema = extend_connect (schema , _type , inner_field_type , field_name )
260
261
connect_name = f'_InputToConnect{ capitalize (field_name )} Of{ _type .name } '
261
262
connect = copy_wrapper_structure (schema .type_map [connect_name ], field .type )
262
- make += f' { field_name } : { connect } '
263
- make += '}'
263
+ make += f' { field_name } : { connect } \n '
264
+ make += '}\n '
264
265
schema = add_to_schema (schema , make )
265
266
return schema
266
267
@@ -387,12 +388,12 @@ def add_input_update(schema: GraphQLSchema):
387
388
inner_field_type = get_named_type (f_type )
388
389
389
390
if is_enum_or_scalar (inner_field_type ):
390
- make += f'extend input { update_name } {{ { field_name } : { f_type } }} '
391
+ make += f'extend input { update_name } {{ { field_name } : { f_type } }} \n '
391
392
else :
392
393
# add create or connect field
393
394
connect_name = f'_InputToConnect{ capitalize (field_name )} Of{ _type .name } '
394
395
connect = copy_wrapper_structure (schema .get_type (connect_name ), f_type )
395
- make += f'extend input { update_name } {{ { field_name } : { connect } }} '
396
+ make += f'extend input { update_name } {{ { field_name } : { connect } }} \n '
396
397
schema = add_to_schema (schema , make )
397
398
return schema
398
399
@@ -814,8 +815,8 @@ def directive_from_interface(directive, interface_name):
814
815
# The only two cases who needs special attention is @requiredForTarget and @uniqueForTarget
815
816
if directive_string == 'requiredForTarget' :
816
817
directive_string = '_requiredForTarget_AccordingToInterface(interface: "' + interface_name + '")'
817
- elif directive_string == 'uniqueForTarget' :
818
- directive_string = '_uniqueForTarget_AccordingToInterface(interface: "' + interface_name + '")'
818
+ # elif directive_string == 'uniqueForTarget':
819
+ # directive_string = '_uniqueForTarget_AccordingToInterface(interface: "' + interface_name + '")'
819
820
else :
820
821
directive_string += get_directive_arguments (directive )
821
822
@@ -859,9 +860,12 @@ def get_directive_arguments(directive):
859
860
return output
860
861
861
862
862
- def get_field_directives (field , field_name , _type ):
863
+ def get_field_directives (field , field_name , _type , schema ):
863
864
"""
864
865
Get the directives of given field, and return them as string
866
+ :param field:
867
+ :param field_name:
868
+ :param _type:
865
869
:param schema:
866
870
:return string:
867
871
"""
@@ -871,6 +875,20 @@ def get_field_directives(field, field_name, _type):
871
875
# Used to make sure we don't add the same directive multiple times to the same field
872
876
directives_set = set ()
873
877
878
+ if is_input_type (_type ):
879
+ # Get the target type instead (unless it is a filter or delete input, then we dont care)
880
+ # We also ignore @required directives for inputs
881
+ if _type .name [:14 ] == '_InputToUpdate' :
882
+ directives_set .add ('required' )
883
+ _type = schema .get_type (_type .name [14 :])
884
+
885
+ elif _type .name [:14 ] == '_InputToCreate' :
886
+ _type = schema .get_type (_type .name [14 :])
887
+ directives_set .add ('required' )
888
+
889
+ else :
890
+ return ''
891
+
874
892
# Get all directives directly on field
875
893
for directive in field .ast_node .directives :
876
894
if not directive .name .value in directives_set :
@@ -892,19 +910,57 @@ def get_field_directives(field, field_name, _type):
892
910
return output
893
911
894
912
895
- def printSchemaWithDirectives (schema ):
913
+ def get_type_directives (_type , schema ):
914
+ """
915
+ Get the directives of given type, or target type if create- or update-input
916
+ :param type:
917
+ :return string:
918
+ """
919
+
920
+ output = ''
921
+
922
+ if is_input_type (_type ):
923
+ # Get the target type instead (unless it is a filter or delete input, then we dont care)
924
+ if _type .name [:14 ] == '_InputToUpdate' :
925
+ _type = schema .get_type (_type .name [14 :])
926
+
927
+ elif _type .name [:14 ] == '_InputToCreate' :
928
+ _type = schema .get_type (_type .name [14 :])
929
+ else :
930
+ return ''
931
+
932
+ if hasattr (_type , 'ast_node' ) and _type .ast_node is not None :
933
+ # Get directives on type
934
+ for directive in _type .ast_node .directives :
935
+ output += ' @' + directive .name .value
936
+ output += get_directive_arguments (directive )
937
+
938
+ return output
939
+
940
+
941
+ def print_schema_with_directives (schema ):
896
942
"""
897
943
Ouputs the given schema as string, in the format we want it.
898
944
Types and fields will all contain directives
899
945
:param schema:
900
946
:return string:
901
947
"""
902
948
949
+
950
+ manual_directives = {'key' :'directive @key(fields: [String!]!) on OBJECT | INPUT_OBJECT' ,\
951
+ 'distinct' :'directive @distinct on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION' ,\
952
+ 'noloops' :'directive @noloops on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION' ,\
953
+ 'requiredForTarget' :'directive @requiredForTarget on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION' ,\
954
+ 'uniqueForTarget' :'directive @uniqueForTarget on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION' ,\
955
+ '_requiredForTarget_AccordingToInterface' :'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION' ,\
956
+ '_uniqueForTarget_AccordingToInterface' :'directive @_uniqueForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION' \
957
+ }
958
+
903
959
output = ''
904
960
905
- # Start by adding directives
961
+ # Add directives
906
962
for _dir in schema .directives :
907
- if _dir .ast_node is not None :
963
+ if _dir .ast_node is not None and _dir . name not in manual_directives . keys () :
908
964
# If the directive does not have a proper ast_node
909
965
# Then it is an non-user defined directive, and can hence, be skipped
910
966
output += 'directive @' + _dir .name
@@ -920,11 +976,10 @@ def printSchemaWithDirectives(schema):
920
976
output += _location ._name_ + ' | '
921
977
922
978
output = output [:- 3 ] + '\n \n '
923
- print (_dir .name )
924
-
925
- # Two special directives that should not exists in the db schema
926
- output += 'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n \n '
927
- output += 'directive @_uniqueForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n \n '
979
+
980
+ # Manualy handled directives
981
+ for _dir in manual_directives .values ():
982
+ output += _dir + '\n \n '
928
983
929
984
# For each type, and output the types sortad after name
930
985
for _type in sorted (schema .type_map .values (), key = lambda x : x .name ):
@@ -962,11 +1017,8 @@ def printSchemaWithDirectives(schema):
962
1017
elif not is_enum_or_scalar (_type ):
963
1018
# This should be a type, or an interface
964
1019
965
- if _type .ast_node is not None :
966
- # Get directives on type
967
- for directive in _type .ast_node .directives :
968
- output += ' @' + directive .name .value
969
- output += get_directive_arguments (directive )
1020
+ # Get directives on type
1021
+ output += get_type_directives (_type , schema )
970
1022
971
1023
output += ' {\n '
972
1024
@@ -984,7 +1036,7 @@ def printSchemaWithDirectives(schema):
984
1036
output += ': ' + str (field .type )
985
1037
986
1038
# Add directives
987
- output += get_field_directives (field , field_name , _type )
1039
+ output += get_field_directives (field , field_name , _type , schema )
988
1040
989
1041
output += '\n '
990
1042
@@ -993,4 +1045,4 @@ def printSchemaWithDirectives(schema):
993
1045
if _type .ast_node is not None :
994
1046
output += '\n \n '
995
1047
996
- return output
1048
+ return output
0 commit comments