Skip to content

Commit

Permalink
Add support for N-Dimensional Arrays
Browse files Browse the repository at this point in the history
Fixes #288
  • Loading branch information
jbeard4 authored Apr 28, 2022
1 parent 771f4f5 commit 869a55b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion graphene_sqlalchemy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,15 @@ def convert_scalar_list_to_list(type, column, registry=None):
return List(String)


def init_array_list_recursive(inner_type, n):
return inner_type if n == 0 else List(init_array_list_recursive(inner_type, n-1))


@convert_sqlalchemy_type.register(types.ARRAY)
@convert_sqlalchemy_type.register(postgresql.ARRAY)
def convert_array_to_list(_type, column, registry=None):
inner_type = convert_sqlalchemy_type(column.type.item_type, column)
return List(inner_type)
return List(init_array_list_recursive(inner_type, (column.type.dimensions or 1) - 1))


@convert_sqlalchemy_type.register(postgresql.HSTORE)
Expand Down
15 changes: 15 additions & 0 deletions graphene_sqlalchemy/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ def test_should_array_convert():
assert field.type.of_type == graphene.Int


def test_should_2d_array_convert():
field = get_field(types.ARRAY(types.Integer, dimensions=2))
assert isinstance(field.type, graphene.List)
assert isinstance(field.type.of_type, graphene.List)
assert field.type.of_type.of_type == graphene.Int


def test_should_3d_array_convert():
field = get_field(types.ARRAY(types.Integer, dimensions=3))
assert isinstance(field.type, graphene.List)
assert isinstance(field.type.of_type, graphene.List)
assert isinstance(field.type.of_type.of_type, graphene.List)
assert field.type.of_type.of_type.of_type == graphene.Int


def test_should_postgresql_json_convert():
assert get_field(postgresql.JSON()).type == graphene.JSONString

Expand Down

0 comments on commit 869a55b

Please sign in to comment.