@@ -947,72 +947,61 @@ def get_type_directives(_type, schema):
947
947
948
948
def print_schema_with_directives (schema ):
949
949
"""
950
- Ouputs the given schema as string, in the format we want it.
950
+ Outputs the given schema as string, in the format we want it.
951
951
Types and fields will all contain directives
952
952
:param schema:
953
953
:return string:
954
954
"""
955
-
956
-
957
- manual_directives = {'key' :'directive @key(fields: [String!]!) on OBJECT | INPUT_OBJECT' ,\
958
- 'distinct' :'directive @distinct on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,\
959
- 'noloops' :'directive @noloops on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,\
960
- 'requiredForTarget' :'directive @requiredForTarget on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,\
961
- 'uniqueForTarget' :'directive @uniqueForTarget on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,\
962
- '_requiredForTarget_AccordingToInterface' :'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,\
963
- '_uniqueForTarget_AccordingToInterface' :'directive @_uniqueForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' \
964
- }
965
-
955
+ manual_directives = {
956
+ 'required' : 'directive @required on FIELD_DEFINITION' ,
957
+ 'key' : 'directive @key(fields: [String!]!) on OBJECT | INPUT_OBJECT' ,
958
+ 'distinct' : 'directive @distinct on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,
959
+ 'noloops' : 'directive @noloops on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,
960
+ 'requiredForTarget' : 'directive @requiredForTarget on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,
961
+ 'uniqueForTarget' : 'directive @uniqueForTarget on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,
962
+ '_requiredForTarget_AccordingToInterface' : 'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION' ,
963
+ '_uniqueForTarget_AccordingToInterface' : 'directive @_uniqueForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION'
964
+ }
966
965
output = ''
967
-
968
966
# Add directives
969
967
for _dir in schema .directives :
970
- if _dir .ast_node is not None and _dir .name not in manual_directives .keys ():
971
- # If the directive does not have a proper ast_node
972
- # Then it is an non-user defined directive, and can hence, be skipped
973
- output += 'directive @' + _dir .name
974
-
975
- if _dir .ast_node .arguments :
976
- output += '('
977
- for arg in _dir .ast_node .arguments :
978
- output += arg .name .value + ': ' + ast_type_to_string (arg .type ) + ', '
979
- output = output [:- 2 ] + ')'
980
-
981
- output += ' on '
982
- for _location in _dir .locations :
983
- output += _location ._name_ + ' | '
984
-
985
- output = output [:- 3 ] + '\n \n '
968
+ # Skip non-user defined directives
969
+ if _dir .ast_node is None or _dir .name in manual_directives .keys ():
970
+ continue
971
+
972
+ output += f'directive @{ _dir .name } '
973
+ if _dir .ast_node .arguments :
974
+ args = ', ' .join ([f'{ arg .name .value } : { ast_type_to_string (arg .type )} ' for arg in _dir .ast_node .arguments ])
975
+ output += f'({ args } )'
976
+
977
+ output += ' on ' + ' | ' .join ([loc .name for loc in _dir .locations ])
978
+ output += '\n \n '
986
979
987
- # Manualy handled directives
980
+ # Manually handled directives
988
981
for _dir in manual_directives .values ():
989
- output += _dir + '\n \n '
990
-
991
- # For each type, and output the types sortad after name
992
- for _type in sorted (schema .type_map .values (), key = lambda x : x .name ):
982
+ output += _dir + '\n \n '
993
983
984
+ # For each type, and output the types sorted by name
985
+ for _type in sorted (schema .type_map .values (), key = lambda x : x .name ):
994
986
# Internal type
995
- if _type .name [: 2 ] == '__' :
987
+ if _type .name . startswith ( '__' ) :
996
988
continue
997
989
998
990
if is_interface_type (_type ):
999
991
output += 'interface ' + _type .name
1000
992
elif is_enum_type (_type ):
1001
993
output += 'enum ' + _type .name
1002
994
elif is_scalar_type (_type ):
995
+ # Skip non-user defined directives
1003
996
if _type .ast_node is not None :
1004
- # If the scalar does not have a proper ast_node
1005
- # Then it is an non-user defined scalar, and can hence, be skipped
1006
997
output += 'scalar ' + _type .name
1007
998
elif is_input_type (_type ):
1008
999
output += 'input ' + _type .name
1009
- else : # type, hopefully
1000
+ else :
1010
1001
output += 'type ' + _type .name
1011
1002
if hasattr (_type , 'interfaces' ) and _type .interfaces :
1012
1003
output += ' implements '
1013
- for interface in _type .interfaces :
1014
- output += interface .name + ' & '
1015
- output = output [:- 3 ]
1004
+ output += ' & ' .join ([interface .name for interface in _type .interfaces ])
1016
1005
1017
1006
if is_enum_type (_type ):
1018
1007
# For enums we can get the values directly and add them
@@ -1023,10 +1012,8 @@ def print_schema_with_directives(schema):
1023
1012
1024
1013
elif not is_enum_or_scalar (_type ):
1025
1014
# This should be a type, or an interface
1026
-
1027
1015
# Get directives on type
1028
1016
output += get_type_directives (_type , schema )
1029
-
1030
1017
output += ' {\n '
1031
1018
1032
1019
# Get fields
@@ -1035,21 +1022,18 @@ def print_schema_with_directives(schema):
1035
1022
1036
1023
# Get arguments for field
1037
1024
if hasattr (field , 'args' ) and field .args :
1038
- output += '('
1039
- for arg_name , arg in field .args .items ():
1040
- output += arg_name + ': ' + str (arg .type ) + ', '
1041
- output = output [:- 2 ] + ')'
1025
+ args = ', ' .join ([f'{ arg_name } : { arg .type } ' for arg_name , arg in field .args .items ()])
1026
+ output += f'({ args } )'
1042
1027
1043
1028
output += ': ' + str (field .type )
1044
1029
1045
1030
# Add directives
1046
1031
output += get_field_directives (field_name , _type , schema )
1047
-
1048
1032
output += '\n '
1049
1033
1050
1034
output += '}'
1051
1035
1052
1036
if _type .ast_node is not None :
1053
1037
output += '\n \n '
1054
1038
1055
- return output
1039
+ return output
0 commit comments