Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for limiting array length and updated next/previous logic #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions graphql_relay/connection/arrayconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def connection_from_promised_list(data_promise, args=None, **kwargs):

def connection_from_list_slice(list_slice, args=None, connection_type=None,
edge_type=None, pageinfo_type=None,
slice_start=0, list_length=0, list_slice_length=None):
slice_start=0, list_length=0, list_slice_length=None, limit=None):
"""
Given a slice (subset) of an array, returns a connection object for use in
GraphQL.
Expand Down Expand Up @@ -75,11 +75,13 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
end_offset - last
)

_start = max(start_offset - slice_start, 0)
_finish = list_slice_length - (slice_end - end_offset)
if limit and _finish - _start > limit:
_finish = _start + limit

# If supplied slice is too large, trim it down before mapping over it.
_slice = list_slice[
max(start_offset - slice_start, 0):
list_slice_length - (slice_end - end_offset)
]
_slice = list_slice[_start:_finish]
edges = [
edge_type(
node=node,
Expand All @@ -90,16 +92,14 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,

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

return connection_type(
edges=edges,
page_info=pageinfo_type(
start_cursor=first_edge_cursor,
end_cursor=last_edge_cursor,
has_previous_page=isinstance(last, int) and start_offset > lower_bound,
has_next_page=isinstance(first, int) and end_offset < upper_bound
has_previous_page= _start > 0,
has_next_page= _finish < list_slice_length
)
)

Expand Down