Skip to content

Commit 869a55b

Browse files
authored
Add support for N-Dimensional Arrays
Fixes #288
1 parent 771f4f5 commit 869a55b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

graphene_sqlalchemy/converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,15 @@ def convert_scalar_list_to_list(type, column, registry=None):
239239
return List(String)
240240

241241

242+
def init_array_list_recursive(inner_type, n):
243+
return inner_type if n == 0 else List(init_array_list_recursive(inner_type, n-1))
244+
245+
242246
@convert_sqlalchemy_type.register(types.ARRAY)
243247
@convert_sqlalchemy_type.register(postgresql.ARRAY)
244248
def convert_array_to_list(_type, column, registry=None):
245249
inner_type = convert_sqlalchemy_type(column.type.item_type, column)
246-
return List(inner_type)
250+
return List(init_array_list_recursive(inner_type, (column.type.dimensions or 1) - 1))
247251

248252

249253
@convert_sqlalchemy_type.register(postgresql.HSTORE)

graphene_sqlalchemy/tests/test_converter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ def test_should_array_convert():
324324
assert field.type.of_type == graphene.Int
325325

326326

327+
def test_should_2d_array_convert():
328+
field = get_field(types.ARRAY(types.Integer, dimensions=2))
329+
assert isinstance(field.type, graphene.List)
330+
assert isinstance(field.type.of_type, graphene.List)
331+
assert field.type.of_type.of_type == graphene.Int
332+
333+
334+
def test_should_3d_array_convert():
335+
field = get_field(types.ARRAY(types.Integer, dimensions=3))
336+
assert isinstance(field.type, graphene.List)
337+
assert isinstance(field.type.of_type, graphene.List)
338+
assert isinstance(field.type.of_type.of_type, graphene.List)
339+
assert field.type.of_type.of_type.of_type == graphene.Int
340+
341+
327342
def test_should_postgresql_json_convert():
328343
assert get_field(postgresql.JSON()).type == graphene.JSONString
329344

0 commit comments

Comments
 (0)