Skip to content

Commit 6f6c61c

Browse files
committed
Fallback to String / Implemented Enum support.
1 parent d83f7e1 commit 6f6c61c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

graphene_sqlalchemy/converter.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import datetime
2+
import enum
23
import typing
4+
import warnings
35
from decimal import Decimal
46
from functools import singledispatch
57
from typing import Any
@@ -18,7 +20,8 @@
1820
default_connection_field_factory)
1921
from .registry import get_global_registry
2022
from .resolvers import get_attr_resolver, get_custom_resolver
21-
from .utils import singledispatchbymatchfunction, value_equals
23+
from .utils import (singledispatchbymatchfunction, value_equals,
24+
value_is_subclass)
2225

2326
try:
2427
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, TSVectorType
@@ -271,7 +274,13 @@ def convert_hybrid_property_return_type_inner(arg: Any):
271274
existing_graphql_type = get_global_registry().get_type_for_model(arg)
272275
if existing_graphql_type:
273276
return existing_graphql_type
274-
raise Exception(f"I don't know how to generate a GraphQL type out of a \"{arg}\" type")
277+
278+
# No valid type found, warn and fall back to graphene.String
279+
warnings.warn(
280+
(f"I don't know how to generate a GraphQL type out of a \"{arg}\" type."
281+
"Falling back to \"graphene.String\"")
282+
)
283+
return String
275284

276285

277286
@convert_hybrid_property_return_type_inner.register(value_equals(str))
@@ -317,6 +326,11 @@ def convert_hybrid_property_return_type_inner_time(arg):
317326
return Time
318327

319328

329+
@convert_hybrid_property_return_type_inner.register(value_is_subclass(enum.Enum))
330+
def convert_hybrid_property_return_type_inner_enum(arg):
331+
return Enum.from_enum(arg)
332+
333+
320334
@convert_hybrid_property_return_type_inner.register(lambda x: getattr(x, '__origin__', None) in [list, typing.List])
321335
def convert_hybrid_property_return_type_inner_list(arg):
322336
# type is either list[T] or List[T], generic argument at __args__[0]

graphene_sqlalchemy/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,13 @@ def value_equals(value):
188188
"""A simple function that makes the equality based matcher functions for
189189
SingleDispatchByMatchFunction prettier"""
190190
return lambda x: x == value
191+
192+
193+
def value_is_subclass(cls):
194+
def safe_subclass_checker(o):
195+
try:
196+
return issubclass(o, cls)
197+
except TypeError:
198+
return False
199+
200+
return safe_subclass_checker

0 commit comments

Comments
 (0)