Skip to content

Commit 6897f31

Browse files
author
Cameron Hurst
committed
Adding support for limiting array length and updated next/previous logic
1 parent 17ce2ef commit 6897f31

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
@@ -31,7 +31,7 @@ def connection_from_promised_list(data_promise, args=None, **kwargs):
3131

3232
def connection_from_list_slice(list_slice, args=None, connection_type=None,
3333
edge_type=None, pageinfo_type=None,
34-
slice_start=0, list_length=0, list_slice_length=None):
34+
slice_start=0, list_length=0, list_slice_length=None, limit=None):
3535
'''
3636
Given a slice (subset) of an array, returns a connection object for use in
3737
GraphQL.
@@ -77,11 +77,13 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
7777
end_offset - last
7878
)
7979

80+
_start = max(start_offset - slice_start, 0)
81+
_finish = list_slice_length - (slice_end - end_offset)
82+
if limit and _finish - _start > limit:
83+
_finish = _start + limit
84+
8085
# If supplied slice is too large, trim it down before mapping over it.
81-
_slice = list_slice[
82-
max(start_offset - slice_start, 0):
83-
list_slice_length - (slice_end - end_offset)
84-
]
86+
_slice = list_slice[_start:_finish]
8587
edges = [
8688
edge_type(
8789
node=node,
@@ -93,16 +95,14 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
9395

9496
first_edge_cursor = edges[0].cursor if edges else None
9597
last_edge_cursor = edges[-1].cursor if edges else None
96-
lower_bound = after_offset + 1 if after else 0
97-
upper_bound = before_offset if before else list_length
9898

9999
return connection_type(
100100
edges=edges,
101101
page_info=pageinfo_type(
102102
start_cursor=first_edge_cursor,
103103
end_cursor=last_edge_cursor,
104-
has_previous_page=isinstance(last, int) and start_offset > lower_bound,
105-
has_next_page=isinstance(first, int) and end_offset < upper_bound
104+
has_previous_page= _start > 0,
105+
has_next_page= _finish < list_slice_length
106106
)
107107
)
108108

0 commit comments

Comments
 (0)