Skip to content

Add additional sqlalchemy_utils types #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@
from .registry import get_global_registry
from .resolvers import get_attr_resolver, get_custom_resolver


class DummyImport:
def __getattr__(self, name):
return object


try:
import sqlalchemy_utils
except ImportError:
sqlalchemy_utils = DummyImport()

try:
from sqlalchemy_utils import ChoiceType, JSONType, ScalarListType, TSVectorType
from geoalchemy2.elements import Geometry
except ImportError:
ChoiceType = JSONType = ScalarListType = TSVectorType = object
Geometry = object


is_selectin_available = getattr(strategies, 'SelectInLoader', None)
Expand Down Expand Up @@ -183,7 +194,12 @@ def convert_sqlalchemy_type(type, column, registry=None):
@convert_sqlalchemy_type.register(postgresql.UUID)
@convert_sqlalchemy_type.register(postgresql.INET)
@convert_sqlalchemy_type.register(postgresql.CIDR)
@convert_sqlalchemy_type.register(TSVectorType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.TSVectorType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.UUIDType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.EmailType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.URLType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.IPAddressType)
@convert_sqlalchemy_type.register(Geometry)
def convert_column_to_string(type, column, registry=None):
return String

Expand Down Expand Up @@ -218,7 +234,7 @@ def convert_enum_to_enum(type, column, registry=None):


# TODO Make ChoiceType conversion consistent with other enums
@convert_sqlalchemy_type.register(ChoiceType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.ChoiceType)
def convert_choice_to_enum(type, column, registry=None):
name = "{}_{}".format(column.table.name, column.name).upper()
if isinstance(type.choices, EnumMeta):
Expand All @@ -229,7 +245,7 @@ def convert_choice_to_enum(type, column, registry=None):
return Enum(name, type.choices)


@convert_sqlalchemy_type.register(ScalarListType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.ScalarListType)
def convert_scalar_list_to_list(type, column, registry=None):
return List(String)

Expand All @@ -248,6 +264,6 @@ def convert_json_to_string(type, column, registry=None):
return JSONString


@convert_sqlalchemy_type.register(JSONType)
@convert_sqlalchemy_type.register(sqlalchemy_utils.JSONType)
def convert_json_type_to_string(type, column, registry=None):
return JSONString
8 changes: 7 additions & 1 deletion graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from graphene.types.datetime import DateTime
from graphene.types.json import JSONString

from ..converter import (convert_sqlalchemy_column,
from ..converter import (DummyImport, convert_sqlalchemy_column,
convert_sqlalchemy_composite,
convert_sqlalchemy_relationship)
from ..fields import (UnsortedSQLAlchemyConnectionField,
Expand Down Expand Up @@ -369,3 +369,9 @@ def __init__(self, col1, col2):
Registry(),
mock_resolver,
)


def test_dummy_import():
"""The dummy module returns 'object' for a query for any member"""
dummy_module = DummyImport()
assert dummy_module.foo == object