Skip to content

Commit 7ef87a4

Browse files
committed
add directive @required to manually added drirectives, minor optimizations to schema printer
1 parent 3db6b3a commit 7ef87a4

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

graphql-api-generator/utils/utils.py

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -947,72 +947,61 @@ def get_type_directives(_type, schema):
947947

948948
def print_schema_with_directives(schema):
949949
"""
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.
951951
Types and fields will all contain directives
952952
:param schema:
953953
:return string:
954954
"""
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+
}
966965
output = ''
967-
968966
# Add directives
969967
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'
986979

987-
# Manualy handled directives
980+
# Manually handled directives
988981
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'
993983

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):
994986
# Internal type
995-
if _type.name[:2] == '__':
987+
if _type.name.startswith('__'):
996988
continue
997989

998990
if is_interface_type(_type):
999991
output += 'interface ' + _type.name
1000992
elif is_enum_type(_type):
1001993
output += 'enum ' + _type.name
1002994
elif is_scalar_type(_type):
995+
# Skip non-user defined directives
1003996
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
1006997
output += 'scalar ' + _type.name
1007998
elif is_input_type(_type):
1008999
output += 'input ' + _type.name
1009-
else: # type, hopefully
1000+
else:
10101001
output += 'type ' + _type.name
10111002
if hasattr(_type, 'interfaces') and _type.interfaces:
10121003
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])
10161005

10171006
if is_enum_type(_type):
10181007
# For enums we can get the values directly and add them
@@ -1023,10 +1012,8 @@ def print_schema_with_directives(schema):
10231012

10241013
elif not is_enum_or_scalar(_type):
10251014
# This should be a type, or an interface
1026-
10271015
# Get directives on type
10281016
output += get_type_directives(_type, schema)
1029-
10301017
output += ' {\n'
10311018

10321019
# Get fields
@@ -1035,21 +1022,18 @@ def print_schema_with_directives(schema):
10351022

10361023
# Get arguments for field
10371024
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})'
10421027

10431028
output += ': ' + str(field.type)
10441029

10451030
# Add directives
10461031
output += get_field_directives(field_name, _type, schema)
1047-
10481032
output += '\n'
10491033

10501034
output += '}'
10511035

10521036
if _type.ast_node is not None:
10531037
output += '\n\n'
10541038

1055-
return output
1039+
return output

0 commit comments

Comments
 (0)