Skip to content

Commit da5f223

Browse files
committed
Corrected some minor stuff and added a new helper function
1 parent 4d217dd commit da5f223

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

graphql-api-generator/utils/utils.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,20 @@ def add_input_to_create(schema: GraphQLSchema):
247247
for _type in schema.type_map.values():
248248
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
249249
continue
250-
make += f'\nextend input _InputToCreate{_type.name} {{ '
250+
make += f'\nextend input _InputToCreate{_type.name} {{'
251251
for field_name, field in _type.fields.items():
252252
if field_name == 'id' or field_name[0] == '_':
253253
continue
254254
inner_field_type = get_named_type(field.type)
255+
255256
if is_enum_or_scalar(inner_field_type):
256-
make += f'{field_name}: {field.type} '
257+
make += f' {field_name}: {field.type} '
257258
else:
258259
schema = extend_connect(schema, _type, inner_field_type, field_name)
259260
connect_name = f'_InputToConnect{capitalize(field_name)}Of{_type.name}'
260261
connect = copy_wrapper_structure(schema.type_map[connect_name], field.type)
261262
make += f' {field_name}: {connect} '
262-
make += '} '
263+
make += '}'
263264
schema = add_to_schema(schema, make)
264265
return schema
265266

@@ -858,6 +859,39 @@ def get_directive_arguments(directive):
858859
return output
859860

860861

862+
def get_field_directives(field, field_name, _type):
863+
"""
864+
Get the directives of given field, and return them as string
865+
:param schema:
866+
:return string:
867+
"""
868+
869+
output = ''
870+
871+
# Used to make sure we don't add the same directive multiple times to the same field
872+
directives_set = set()
873+
874+
# Get all directives directly on field
875+
for directive in field.ast_node.directives:
876+
if not directive.name.value in directives_set:
877+
output+= ' @' + directive.name.value
878+
directives_set.add(directive.name.value)
879+
output += get_directive_arguments(directive)
880+
881+
882+
if hasattr(_type, 'interfaces'):
883+
# Get all inherited directives
884+
for interface in _type.interfaces:
885+
if field_name in interface.fields:
886+
for directive in interface.fields[field_name].ast_node.directives:
887+
directive_str = directive_from_interface(directive, interface.name)
888+
if not directive_str in directives_set:
889+
output+= ' @' + directive_str
890+
directives_set.add(directive_str)
891+
892+
return output
893+
894+
861895
def printSchemaWithDirectives(schema):
862896
"""
863897
Ouputs the given schema as string, in the format we want it.
@@ -883,9 +917,9 @@ def printSchemaWithDirectives(schema):
883917

884918
output+= ' on '
885919
for _location in _dir.locations:
886-
output+= _location._name_ + ', '
920+
output+= _location._name_ + ' | '
887921

888-
output = output[:-2] + '\n\n'
922+
output = output[:-3] + '\n\n'
889923

890924
# Two special directives that should not exists in the db schema
891925
output += 'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n\n'
@@ -914,8 +948,8 @@ def printSchemaWithDirectives(schema):
914948
if hasattr(_type, 'interfaces') and _type.interfaces:
915949
output += ' implements '
916950
for interface in _type.interfaces:
917-
output += interface.name + ', '
918-
output = output[:-2]
951+
output += interface.name + ' & '
952+
output = output[:-3]
919953

920954
if is_enum_type(_type):
921955
# For enums we can get the values directly and add them
@@ -948,27 +982,8 @@ def printSchemaWithDirectives(schema):
948982

949983
output += ': ' + str(field.type)
950984

951-
# Used to make sure we don't add the same directive multiple times to the same field
952-
directives_set = set()
953-
954-
# Get all directives directly on field
955-
for directive in field.ast_node.directives:
956-
if not directive.name.value in directives_set:
957-
output+= ' @' + directive.name.value
958-
directives_set.add(directive.name.value)
959-
output += get_directive_arguments(directive)
960-
961-
962-
if hasattr(_type, 'interfaces'):
963-
# Get all inherited directives
964-
for interface in _type.interfaces:
965-
if field_name in interface.fields:
966-
for directive in interface.fields[field_name].ast_node.directives:
967-
directive_str = directive_from_interface(directive, interface.name)
968-
if not directive_str in directives_set:
969-
output+= ' @' + directive_str
970-
directives_set.add(directive_str)
971-
985+
# Add directives
986+
output += get_field_directives(field, field_name, _type)
972987

973988
output += '\n'
974989

0 commit comments

Comments
 (0)