@@ -49,6 +49,10 @@ def cmd(args):
49
49
50
50
51
51
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
+
52
56
# validate
53
57
if config .get ('validate' ):
54
58
validate_names (schema , config .get ('validate' ))
@@ -140,6 +144,17 @@ def run(schema: GraphQLSchema, config: dict):
140
144
141
145
142
146
def validate_names (schema : GraphQLSchema , validate ):
147
+ # scalars
148
+ if validate .get ('scalar_names' ):
149
+ # type names
150
+ f = string_transforms .get (validate .get ('scalar_names' ))
151
+ if f is None :
152
+ raise Exception ('Unrecognized option: ' + validate .get ('type_names' ))
153
+ for type_name , _type in schema .type_map .items ():
154
+ if is_sclara_type (_type ):
155
+ if f (type_name ) != type_name :
156
+ raise Exception (f'Scalar "{ type_name } " does not follow { validate .get ("scalar_names" )} ' )
157
+
143
158
# types and interfaces
144
159
if validate .get ('type_names' ):
145
160
# type names
@@ -181,6 +196,13 @@ def validate_names(schema: GraphQLSchema, validate):
181
196
182
197
183
198
def transform_names (schema : GraphQLSchema , transform ):
199
+ # scalar
200
+ if transform .get ('scalar_names' ):
201
+ if transform .get ('scalar_names' ) in string_transforms :
202
+ transform_scalars (schema , string_transforms [transform .get ('scalar_names' )])
203
+ else :
204
+ raise Exception ('Unsupported scalar name transform: ' + transform .get ('scalar_names' ))
205
+
184
206
# types and interfaces
185
207
if transform .get ('type_names' ):
186
208
if transform .get ('type_names' ) in string_transforms :
@@ -207,6 +229,16 @@ def transform_names(schema: GraphQLSchema, transform):
207
229
drop_comments (schema )
208
230
209
231
232
+ def transform_scalars (schema , transform ):
233
+ type_names = set (schema .type_map .keys ())
234
+ for type_name in type_names :
235
+ _type = schema .type_map [type_name ]
236
+ if is_scalar_type (_type ):
237
+ schema .type_map .pop (type_name )
238
+ _type .name = transform (type_name )
239
+ schema .type_map [_type .name ] = _type
240
+
241
+
210
242
def transform_types (schema , transform ):
211
243
type_names = set (schema .type_map .keys ())
212
244
for type_name in type_names :
@@ -256,6 +288,17 @@ def drop_comments(schema):
256
288
field .description = None
257
289
258
290
291
+ def datetime_control (schema ):
292
+ type_names = set (schema .type_map .keys ())
293
+ if 'DateTime' in type_names :
294
+ if not is_scalar_type (schema .type_map ['DateTime' ]):
295
+ raise Exception ('DateTime exists but is not scalar type: ' + schema .type_map ['DateTime' ])
296
+ else :
297
+ schema .type_map ['DateTime' ] = GraphQLScalarType ('DateTime' )
298
+ if not is_scalar_type (schema .type_map ['DateTime' ]):
299
+ raise Exception ('DateTime could not be added as scalar!' )
300
+
301
+
259
302
if __name__ == '__main__' :
260
303
parser = argparse .ArgumentParser ()
261
304
parser .add_argument ('--input' , type = str , required = True ,
0 commit comments