Skip to content

Commit 4d217dd

Browse files
committed
Added some comments and fixed some phytonic errors
1 parent e0a0dbf commit 4d217dd

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

graphql-api-generator/utils/utils.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ def get_directive_arguments(directive):
829829
"""
830830

831831
output = ''
832-
if len(directive.arguments) > 0:
832+
if directive.arguments:
833833
output+= '('
834834
for arg in directive.arguments:
835835
output+= arg.name.value + ':'
@@ -859,16 +859,23 @@ def get_directive_arguments(directive):
859859

860860

861861
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+
"""
862868

863869
output = ''
864870

871+
# Start by adding directives
865872
for _dir in schema.directives:
866873
if _dir.ast_node is not None:
867874
# If the directive does not have a proper ast_node
868875
# Then it is an non-user defined directive, and can hence, be skipped
869876
output+= 'directive @' + _dir.name
870877

871-
if len(_dir.ast_node.arguments) > 0:
878+
if _dir.ast_node.arguments:
872879
output+= '('
873880
for arg in _dir.ast_node.arguments:
874881
output+= arg.name.value + ': ' + ast_type_to_string(arg.type) + ', '
@@ -880,12 +887,14 @@ def printSchemaWithDirectives(schema):
880887

881888
output = output[:-2] + '\n\n'
882889

883-
890+
# Two special directives that should not exists in the db schema
884891
output += 'directive @_requiredForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n\n'
885892
output += 'directive @_uniqueForTarget_AccordingToInterface(interface: String!) on FIELD_DEFINITION\n\n'
886893

887-
894+
# For each type, and output the types sortad after name
888895
for _type in sorted(schema.type_map.values(), key=lambda x : x.name):
896+
897+
# Internal type
889898
if _type.name[:2] == '__':
890899
continue
891900

@@ -902,29 +911,35 @@ def printSchemaWithDirectives(schema):
902911
output += 'input ' + _type.name
903912
else: # type, hopefully
904913
output += 'type ' + _type.name
905-
if hasattr(_type, 'interfaces') and len(_type.interfaces) > 0:
914+
if hasattr(_type, 'interfaces') and _type.interfaces:
906915
output += ' implements '
907916
for interface in _type.interfaces:
908917
output += interface.name + ', '
909918
output = output[:-2]
910919

911920
if is_enum_type(_type):
921+
# For enums we can get the values directly and add them
912922
output += ' {\n'
913923
for value in _type.values:
914924
output += ' ' + value + '\n'
915925
output += '}'
916926

917927
elif not is_enum_or_scalar(_type):
928+
# This should be a type, or an interface
929+
918930
if _type.ast_node is not None:
931+
# Get directives on type
919932
for directive in _type.ast_node.directives:
920933
output+= ' @' + directive.name.value
921934
output += get_directive_arguments(directive)
922935

923936
output += ' {\n'
924937

938+
# Get fields
925939
for field_name, field in _type.fields.items():
926940
output += ' ' + field_name
927941

942+
# Get arguments for field
928943
if hasattr(field, 'args') and field.args:
929944
output += '('
930945
for arg_name, arg in field.args.items():
@@ -933,8 +948,10 @@ def printSchemaWithDirectives(schema):
933948

934949
output += ': ' + str(field.type)
935950

951+
# Used to make sure we don't add the same directive multiple times to the same field
936952
directives_set = set()
937953

954+
# Get all directives directly on field
938955
for directive in field.ast_node.directives:
939956
if not directive.name.value in directives_set:
940957
output+= ' @' + directive.name.value
@@ -943,6 +960,7 @@ def printSchemaWithDirectives(schema):
943960

944961

945962
if hasattr(_type, 'interfaces'):
963+
# Get all inherited directives
946964
for interface in _type.interfaces:
947965
if field_name in interface.fields:
948966
for directive in interface.fields[field_name].ast_node.directives:
@@ -955,7 +973,7 @@ def printSchemaWithDirectives(schema):
955973
output += '\n'
956974

957975
output += '}'
958-
976+
959977
if _type.ast_node is not None:
960978
output += '\n\n'
961979

0 commit comments

Comments
 (0)