Skip to content

Commit 5773d3e

Browse files
committed
Added support for adding creationDate and lastUpdateDate fields to all types
1 parent 48fd687 commit 5773d3e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

graphql-api-generator/generator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def run(schema: GraphQLSchema, config: dict):
6868
if config.get('generation').get('field_for_id'):
6969
schema = add_id_to_types(schema)
7070

71+
# add creationDate
72+
if config.get('generation').get('field_for_creation_date'):
73+
schema = add_creation_date_to_types(schema)
74+
75+
# add lastUpdateDate
76+
if config.get('generation').get('field_for_last_update_date'):
77+
schema = add_last_update_date_to_types(schema)
78+
7179
# add reverse edges for traversal
7280
if config.get('generation').get('reverse_edges'):
7381
schema = add_reverse_edges(schema)

graphql-api-generator/utils/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ def add_id_to_types(schema: GraphQLSchema):
9898
return add_to_schema(schema, make)
9999

100100

101+
def add_creation_date_to_types(schema: GraphQLSchema):
102+
"""
103+
Extend all object types in the schema with an creationDate field.
104+
:param schema:
105+
:return:
106+
"""
107+
make = ''
108+
for _type in schema.type_map.values():
109+
if not is_schema_defined_type(_type):
110+
continue
111+
if is_interface_type(_type):
112+
make += f'extend interface {_type.name} {{ _creationDate: Date! }} '
113+
else:
114+
make += f'extend type {_type.name} {{ _creationDate: Date! }} '
115+
return add_to_schema(schema, make)
116+
117+
118+
def add_last_update_date_to_types(schema: GraphQLSchema):
119+
"""
120+
Extend all object types in the schema with an lastUpdateDate field.
121+
:param schema:
122+
:return:
123+
"""
124+
make = ''
125+
for _type in schema.type_map.values():
126+
if not is_schema_defined_type(_type):
127+
continue
128+
if is_interface_type(_type):
129+
make += f'extend interface {_type.name} {{ _lastUpdateDate: Date }} '
130+
else:
131+
make += f'extend type {_type.name} {{ _lastUpdateDate: Date }} '
132+
return add_to_schema(schema, make)
133+
134+
101135
def copy_wrapper_structure(_type: GraphQLType, original: GraphQLType):
102136
"""
103137
Copy the wrapper structure of original to _type.

0 commit comments

Comments
 (0)