Skip to content
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
30 changes: 21 additions & 9 deletions graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
class ChoiceType(object):
pass


class ScalarListType(object):
pass


class JSONType(object):
pass

Expand All @@ -34,7 +36,7 @@ def dynamic_type():
if (direction == interfaces.MANYTOONE or not relationship.uselist):
return Field(_type)
elif (direction == interfaces.ONETOMANY or
direction == interfaces.MANYTOMANY):
direction == interfaces.MANYTOMANY):
if is_node(_type):
return SQLAlchemyConnectionField(_type)
return Field(List(_type))
Expand Down Expand Up @@ -64,8 +66,10 @@ def _register_composite_class(cls, registry=None):

def inner(fn):
registry.register_composite_converter(cls, fn)

return inner


convert_sqlalchemy_composite.register = _register_composite_class


Expand All @@ -91,28 +95,29 @@ def convert_sqlalchemy_type(type, column, registry=None):
@convert_sqlalchemy_type.register(postgresql.UUID)
def convert_column_to_string(type, column, registry=None):
return String(description=getattr(column, 'doc', None),
required=not(getattr(column, 'nullable', True)))
required=not (getattr(column, 'nullable', True)))


@convert_sqlalchemy_type.register(types.SmallInteger)
@convert_sqlalchemy_type.register(types.BigInteger)
@convert_sqlalchemy_type.register(types.Integer)
def convert_column_to_int_or_id(type, column, registry=None):
if column.primary_key:
return ID(description=column.doc, required=not(column.nullable))
return ID(description=column.doc, required=not (column.nullable))
else:
return Int(description=column.doc, required=not(column.nullable))
return Int(description=getattr(column, 'doc', None),
required=not (getattr(column, 'nullable', True)))


@convert_sqlalchemy_type.register(types.Boolean)
def convert_column_to_boolean(type, column, registry=None):
return Boolean(description=column.doc, required=not(column.nullable))
return Boolean(description=column.doc, required=not (column.nullable))


@convert_sqlalchemy_type.register(types.Float)
@convert_sqlalchemy_type.register(types.Numeric)
def convert_column_to_float(type, column, registry=None):
return Float(description=column.doc, required=not(column.nullable))
return Float(description=column.doc, required=not (column.nullable))


@convert_sqlalchemy_type.register(ChoiceType)
Expand All @@ -129,16 +134,23 @@ def convert_scalar_list_to_list(type, column, registry=None):
@convert_sqlalchemy_type.register(postgresql.ARRAY)
def convert_postgres_array_to_list(type, column, registry=None):
graphene_type = convert_sqlalchemy_type(column.type.item_type, column)
return List(graphene_type, description=column.doc, required=not(column.nullable))
return List(graphene_type, description=column.doc, required=not (column.nullable))


@convert_sqlalchemy_type.register(postgresql.HSTORE)
@convert_sqlalchemy_type.register(postgresql.JSON)
@convert_sqlalchemy_type.register(postgresql.JSONB)
def convert_json_to_string(type, column, registry=None):
return JSONString(description=column.doc, required=not(column.nullable))
return JSONString(description=column.doc, required=not (column.nullable))


@convert_sqlalchemy_type.register(postgresql.TSRANGE)
@convert_sqlalchemy_type.register(postgresql.TSTZRANGE)
def convert_posgres_range_to_string(type, column, registry=None):
graphene_type = convert_sqlalchemy_type(column.type.item_type, column)
return List(graphene_type, description=column.doc, required=not (column.nullable))


@convert_sqlalchemy_type.register(JSONType)
def convert_json_type_to_string(type, column, registry=None):
return JSONString(description=column.doc, required=not(column.nullable))
return JSONString(description=column.doc, required=not (column.nullable))
5 changes: 5 additions & 0 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def test_should_label_convert_string():
assert isinstance(graphene_type, graphene.String)


def test_should_label_convert_int():
label = Label('int_label_test', case([], else_="foo"), type_=types.Integer())
graphene_type = convert_sqlalchemy_column(label)
assert isinstance(graphene_type, graphene.Int)

def test_should_choice_convert_enum():
TYPES = [
(u'es', u'Spanish'),
Expand Down