Skip to content

Commit 65e9985

Browse files
authored
Merge pull request #40 from LiUGraphQL/Time-data-for-node-and-edge-creation
Time data for node and edge creation
2 parents 48fd687 + 008f33d commit 65e9985

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ generation:
4040
add_mutation_type: true
4141
# add id field to all schema types
4242
field_for_id: true
43+
# add creation date and last update date field(s) to all schema types
44+
generate_datetime: false
45+
field_for_creation_date: true
46+
field_for_last_update_date: true
4347
# add reverse edges for traversal
4448
reverse_edges: true
4549
# add edge types

graphql-api-generator/generator.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def cmd(args):
4949

5050

5151
def run(schema: GraphQLSchema, config: dict):
52+
# check if DateTime exists, or should be added
53+
if config.get('generation').get('generate_datetime') or config.get('generation').get('field_for_creation_date') or config.get('generation').get('field_for_last_update_date'):
54+
datetime_control(schema)
55+
5256
# validate
5357
if config.get('validate'):
5458
validate_names(schema, config.get('validate'))
@@ -68,13 +72,21 @@ def run(schema: GraphQLSchema, config: dict):
6872
if config.get('generation').get('field_for_id'):
6973
schema = add_id_to_types(schema)
7074

75+
# add creationDate
76+
if config.get('generation').get('field_for_creation_date'):
77+
schema = add_creation_date_to_types(schema)
78+
79+
# add lastUpdateDate
80+
if config.get('generation').get('field_for_last_update_date'):
81+
schema = add_last_update_date_to_types(schema)
82+
7183
# add reverse edges for traversal
7284
if config.get('generation').get('reverse_edges'):
7385
schema = add_reverse_edges(schema)
7486

7587
# add edge types
7688
if config.get('generation').get('edge_types') or config.get('generation').get('create_edge_objects'):
77-
schema = add_edge_objects(schema)
89+
schema = add_edge_objects(schema, config.get('generation').get('field_for_creation_date'), config.get('generation').get('field_for_last_update_date'))
7890
if config.get('generation').get('fields_for_edge_types'):
7991
raise UnsupportedOperation('{0} is currently not supported'.format('fields_for_edge_types'))
8092

@@ -132,6 +144,7 @@ def run(schema: GraphQLSchema, config: dict):
132144

133145

134146
def validate_names(schema: GraphQLSchema, validate):
147+
135148
# types and interfaces
136149
if validate.get('type_names'):
137150
# type names
@@ -173,6 +186,7 @@ def validate_names(schema: GraphQLSchema, validate):
173186

174187

175188
def transform_names(schema: GraphQLSchema, transform):
189+
176190
# types and interfaces
177191
if transform.get('type_names'):
178192
if transform.get('type_names') in string_transforms:
@@ -248,6 +262,17 @@ def drop_comments(schema):
248262
field.description = None
249263

250264

265+
def datetime_control(schema):
266+
type_names = set(schema.type_map.keys())
267+
if 'DateTime' in type_names:
268+
if not is_scalar_type(schema.type_map['DateTime']):
269+
raise Exception('DateTime exists but is not scalar type: ' + schema.type_map['DateTime'])
270+
else:
271+
schema.type_map['DateTime'] = GraphQLScalarType('DateTime')
272+
if not is_scalar_type(schema.type_map['DateTime']):
273+
raise Exception('DateTime could not be added as scalar!')
274+
275+
251276
if __name__ == '__main__':
252277
parser = argparse.ArgumentParser()
253278
parser.add_argument('--input', type=str, required=True,

graphql-api-generator/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ generation:
1212
add_mutation_type: true
1313
# add id field to all schema types
1414
field_for_id: true
15+
# add creation date and last update date field(s) to all schema types
16+
generate_datetime: false
17+
field_for_creation_date: true
18+
field_for_last_update_date: true
1519
# add reverse edges for traversal
1620
reverse_edges: true
1721
# add edge types

graphql-api-generator/test/all_tests.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,69 @@ def test_add_input_to_create_1(self):
5050
''')
5151
schema_out = generator.run(schema_in, config)
5252
assert compare.is_equals_schema(schema_out, expected)
53+
54+
def test_add_crationDate_1(self):
55+
schema_in = build_schema('''
56+
type Test
57+
''')
58+
config = {'generation': {'field_for_creation_date': True}}
59+
expected = build_schema('''
60+
scalar DateTime
61+
type Test {
62+
_creationDate: DateTime!
63+
}
64+
''')
65+
schema_out = generator.run(schema_in, config)
66+
assert compare.is_equals_schema(schema_out, expected)
67+
68+
def test_add_lastUpdateDate_1(self):
69+
schema_in = build_schema('''
70+
type Test
71+
''')
72+
config = {'generation': {'field_for_last_update_date': True}}
73+
expected = build_schema('''
74+
scalar DateTime
75+
type Test {
76+
_lastUpdateDate: DateTime
77+
}
78+
''')
79+
schema_out = generator.run(schema_in, config)
80+
assert compare.is_equals_schema(schema_out, expected)
81+
82+
def test_datetime_scalar_1(self):
83+
schema_in = build_schema('''
84+
type datetime_test
85+
''')
86+
config = {'generation': {'generate_datetime': True}}
87+
try:
88+
run(schema_in, config)
89+
assert True
90+
except:
91+
assert False
92+
93+
def test_datetime_scalar_2(self):
94+
schema_in = build_schema('''
95+
scalar DateTime
96+
type datetime_test { date: DateTime! }
97+
''')
98+
config = {'generation': {'generate_datetime': True}}
99+
try:
100+
generator.run(schema_in, config)
101+
assert True
102+
except:
103+
assert False
104+
105+
def test_datetime_scalar_3(self):
106+
schema_in = build_schema('''
107+
type DateTime {
108+
date: String!
109+
time: String!
110+
}
111+
type datetime_test { date: DateTime! }
112+
''')
113+
config = {'generation': {'generate_datetime': True}}
114+
try:
115+
generator.run(schema_in, config)
116+
assert False
117+
except:
118+
assert True

graphql-api-generator/utils/utils.py

Lines changed: 42 additions & 2 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: DateTime! }} '
113+
else:
114+
make += f'extend type {_type.name} {{ _creationDate: DateTime! }} '
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: DateTime }} '
130+
else:
131+
make += f'extend type {_type.name} {{ _lastUpdateDate: DateTime }} '
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.
@@ -526,8 +560,14 @@ def get_field_annotations(field: GraphQLField):
526560
return " ".join(annotation_fields)
527561

528562

529-
def add_edge_objects(schema: GraphQLSchema):
563+
def add_edge_objects(schema: GraphQLSchema, field_for_creation_date, field_for_last_update_date):
530564
make = ''
565+
creation_date_string = ''
566+
last_update_date_string = ''
567+
if field_for_creation_date:
568+
creation_date_string = '_creationDate: Date!'
569+
if field_for_last_update_date:
570+
last_update_date_string = '_lastUpdateDate: Date'
531571
for _type in schema.type_map.values():
532572
if not is_schema_defined_type(_type) or is_interface_type(_type):
533573
continue
@@ -539,7 +579,7 @@ def add_edge_objects(schema: GraphQLSchema):
539579
for t in connected_types:
540580
edge_from = f'{capitalize(field_name)}EdgeFrom{t.name}'
541581
annotations = get_field_annotations(field)
542-
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {annotations}}}\n'
582+
make += f'type _{edge_from} {{id:ID! source: {t.name}! target: {inner_field_type}! {creation_date_string} {last_update_date_string} {annotations}}}\n'
543583

544584
schema = add_to_schema(schema, make)
545585
return schema

0 commit comments

Comments
 (0)