Skip to content

Commit 7978c6a

Browse files
committed
Add additional sqlalchemy_utils types
Add most trivial string types from sqlalchemy_utils: UUIDType, EmailType, URLType, and IPAddressType
1 parent 421f8e4 commit 7978c6a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

graphene_sqlalchemy/converter.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
from .registry import get_global_registry
1717
from .resolvers import get_attr_resolver, get_custom_resolver
1818

19+
20+
class DummyImport:
21+
def __getattr__(self, name):
22+
return object
23+
24+
1925
try:
20-
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, TSVectorType
26+
import sqlalchemy_utils
2127
except ImportError:
22-
ChoiceType = JSONType = ScalarListType = TSVectorType = object
28+
sqlalchemy_utils = DummyImport()
2329

2430

2531
is_selectin_available = getattr(strategies, 'SelectInLoader', None)
@@ -183,7 +189,11 @@ def convert_sqlalchemy_type(type, column, registry=None):
183189
@convert_sqlalchemy_type.register(postgresql.UUID)
184190
@convert_sqlalchemy_type.register(postgresql.INET)
185191
@convert_sqlalchemy_type.register(postgresql.CIDR)
186-
@convert_sqlalchemy_type.register(TSVectorType)
192+
@convert_sqlalchemy_type.register(sqlalchemy_utils.TSVectorType)
193+
@convert_sqlalchemy_type.register(sqlalchemy_utils.UUIDType)
194+
@convert_sqlalchemy_type.register(sqlalchemy_utils.EmailType)
195+
@convert_sqlalchemy_type.register(sqlalchemy_utils.URLType)
196+
@convert_sqlalchemy_type.register(sqlalchemy_utils.IPAddressType)
187197
def convert_column_to_string(type, column, registry=None):
188198
return String
189199

@@ -218,7 +228,7 @@ def convert_enum_to_enum(type, column, registry=None):
218228

219229

220230
# TODO Make ChoiceType conversion consistent with other enums
221-
@convert_sqlalchemy_type.register(ChoiceType)
231+
@convert_sqlalchemy_type.register(sqlalchemy_utils.ChoiceType)
222232
def convert_choice_to_enum(type, column, registry=None):
223233
name = "{}_{}".format(column.table.name, column.name).upper()
224234
if isinstance(type.choices, EnumMeta):
@@ -229,7 +239,7 @@ def convert_choice_to_enum(type, column, registry=None):
229239
return Enum(name, type.choices)
230240

231241

232-
@convert_sqlalchemy_type.register(ScalarListType)
242+
@convert_sqlalchemy_type.register(sqlalchemy_utils.ScalarListType)
233243
def convert_scalar_list_to_list(type, column, registry=None):
234244
return List(String)
235245

@@ -248,6 +258,6 @@ def convert_json_to_string(type, column, registry=None):
248258
return JSONString
249259

250260

251-
@convert_sqlalchemy_type.register(JSONType)
261+
@convert_sqlalchemy_type.register(sqlalchemy_utils.JSONType)
252262
def convert_json_type_to_string(type, column, registry=None):
253263
return JSONString

graphene_sqlalchemy/tests/test_converter.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from graphene.types.datetime import DateTime
1414
from graphene.types.json import JSONString
1515

16-
from ..converter import (convert_sqlalchemy_column,
16+
from ..converter import (DummyImport, convert_sqlalchemy_column,
1717
convert_sqlalchemy_composite,
1818
convert_sqlalchemy_relationship)
1919
from ..fields import (UnsortedSQLAlchemyConnectionField,
@@ -369,3 +369,9 @@ def __init__(self, col1, col2):
369369
Registry(),
370370
mock_resolver,
371371
)
372+
373+
374+
def test_dummy_import():
375+
"""The dummy module returns 'object' for a query for any member"""
376+
dummy_module = DummyImport()
377+
assert dummy_module.foo == object

0 commit comments

Comments
 (0)