Skip to content

Commit 67d30f0

Browse files
committed
Split edge object generation into three phases (object, input object, create mutation), minor clean-up.
1 parent 8d90268 commit 67d30f0

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

graphql-api-generator/generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def run(schema: GraphQLSchema, config: dict):
7373
schema = add_reverse_edges(schema)
7474

7575
# add edge types
76-
if config.get('generation').get('edge_types'):
77-
raise UnsupportedOperation('{0} is currently not supported'.format('edgeTypes'))
76+
if config.get('generation').get('edge_types') or config.get('generation').get('create_edge_objects'):
77+
schema = add_edge_objects(schema)
7878
if config.get('generation').get('fields_for_edge_types'):
7979
raise UnsupportedOperation('{0} is currently not supported'.format('fields_for_edge_types'))
8080

@@ -105,7 +105,7 @@ def run(schema: GraphQLSchema, config: dict):
105105

106106
# add edge input types
107107
if config.get('generation').get('input_to_create_edge_objects'):
108-
schema = add_create_edge_objects(schema)
108+
schema = add_input_to_create_edge_objects(schema)
109109
if config.get('generation').get('input_to_update_edge_objects'):
110110
raise UnsupportedOperation('{0} is currently not supported'.format('input_to_update_edge_objects'))
111111

@@ -119,7 +119,7 @@ def run(schema: GraphQLSchema, config: dict):
119119

120120
# add edge mutations
121121
if config.get('generation').get('create_edge_objects'):
122-
raise UnsupportedOperation('{0} is currently not supported'.format('create_edge_objects'))
122+
schema = add_mutation_create_edge_objects(schema)
123123
if config.get('generation').get('update_edge_objects'):
124124
raise UnsupportedOperation('{0} is currently not supported'.format('update_edge_objects'))
125125
if config.get('generation').get('delete_edge_objects'):

graphql-api-generator/resources/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ generation:
2525
# add input types
2626
input_to_create_objects: true
2727
input_to_update_objects: true
28-
# add edge input types (not supported)
28+
# add edge input types (update not supported)
2929
input_to_create_edge_objects: true
3030
input_to_update_edge_objects: false
3131
# add mutations
3232
create_objects: true
3333
update_objects: true
34-
delete_objects: true
35-
# add edge mutations (not supported)
36-
create_edge_objects: false
34+
delete_objects: false
35+
# add edge mutations (update and delete not supported)
36+
create_edge_objects: true
3737
update_edge_objects: false
3838
delete_edge_objects: false

graphql-api-generator/utils/utils.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ def add_reverse_edges(schema: GraphQLSchema):
138138
continue
139139

140140
for field_name, field_type in _type.fields.items():
141-
inner_field_type = get_named_type(field_type.type)
142-
if is_scalar_type(inner_field_type) or is_enum_type(inner_field_type):
141+
if is_enum_or_scalar(get_named_type(field_type.type)):
143142
continue
144143

145144
# Reverse edge
@@ -516,47 +515,76 @@ def add_object_type_filters(schema: GraphQLSchema):
516515
return schema
517516

518517

519-
def add_create_edge_objects(schema: GraphQLSchema):
518+
def get_field_annotations(field: GraphQLField):
519+
annotation_fields = []
520+
for arg, arg_type in field.args.items():
521+
if arg == 'filter':
522+
continue
523+
if not is_enum_or_scalar(get_named_type(arg_type.type)):
524+
raise Exception("Input object fields are not supported.")
525+
annotation_fields.append(f'{arg}: {arg_type.type}')
526+
return " ".join(annotation_fields)
527+
528+
529+
def add_edge_objects(schema: GraphQLSchema):
520530
make = ''
521531
for _type in schema.type_map.values():
522532
if not is_schema_defined_type(_type) or is_interface_type(_type):
523533
continue
524-
534+
connected_types = schema.get_possible_types(_type) if is_interface_type(_type) else [_type]
525535
for field_name, field in _type.fields.items():
526-
if field_name[0] == '_':
527-
continue
528-
field_type = field.type
529-
inner_field_type = get_named_type(field_type)
530-
if is_scalar_type(inner_field_type) or is_enum_type(inner_field_type):
536+
inner_field_type = get_named_type(field.type)
537+
if field_name.startswith('_') or is_enum_or_scalar(inner_field_type):
531538
continue
539+
for t in connected_types:
540+
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
541+
annotations = get_field_annotations(field)
542+
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {annotations}}}\n'
543+
544+
schema = add_to_schema(schema, make)
545+
return schema
532546

533-
if is_interface_type(_type):
534-
connected_types = schema.get_possible_types(_type)
535-
else:
536-
connected_types = [_type]
537547

548+
def add_input_to_create_edge_objects(schema: GraphQLSchema):
549+
make = ''
550+
for _type in schema.type_map.values():
551+
if not is_schema_defined_type(_type) or is_interface_type(_type):
552+
continue
553+
connected_types = schema.get_possible_types(_type) if is_interface_type(_type) else [_type]
554+
for field_name, field in _type.fields.items():
555+
inner_field_type = get_named_type(field.type)
556+
if field_name.startswith('_') or is_enum_or_scalar(inner_field_type):
557+
continue
538558
for t in connected_types:
539559
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
540-
edge_create = f'create{edge_from}'
541560
edge_input = f'_InputToCreate{edge_from}'
542561
annotate_input = f'_InputToAnnotate{edge_from}'
543-
544-
annotation_fields = []
545-
for arg, arg_type in field.args.items():
546-
if arg == 'filter':
547-
continue
548-
# if not is_scalar_type(arg_type.type.name) and not is_enum_type(arg_type.type.name):
549-
# raise Exception("Fields cannot be object types")
550-
annotation_fields.append(f'{arg}: {arg_type.type}')
551-
annotations = " ".join(annotation_fields)
562+
annotations = get_field_annotations(field)
552563

553564
if len(annotations) > 0:
554565
make += f'input {edge_input} {{sourceID: ID! targetID: ID! annotations: {annotate_input} }}\n'
555566
make += f'input {annotate_input}{{{annotations}}}\n'
556-
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {annotations}}}\n'
557567
else:
558568
make += f'input {edge_input} {{sourceID: ID! targetID: ID!}}\n'
559-
make += f'type _{edge_from} {{id: ID! source: {t.name}! target: {inner_field_type}!}}\n'
569+
570+
schema = add_to_schema(schema, make)
571+
return schema
572+
573+
574+
def add_mutation_create_edge_objects(schema: GraphQLSchema):
575+
make = ''
576+
for _type in schema.type_map.values():
577+
if not is_schema_defined_type(_type) or is_interface_type(_type):
578+
continue
579+
connected_types = schema.get_possible_types(_type) if is_interface_type(_type) else [_type]
580+
for field_name, field in _type.fields.items():
581+
inner_field_type = get_named_type(field.type)
582+
if field_name.startswith('_') or is_enum_or_scalar(inner_field_type):
583+
continue
584+
for t in connected_types:
585+
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
586+
edge_create = f'create{edge_from}'
587+
edge_input = f'_InputToCreate{edge_from}'
560588
make += f'extend type Mutation{{{edge_create}(data: {edge_input}):_{edge_from}}}\n'
561589

562590
schema = add_to_schema(schema, make)

0 commit comments

Comments
 (0)