@@ -93,6 +93,14 @@ def set_non_null_many_relationships(non_null_flag):
93
93
use_non_null_many_relationships = non_null_flag
94
94
95
95
96
+ use_id_type_for_keys = True
97
+
98
+
99
+ def set_id_for_keys (id_flag ):
100
+ global use_id_type_for_keys
101
+ use_id_type_for_keys = id_flag
102
+
103
+
96
104
def get_column_doc (column ):
97
105
return getattr (column , "doc" , None )
98
106
@@ -259,18 +267,34 @@ def inner(fn):
259
267
convert_sqlalchemy_composite .register = _register_composite_class
260
268
261
269
270
+ def _is_primary_or_foreign_key (column ):
271
+ return getattr (column , "primary_key" , False ) or (
272
+ len (getattr (column , "foreign_keys" , [])) > 0
273
+ )
274
+
275
+
262
276
def convert_sqlalchemy_column (column_prop , registry , resolver , ** field_kwargs ):
263
277
column = column_prop .columns [0 ]
264
- # The converter expects a type to find the right conversion function.
265
- # If we get an instance instead, we need to convert it to a type.
266
- # The conversion function will still be able to access the instance via the column argument.
278
+ # We only use the converter if no type was specified using the ORMField
267
279
if "type_" not in field_kwargs :
268
- column_type = getattr (column , "type" , None )
269
- if not isinstance (column_type , type ):
270
- column_type = type (column_type )
280
+ # If the column is a primary key, we use the ID typ
281
+ if use_id_type_for_keys and _is_primary_or_foreign_key (column ):
282
+ field_type = graphene .ID
283
+ else :
284
+ # The converter expects a type to find the right conversion function.
285
+ # If we get an instance instead, we need to convert it to a type.
286
+ # The conversion function will still be able to access the instance via the column argument.
287
+ column_type = getattr (column , "type" , None )
288
+ if not isinstance (column_type , type ):
289
+ column_type = type (column_type )
290
+
291
+ field_type = convert_sqlalchemy_type (
292
+ column_type , column = column , registry = registry
293
+ )
294
+
271
295
field_kwargs .setdefault (
272
296
"type_" ,
273
- convert_sqlalchemy_type ( column_type , column = column , registry = registry ) ,
297
+ field_type ,
274
298
)
275
299
field_kwargs .setdefault ("required" , not is_column_nullable (column ))
276
300
field_kwargs .setdefault ("description" , get_column_doc (column ))
@@ -385,10 +409,6 @@ def convert_column_to_int_or_id(
385
409
registry : Registry = None ,
386
410
** kwargs ,
387
411
):
388
- # fixme drop the primary key processing from here in another pr
389
- if column is not None :
390
- if getattr (column , "primary_key" , False ) is True :
391
- return graphene .ID
392
412
return graphene .Int
393
413
394
414
0 commit comments