Skip to content

Commit 957cf58

Browse files
committed
simplify generation of date fields by placing them later in the pipeline, extra util function to distinguish DB schema types
1 parent 59252bd commit 957cf58

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

graphql-api-generator/generator.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,24 @@ def run(schema: GraphQLSchema, config: dict):
7777
if config.get('generation').get('generate_date'):
7878
date_control(schema)
7979

80-
# add creationDate
81-
if config.get('generation').get('field_for_creation_date'):
82-
schema = add_creation_date_to_types(schema)
83-
84-
# add lastUpdateDate
85-
if config.get('generation').get('field_for_last_update_date'):
86-
schema = add_last_update_date_to_types(schema)
87-
8880
# add reverse edges for traversal
8981
if config.get('generation').get('reverse_edges'):
9082
schema = add_reverse_edges(schema)
9183

9284
# add edge types
9385
if config.get('generation').get('edge_types') or config.get('generation').get('create_edge_objects'):
94-
schema = add_edge_objects(schema, config.get('generation').get('field_for_creation_date'), config.get('generation').get('field_for_last_update_date'))
86+
schema = add_edge_objects(schema)
9587
if config.get('generation').get('fields_for_edge_types'):
9688
raise UnsupportedOperation('{0} is currently not supported'.format('fields_for_edge_types'))
9789

90+
# add creation date
91+
if config.get('generation').get('field_for_creation_date'):
92+
schema = add_creation_date_to_types(schema)
93+
94+
# add last update date
95+
if config.get('generation').get('field_for_last_update_date'):
96+
schema = add_last_update_date_to_types(schema)
97+
9898
# add queries
9999
if config.get('generation').get('query_by_id'):
100100
schema = add_get_queries(schema)

graphql-api-generator/utils/utils.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,27 @@ def decapitalize(string: str):
3636
return string[0].lower() + string[1:]
3737

3838

39-
def is_schema_defined_type(_type: GraphQLType):
39+
def is_db_schema_defined_type(_type: GraphQLType):
4040
"""
41-
Returns true if a type is a schema-defined GraphQL type.
41+
Returns true if _type is a GraphQL type defined in the DB schema.
4242
:param _type:
4343
:return:
4444
"""
4545
if is_input_type(_type) or _type.name.startswith('_') or _type.name == 'Mutation' or _type.name == 'Query':
4646
return False
47+
48+
return True
49+
50+
51+
def is_schema_defined_type(_type: GraphQLType):
52+
"""
53+
Returns true if _type is a schema-defined GraphQL type.
54+
:param _type:
55+
:return:
56+
"""
57+
if is_input_type(_type) or _type.name.startswith('__') or _type.name == 'Mutation' or _type.name == 'Query':
58+
return False
59+
4760
return True
4861

4962

@@ -89,7 +102,7 @@ def add_id_to_types(schema: GraphQLSchema):
89102
"""
90103
make = ''
91104
for _type in schema.type_map.values():
92-
if not is_schema_defined_type(_type):
105+
if not is_db_schema_defined_type(_type):
93106
continue
94107
if is_interface_type(_type):
95108
make += f'extend interface {_type.name} {{ id: ID! }} '
@@ -108,6 +121,7 @@ def add_creation_date_to_types(schema: GraphQLSchema):
108121
for _type in schema.type_map.values():
109122
if not is_schema_defined_type(_type):
110123
continue
124+
111125
if is_interface_type(_type):
112126
make += f'extend interface {_type.name} {{ _creationDate: DateTime! }} '
113127
else:
@@ -168,7 +182,7 @@ def add_reverse_edges(schema: GraphQLSchema):
168182
"""
169183
make = ''
170184
for _type in schema.type_map.values():
171-
if not is_schema_defined_type(_type):
185+
if not is_db_schema_defined_type(_type):
172186
continue
173187

174188
for field_name, field_type in _type.fields.items():
@@ -200,15 +214,15 @@ def add_input_to_create(schema: GraphQLSchema):
200214
# add create types (placeholders)
201215
make = ''
202216
for _type in schema.type_map.values():
203-
if not is_schema_defined_type(_type) or is_interface_type(_type):
217+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
204218
continue
205219
make += f'input _InputToCreate{_type.name} '
206220
schema = add_to_schema(schema, make)
207221

208222
# add fields to create types
209223
make = ''
210224
for _type in schema.type_map.values():
211-
if not is_schema_defined_type(_type) or is_interface_type(_type):
225+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
212226
continue
213227
make += f'\nextend input _InputToCreate{_type.name} {{ '
214228
for field_name, field in _type.fields.items():
@@ -248,7 +262,7 @@ def add_key_input_types(schema: GraphQLSchema):
248262
make_types = ''
249263
extend_fields = ''
250264
for _type in schema.type_map.values():
251-
if not is_schema_defined_type(_type) or is_interface_type(_type):
265+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
252266
continue
253267
keys = _get_keys_for_type(_type)
254268
# TODO: Modify this when we need to handle multiple keys.
@@ -279,7 +293,7 @@ def add_key_queries(schema: GraphQLSchema):
279293
# Create queries for object types
280294
make = ''
281295
for _type in schema.type_map.values():
282-
if not is_schema_defined_type(_type):
296+
if not is_db_schema_defined_type(_type):
283297
continue
284298
keys = _get_keys_for_type(_type)
285299
# TODO: Handle multiple keys here, somehow.
@@ -329,7 +343,7 @@ def add_input_update(schema: GraphQLSchema):
329343
# Create update inputs
330344
make = ''
331345
for _type in schema.type_map.values():
332-
if not is_schema_defined_type(_type) or is_interface_type(_type):
346+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
333347
continue
334348
update_name = f'_InputToUpdate{_type.name}'
335349
make += f'input {update_name} '
@@ -338,7 +352,7 @@ def add_input_update(schema: GraphQLSchema):
338352
# Add fields to update type
339353
make = ''
340354
for _type in schema.type_map.values():
341-
if not is_schema_defined_type(_type) or is_interface_type(_type):
355+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
342356
continue
343357
for field_name, field in _type.fields.items():
344358
if field_name == 'id' or field_name[0] == '_':
@@ -368,7 +382,7 @@ def add_get_queries(schema: GraphQLSchema):
368382
# Create queries for object types
369383
make = ''
370384
for _type in schema.type_map.values():
371-
if not is_schema_defined_type(_type):
385+
if not is_db_schema_defined_type(_type):
372386
continue
373387
make += f'extend type Query {{ {decapitalize(_type.name)}(id:ID!): {_type.name} }} '
374388
schema = add_to_schema(schema, make)
@@ -383,7 +397,7 @@ def add_list_of_types(schema: GraphQLSchema):
383397
"""
384398
make = ''
385399
for _type in schema.type_map.values():
386-
if not is_schema_defined_type(_type):
400+
if not is_db_schema_defined_type(_type):
387401
continue
388402

389403
make += f'type _ListOf{_type.name}s {{ ' \
@@ -403,7 +417,7 @@ def add_list_queries(schema: GraphQLSchema):
403417
"""
404418
make = ''
405419
for _type in schema.type_map.values():
406-
if not is_schema_defined_type(_type):
420+
if not is_db_schema_defined_type(_type):
407421
continue
408422
make += f'extend type Query {{ ' \
409423
f' listOf{_type.name}s(first:Int=10, after:ID="", filter:_FilterFor{_type.name}): _ListOf{_type.name}s ' \
@@ -523,7 +537,7 @@ def add_type_filters(schema: GraphQLSchema, field_for_creation_date, field_for_l
523537
"""
524538
make = ''
525539
for _type in schema.type_map.values():
526-
if not is_schema_defined_type(_type):
540+
if not is_db_schema_defined_type(_type):
527541
continue
528542

529543
make += f'input _FilterFor{_type.name} {{ ' \
@@ -560,7 +574,7 @@ def add_object_type_filters(schema: GraphQLSchema):
560574
:return:
561575
"""
562576
for _type in schema.type_map.values():
563-
if not is_schema_defined_type(_type):
577+
if not is_db_schema_defined_type(_type):
564578
continue
565579

566580
for field_name, field in _type.fields.items():
@@ -588,16 +602,10 @@ def get_field_annotations(field: GraphQLField):
588602
return " ".join(annotation_fields)
589603

590604

591-
def add_edge_objects(schema: GraphQLSchema, field_for_creation_date, field_for_last_update_date):
605+
def add_edge_objects(schema: GraphQLSchema):
592606
make = ''
593-
creation_date_string = ''
594-
last_update_date_string = ''
595-
if field_for_creation_date:
596-
creation_date_string = '_creationDate: DateTime!'
597-
if field_for_last_update_date:
598-
last_update_date_string = '_lastUpdateDate: DateTime'
599607
for _type in schema.type_map.values():
600-
if not is_schema_defined_type(_type) or is_interface_type(_type):
608+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
601609
continue
602610
connected_types = schema.get_possible_types(_type) if is_interface_type(_type) else [_type]
603611
for field_name, field in _type.fields.items():
@@ -607,7 +615,7 @@ def add_edge_objects(schema: GraphQLSchema, field_for_creation_date, field_for_l
607615
for t in connected_types:
608616
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
609617
annotations = get_field_annotations(field)
610-
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {creation_date_string} {last_update_date_string} {annotations}}}\n'
618+
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {annotations}}}\n'
611619

612620
schema = add_to_schema(schema, make)
613621
return schema
@@ -616,7 +624,7 @@ def add_edge_objects(schema: GraphQLSchema, field_for_creation_date, field_for_l
616624
def add_input_to_create_edge_objects(schema: GraphQLSchema):
617625
make = ''
618626
for _type in schema.type_map.values():
619-
if not is_schema_defined_type(_type) or is_interface_type(_type):
627+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
620628
continue
621629
connected_types = schema.get_possible_types(_type) if is_interface_type(_type) else [_type]
622630
for field_name, field in _type.fields.items():
@@ -642,7 +650,7 @@ def add_input_to_create_edge_objects(schema: GraphQLSchema):
642650
def add_mutation_create_edge_objects(schema: GraphQLSchema):
643651
make = ''
644652
for _type in schema.type_map.values():
645-
if not is_schema_defined_type(_type) or is_interface_type(_type):
653+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
646654
continue
647655
connected_types = schema.get_possible_types(_type) if is_interface_type(_type) else [_type]
648656
for field_name, field in _type.fields.items():
@@ -662,7 +670,7 @@ def add_mutation_create_edge_objects(schema: GraphQLSchema):
662670
def remove_field_arguments_for_types(schema: GraphQLSchema):
663671
keep_args = ['filter']
664672
for _type in schema.type_map.values():
665-
if not is_schema_defined_type(_type):
673+
if not is_db_schema_defined_type(_type):
666674
continue
667675
for field_name, field in _type.fields.items():
668676
args = {}
@@ -702,7 +710,7 @@ def add_create_mutations(schema: GraphQLSchema):
702710
"""
703711
make = ''
704712
for _type in schema.type_map.values():
705-
if not is_schema_defined_type(_type) or is_interface_type(_type):
713+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
706714
continue
707715
create = f'create{_type.name}'
708716
input_type = f'_InputToCreate{_type.name}'
@@ -720,7 +728,7 @@ def add_update_mutations(schema: GraphQLSchema):
720728
"""
721729
make = ''
722730
for _type in schema.type_map.values():
723-
if not is_schema_defined_type(_type) or is_interface_type(_type):
731+
if not is_db_schema_defined_type(_type) or is_interface_type(_type):
724732
continue
725733
update = f'update{capitalize(_type.name)} '
726734
input_type = f'_InputToUpdate{_type.name}'
@@ -738,7 +746,7 @@ def add_delete_mutations(schema: GraphQLSchema):
738746
"""
739747
make = ''
740748
for _type in schema.type_map.values():
741-
if not is_schema_defined_type(_type):
749+
if not is_db_schema_defined_type(_type):
742750
continue
743751
delete = f'delete{_type.name}'
744752
make += f'extend type Mutation {{ {delete}(id: ID!): {_type.name} }} '

0 commit comments

Comments
 (0)