4
4
from io import UnsupportedOperation
5
5
6
6
import yaml
7
- from utils .utils import *
7
+ from modules .utils import *
8
8
9
9
string_transforms = {
10
10
'uppercase' : uppercase ,
@@ -35,6 +35,7 @@ def cmd(args):
35
35
for file in files :
36
36
with open (file , 'r' ) as f :
37
37
schema_string += f .read () + '\n '
38
+
38
39
schema = build_schema (schema_string )
39
40
40
41
# run
@@ -49,10 +50,6 @@ def cmd(args):
49
50
50
51
51
52
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
-
56
53
# validate
57
54
if config .get ('validate' ):
58
55
validate_names (schema , config .get ('validate' ))
@@ -63,36 +60,45 @@ def run(schema: GraphQLSchema, config: dict):
63
60
64
61
# API generation
65
62
if config .get ('generation' ):
63
+ # check if DateTime exists or should be added
64
+ if config .get ('generation' ).get ('field_for_creation_date' ):
65
+ datetime_control (schema )
66
+
67
+ # add query type
66
68
if config .get ('generation' ).get ('add_query_type' ):
67
69
schema = add_query_type (schema )
70
+
71
+ # add mutation type
68
72
if config .get ('generation' ).get ('add_mutation_type' ):
69
73
schema = add_mutation_type (schema )
70
74
71
75
# add id
72
76
if config .get ('generation' ).get ('field_for_id' ):
73
77
schema = add_id_to_types (schema )
74
78
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
-
83
79
# add reverse edges for traversal
84
80
if config .get ('generation' ).get ('reverse_edges' ):
85
81
schema = add_reverse_edges (schema )
86
82
87
83
# add edge types
88
84
if config .get ('generation' ).get ('edge_types' ) or config .get ('generation' ).get ('create_edge_objects' ):
89
- schema = add_edge_objects (schema , config . get ( 'generation' ). get ( 'field_for_creation_date' ), config . get ( 'generation' ). get ( 'field_for_last_update_date' ) )
85
+ schema = add_edge_objects (schema )
90
86
if config .get ('generation' ).get ('fields_for_edge_types' ):
91
87
raise UnsupportedOperation ('{0} is currently not supported' .format ('fields_for_edge_types' ))
92
88
89
+ # add creation date
90
+ if config .get ('generation' ).get ('field_for_creation_date' ):
91
+ schema = add_creation_date_to_types (schema )
92
+
93
+ # add last update date
94
+ if config .get ('generation' ).get ('field_for_last_update_date' ):
95
+ schema = add_last_update_date_to_types (schema )
96
+
93
97
# add queries
94
98
if config .get ('generation' ).get ('query_by_id' ):
95
99
schema = add_get_queries (schema )
100
+
101
+ # add filters
96
102
if config .get ('generation' ).get ('query_type_filter' ) or config .get ('generation' ).get ('query_list_of' ):
97
103
schema = add_enum_filters (schema )
98
104
schema = add_scalar_filters (schema )
@@ -144,7 +150,6 @@ def run(schema: GraphQLSchema, config: dict):
144
150
145
151
146
152
def validate_names (schema : GraphQLSchema , validate ):
147
-
148
153
# types and interfaces
149
154
if validate .get ('type_names' ):
150
155
# type names
@@ -186,7 +191,6 @@ def validate_names(schema: GraphQLSchema, validate):
186
191
187
192
188
193
def transform_names (schema : GraphQLSchema , transform ):
189
-
190
194
# types and interfaces
191
195
if transform .get ('type_names' ):
192
196
if transform .get ('type_names' ) in string_transforms :
@@ -268,7 +272,7 @@ def datetime_control(schema):
268
272
if not is_scalar_type (schema .type_map ['DateTime' ]):
269
273
raise Exception ('DateTime exists but is not scalar type: ' + schema .type_map ['DateTime' ])
270
274
else :
271
- schema .type_map ['DateTime' ] = GraphQLScalarType ('DateTime' )
275
+ schema .type_map ['DateTime' ] = GraphQLScalarType ('DateTime' , serialize = lambda x : str ( x ) )
272
276
if not is_scalar_type (schema .type_map ['DateTime' ]):
273
277
raise Exception ('DateTime could not be added as scalar!' )
274
278
@@ -278,10 +282,9 @@ def datetime_control(schema):
278
282
parser .add_argument ('--input' , type = str , required = True ,
279
283
help = 'GraphQL DB schema files (separated by commas), or a path to a schema directory' )
280
284
parser .add_argument ('--output' , type = str ,
281
- help = 'Output schema file (default stdout )' )
285
+ help = 'Output schema file (optional )' )
282
286
parser .add_argument ('--config' , type = str ,
283
287
help = 'Path to configuration file' )
284
-
285
288
cmd (parser .parse_args ())
286
289
287
290
0 commit comments