@@ -829,7 +829,7 @@ def get_directive_arguments(directive):
829
829
"""
830
830
831
831
output = ''
832
- if len ( directive .arguments ) > 0 :
832
+ if directive .arguments :
833
833
output += '('
834
834
for arg in directive .arguments :
835
835
output += arg .name .value + ':'
@@ -859,16 +859,23 @@ def get_directive_arguments(directive):
859
859
860
860
861
861
def printSchemaWithDirectives (schema ):
862
+ """
863
+ Ouputs the given schema as string, in the format we want it.
864
+ Types and fields will all contain directives
865
+ :param schema:
866
+ :return string:
867
+ """
862
868
863
869
output = ''
864
870
871
+ # Start by adding directives
865
872
for _dir in schema .directives :
866
873
if _dir .ast_node is not None :
867
874
# If the directive does not have a proper ast_node
868
875
# Then it is an non-user defined directive, and can hence, be skipped
869
876
output += 'directive @' + _dir .name
870
877
871
- if len ( _dir .ast_node .arguments ) > 0 :
878
+ if _dir .ast_node .arguments :
872
879
output += '('
873
880
for arg in _dir .ast_node .arguments :
874
881
output += arg .name .value + ': ' + ast_type_to_string (arg .type ) + ', '
@@ -880,12 +887,14 @@ def printSchemaWithDirectives(schema):
880
887
881
888
output = output [:- 2 ] + '\n \n '
882
889
883
-
890
+ # Two special directives that should not exists in the db schema
884
891
output += 'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n \n '
885
892
output += 'directive @_uniqueForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n \n '
886
893
887
-
894
+ # For each type, and output the types sortad after name
888
895
for _type in sorted (schema .type_map .values (), key = lambda x : x .name ):
896
+
897
+ # Internal type
889
898
if _type .name [:2 ] == '__' :
890
899
continue
891
900
@@ -902,29 +911,35 @@ def printSchemaWithDirectives(schema):
902
911
output += 'input ' + _type .name
903
912
else : # type, hopefully
904
913
output += 'type ' + _type .name
905
- if hasattr (_type , 'interfaces' ) and len ( _type .interfaces ) > 0 :
914
+ if hasattr (_type , 'interfaces' ) and _type .interfaces :
906
915
output += ' implements '
907
916
for interface in _type .interfaces :
908
917
output += interface .name + ', '
909
918
output = output [:- 2 ]
910
919
911
920
if is_enum_type (_type ):
921
+ # For enums we can get the values directly and add them
912
922
output += ' {\n '
913
923
for value in _type .values :
914
924
output += ' ' + value + '\n '
915
925
output += '}'
916
926
917
927
elif not is_enum_or_scalar (_type ):
928
+ # This should be a type, or an interface
929
+
918
930
if _type .ast_node is not None :
931
+ # Get directives on type
919
932
for directive in _type .ast_node .directives :
920
933
output += ' @' + directive .name .value
921
934
output += get_directive_arguments (directive )
922
935
923
936
output += ' {\n '
924
937
938
+ # Get fields
925
939
for field_name , field in _type .fields .items ():
926
940
output += ' ' + field_name
927
941
942
+ # Get arguments for field
928
943
if hasattr (field , 'args' ) and field .args :
929
944
output += '('
930
945
for arg_name , arg in field .args .items ():
@@ -933,8 +948,10 @@ def printSchemaWithDirectives(schema):
933
948
934
949
output += ': ' + str (field .type )
935
950
951
+ # Used to make sure we don't add the same directive multiple times to the same field
936
952
directives_set = set ()
937
953
954
+ # Get all directives directly on field
938
955
for directive in field .ast_node .directives :
939
956
if not directive .name .value in directives_set :
940
957
output += ' @' + directive .name .value
@@ -943,6 +960,7 @@ def printSchemaWithDirectives(schema):
943
960
944
961
945
962
if hasattr (_type , 'interfaces' ):
963
+ # Get all inherited directives
946
964
for interface in _type .interfaces :
947
965
if field_name in interface .fields :
948
966
for directive in interface .fields [field_name ].ast_node .directives :
@@ -955,7 +973,7 @@ def printSchemaWithDirectives(schema):
955
973
output += '\n '
956
974
957
975
output += '}'
958
-
976
+
959
977
if _type .ast_node is not None :
960
978
output += '\n \n '
961
979
0 commit comments