@@ -29,7 +29,7 @@ def connection_from_promised_list(data_promise, args=None, **kwargs):
29
29
30
30
def connection_from_list_slice (list_slice , args = None , connection_type = None ,
31
31
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 ):
33
33
"""
34
34
Given a slice (subset) of an array, returns a connection object for use in
35
35
GraphQL.
@@ -75,11 +75,13 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
75
75
end_offset - last
76
76
)
77
77
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
+
78
83
# 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 ]
83
85
edges = [
84
86
edge_type (
85
87
node = node ,
@@ -90,16 +92,14 @@ def connection_from_list_slice(list_slice, args=None, connection_type=None,
90
92
91
93
first_edge_cursor = edges [0 ].cursor if edges else None
92
94
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
95
95
96
96
return connection_type (
97
97
edges = edges ,
98
98
page_info = pageinfo_type (
99
99
start_cursor = first_edge_cursor ,
100
100
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
103
103
)
104
104
)
105
105
0 commit comments