Add connection_from_list_slice support to Connection

This allows to return e.g. `Connection.for_node(Node).from_list([1, 2, 3, 4], args, context, info, 5389)` to get pagination support for sliced lists.
This commit is contained in:
Markus Padourek 2016-06-08 17:22:27 +01:00
parent 1711e6a529
commit c6f8c30c84

View File

@ -87,12 +87,17 @@ class Connection(ObjectType):
{'edge_type': edge_type, 'edges': edges}) {'edge_type': edge_type, 'edges': edges})
@classmethod @classmethod
def from_list(cls, iterable, args, context, info): def from_list(cls, iterable, args, context, info, total_count=None):
assert isinstance( assert isinstance(
iterable, Iterable), 'Resolved value from the connection field have to be iterable' iterable, Iterable), 'Resolved value from the connection field have to be iterable'
connection = connection_from_list(
list_slice_length = len(iterable)
list_length = total_count if total_count else list_slice_length
connection = connection_from_list_slice(
iterable, args, connection_type=cls, iterable, args, connection_type=cls,
edge_type=cls.edge_type, pageinfo_type=PageInfo) edge_type=cls.edge_type, pageinfo_type=PageInfo,
list_length=list_length, list_slice_length=list_slice_length)
connection.set_connection_data(iterable) connection.set_connection_data(iterable)
return connection return connection