Skip to content

Commit 8d90268

Browse files
committed
Add edge objects to schema.
TODO: Properly split out behavior between different config flags.
1 parent 61b2d7f commit 8d90268

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

graphql-api-generator/generator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ def run(schema: GraphQLSchema, config: dict):
8686
schema = add_scalar_filters(schema)
8787
schema = add_type_filters(schema)
8888

89-
# remove field arguments for edges (should not be in the API schema)
90-
schema = remove_field_arguments_for_types(schema)
91-
9289
if config.get('generation').get('query_type_filter'):
9390
schema = add_object_type_filters(schema)
9491

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

109106
# add edge input types
110107
if config.get('generation').get('input_to_create_edge_objects'):
111-
raise UnsupportedOperation('{0} is currently not supported'.format('input_to_create_edge_objects'))
108+
schema = add_create_edge_objects(schema)
112109
if config.get('generation').get('input_to_update_edge_objects'):
113110
raise UnsupportedOperation('{0} is currently not supported'.format('input_to_update_edge_objects'))
114111

@@ -128,10 +125,13 @@ def run(schema: GraphQLSchema, config: dict):
128125
if config.get('generation').get('delete_edge_objects'):
129126
raise UnsupportedOperation('{0} is currently not supported'.format('delete_edge_objects'))
130127

128+
# remove field arguments for edges (should not be in the API schema)
129+
schema = remove_field_arguments_for_types(schema)
130+
131131
return schema
132132

133133

134-
def validate_names(schema:GraphQLSchema, validate):
134+
def validate_names(schema: GraphQLSchema, validate):
135135
# types and interfaces
136136
if validate.get('type_names'):
137137
# type names

graphql-api-generator/resources/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ generation:
2626
input_to_create_objects: true
2727
input_to_update_objects: true
2828
# add edge input types (not supported)
29-
input_to_create_edge_objects: false
29+
input_to_create_edge_objects: true
3030
input_to_update_edge_objects: false
3131
# add mutations
3232
create_objects: true

graphql-api-generator/utils/utils.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,64 @@ def add_object_type_filters(schema: GraphQLSchema):
516516
return schema
517517

518518

519+
def add_create_edge_objects(schema: GraphQLSchema):
520+
make = ''
521+
for _type in schema.type_map.values():
522+
if not is_schema_defined_type(_type) or is_interface_type(_type):
523+
continue
524+
525+
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):
531+
continue
532+
533+
if is_interface_type(_type):
534+
connected_types = schema.get_possible_types(_type)
535+
else:
536+
connected_types = [_type]
537+
538+
for t in connected_types:
539+
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
540+
edge_create = f'create{edge_from}'
541+
edge_input = f'_InputToCreate{edge_from}'
542+
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)
552+
553+
if len(annotations) > 0:
554+
make += f'input {edge_input} {{sourceID: ID! targetID: ID! annotations: {annotate_input} }}\n'
555+
make += f'input {annotate_input}{{{annotations}}}\n'
556+
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {annotations}}}\n'
557+
else:
558+
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'
560+
make += f'extend type Mutation{{{edge_create}(data: {edge_input}):_{edge_from}}}\n'
561+
562+
schema = add_to_schema(schema, make)
563+
return schema
564+
565+
519566
def remove_field_arguments_for_types(schema: GraphQLSchema):
567+
keep_args = ['filter']
520568
for _type in schema.type_map.values():
521569
if not is_schema_defined_type(_type):
522570
continue
523571
for field_name, field in _type.fields.items():
524-
field.args = {}
572+
args = {}
573+
for arg in field.args:
574+
if arg in keep_args:
575+
args[arg] = field.args[arg]
576+
field.args = args
525577
return schema
526578

527579

0 commit comments

Comments
 (0)