24
24
safe_isinstance ,
25
25
singledispatchbymatchfunction ,
26
26
value_equals ,
27
- value_equals_strict ,
28
27
)
29
28
30
29
# We just use MapperProperties for type hints, they don't exist in sqlalchemy < 1.4
@@ -214,12 +213,15 @@ def inner(fn):
214
213
215
214
def convert_sqlalchemy_column (column_prop , registry , resolver , ** field_kwargs ):
216
215
column = column_prop .columns [0 ]
217
-
216
+ column_type = getattr (column , "type" , None )
217
+ # The converter expects a type to find the right conversion function.
218
+ # If we get an instance instead, we need to convert it to a type.
219
+ # The conversion function will still be able to access the instance via the column argument.
220
+ if not isinstance (column_type , type ):
221
+ column_type = type (column_type )
218
222
field_kwargs .setdefault (
219
223
"type_" ,
220
- convert_sqlalchemy_type (
221
- getattr (column , "type" , None ), column = column , registry = registry
222
- ),
224
+ convert_sqlalchemy_type (column_type , column = column , registry = registry ),
223
225
)
224
226
field_kwargs .setdefault ("required" , not is_column_nullable (column ))
225
227
field_kwargs .setdefault ("description" , get_column_doc (column ))
@@ -251,7 +253,7 @@ def convert_sqlalchemy_type(
251
253
)
252
254
253
255
254
- @convert_sqlalchemy_type .register (value_equals_strict (str ))
256
+ @convert_sqlalchemy_type .register (value_equals (str ))
255
257
@convert_sqlalchemy_type .register (value_equals (sqa_types .String ))
256
258
@convert_sqlalchemy_type .register (value_equals (sqa_types .Text ))
257
259
@convert_sqlalchemy_type .register (value_equals (sqa_types .Unicode ))
@@ -305,7 +307,7 @@ def convert_column_to_date(
305
307
306
308
@convert_sqlalchemy_type .register (value_equals (sqa_types .SmallInteger ))
307
309
@convert_sqlalchemy_type .register (value_equals (sqa_types .Integer ))
308
- @convert_sqlalchemy_type .register (value_equals_strict (int ))
310
+ @convert_sqlalchemy_type .register (value_equals (int ))
309
311
def convert_column_to_int_or_id (
310
312
type_arg : Any ,
311
313
column : Optional [Union [MapperProperty , hybrid_property ]] = None ,
@@ -318,15 +320,15 @@ def convert_column_to_int_or_id(
318
320
319
321
320
322
@convert_sqlalchemy_type .register (value_equals (sqa_types .Boolean ))
321
- @convert_sqlalchemy_type .register (value_equals_strict (bool ))
323
+ @convert_sqlalchemy_type .register (value_equals (bool ))
322
324
def convert_column_to_boolean (
323
325
type_arg : Any ,
324
326
** kwargs ,
325
327
):
326
328
return graphene .Boolean
327
329
328
330
329
- @convert_sqlalchemy_type .register (value_equals_strict (float ))
331
+ @convert_sqlalchemy_type .register (value_equals (float ))
330
332
@convert_sqlalchemy_type .register (value_equals (sqa_types .Float ))
331
333
@convert_sqlalchemy_type .register (value_equals (sqa_types .Numeric ))
332
334
@convert_sqlalchemy_type .register (value_equals (sqa_types .BigInteger ))
@@ -345,23 +347,29 @@ def convert_enum_to_enum(
345
347
registry : Registry = None ,
346
348
** kwargs ,
347
349
):
348
- return lambda : enum_for_sa_enum (type_arg , registry or get_global_registry ())
350
+ if column is None or isinstance (column , hybrid_property ):
351
+ raise Exception ("SQL-Enum conversion requires a column" )
352
+
353
+ return lambda : enum_for_sa_enum (column .type , registry or get_global_registry ())
349
354
350
355
351
356
# TODO Make ChoiceType conversion consistent with other enums
352
357
@convert_sqlalchemy_type .register (value_equals (sqa_utils .ChoiceType ))
353
358
def convert_choice_to_enum (
354
359
type_arg : sqa_utils .ChoiceType ,
355
360
column : Optional [Union [MapperProperty , hybrid_property ]] = None ,
356
- registry : Registry = None ,
361
+ ** kwargs ,
357
362
):
363
+ if column is None or isinstance (column , hybrid_property ):
364
+ raise Exception ("ChoiceType conversion requires a column" )
365
+
358
366
name = "{}_{}" .format (column .table .name , column .key ).upper ()
359
- if isinstance (type_arg .type_impl , EnumTypeImpl ):
367
+ if isinstance (column . type .type_impl , EnumTypeImpl ):
360
368
# type.choices may be Enum/IntEnum, in ChoiceType both presented as EnumMeta
361
369
# do not use from_enum here because we can have more than one enum column in table
362
- return graphene .Enum (name , list ((v .name , v .value ) for v in type_arg .choices ))
370
+ return graphene .Enum (name , list ((v .name , v .value ) for v in column . type .choices ))
363
371
else :
364
- return graphene .Enum (name , type_arg .choices )
372
+ return graphene .Enum (name , column . type .choices )
365
373
366
374
367
375
@convert_sqlalchemy_type .register (value_equals (sqa_utils .ScalarListType ))
@@ -388,8 +396,13 @@ def convert_array_to_list(
388
396
registry : Registry = None ,
389
397
** kwargs ,
390
398
):
399
+ if column is None or isinstance (column , hybrid_property ):
400
+ raise Exception ("SQL-Array conversion requires a column" )
401
+ item_type = column .type .item_type
402
+ if not isinstance (item_type , type ):
403
+ item_type = type (item_type )
391
404
inner_type = convert_sqlalchemy_type (
392
- column . type . item_type , column = column , registry = registry , ** kwargs
405
+ item_type , column = column , registry = registry , ** kwargs
393
406
)
394
407
return graphene .List (
395
408
init_array_list_recursive (inner_type , (column .type .dimensions or 1 ) - 1 )
@@ -422,12 +435,18 @@ def convert_variant_to_impl_type(
422
435
registry : Registry = None ,
423
436
** kwargs ,
424
437
):
438
+ if column is None or isinstance (column , hybrid_property ):
439
+ raise Exception ("Vaiant conversion requires a column" )
440
+
441
+ type_impl = column .type .impl
442
+ if not isinstance (type_impl , type ):
443
+ type_impl = type (type_impl )
425
444
return convert_sqlalchemy_type (
426
- type_arg . impl , column = column , registry = registry , ** kwargs
445
+ type_impl , column = column , registry = registry , ** kwargs
427
446
)
428
447
429
448
430
- @convert_sqlalchemy_type .register (value_equals_strict (Decimal ))
449
+ @convert_sqlalchemy_type .register (value_equals (Decimal ))
431
450
def convert_sqlalchemy_hybrid_property_type_decimal (type_arg : Any , ** kwargs ):
432
451
# The reason Decimal should be serialized as a String is because this is a
433
452
# base10 type used in things like money, and string allows it to not
0 commit comments