Skip to content

Commit 265df0f

Browse files
author
Cameron Hurst
committed
Adding support for limiting array length and updated next/previous logic
1 parent 54fa427 commit 265df0f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Diff for: graphql_relay/connection/arrayconnection.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def connection_from_promised_list(data_promise, args=None, **kwargs):
2929

3030
def connection_from_list_slice(list_slice, args=None, connection_type=None,
3131
edge_type=None, pageinfo_type=None,
32-
slice_start=0, list_length=0, list_slice_length=None):
32+
slice_start=0, list_length=0, list_slice_length=None, limit=None):
3333
"""
3434
Given a slice (subset) of an array, returns a connection object for use in
3535
GraphQL.
@@ -75,11 +75,13 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
7575
end_offset - last
7676
)
7777

78+
_start = max(start_offset - slice_start, 0)
79+
_finish = list_slice_length - (slice_end - end_offset)
80+
if limit and _finish - _start > limit:
81+
_finish = _start + limit
82+
7883
# If supplied slice is too large, trim it down before mapping over it.
79-
_slice = list_slice[
80-
max(start_offset - slice_start, 0):
81-
list_slice_length - (slice_end - end_offset)
82-
]
84+
_slice = list_slice[_start:_finish]
8385
edges = [
8486
edge_type(
8587
node=node,
@@ -90,16 +92,14 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
9092

9193
first_edge_cursor = edges[0].cursor if edges else None
9294
last_edge_cursor = edges[-1].cursor if edges else None
93-
lower_bound = after_offset + 1 if after else 0
94-
upper_bound = before_offset if before else list_length
9595

9696
return connection_type(
9797
edges=edges,
9898
page_info=pageinfo_type(
9999
start_cursor=first_edge_cursor,
100100
end_cursor=last_edge_cursor,
101-
has_previous_page=isinstance(last, int) and start_offset > lower_bound,
102-
has_next_page=isinstance(first, int) and end_offset < upper_bound
101+
has_previous_page= _start > 0,
102+
has_next_page= _finish < list_slice_length
103103
)
104104
)
105105

0 commit comments

Comments
 (0)