From 6cb37316a4851309b7bbc72a30259d498f1e5c95 Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Wed, 22 Nov 2017 15:22:31 -0500 Subject: [PATCH 1/2] Fix pagination indicators when using list slices According to the spec, if hasNextPage and hasPreviousPage should be set regardless of the pagination direction if they can be efficiently computed. --- graphql_relay/connection/arrayconnection.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/graphql_relay/connection/arrayconnection.py b/graphql_relay/connection/arrayconnection.py index ea951f5..cc648b5 100644 --- a/graphql_relay/connection/arrayconnection.py +++ b/graphql_relay/connection/arrayconnection.py @@ -90,16 +90,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_offset > 0, + has_next_page=end_offset < list_length ) ) From a2df9a87f3b20d16db50795e99ae3dcc51583329 Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Wed, 22 Nov 2017 15:38:25 -0500 Subject: [PATCH 2/2] Fix tests for pagination markers --- .../connection/tests/test_arrayconnection.py | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/graphql_relay/connection/tests/test_arrayconnection.py b/graphql_relay/connection/tests/test_arrayconnection.py index 810558d..2d1ad54 100644 --- a/graphql_relay/connection/tests/test_arrayconnection.py +++ b/graphql_relay/connection/tests/test_arrayconnection.py @@ -179,7 +179,7 @@ def test_pagination_respects_first_after(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -207,7 +207,7 @@ def test_pagination_respects_longfirst_after(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjQ=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': False, } } @@ -231,7 +231,7 @@ def test_pagination_respects_last_before(): 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'hasPreviousPage': True, - 'hasNextPage': False, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -259,7 +259,7 @@ def test_pagination_respects_longlast_before(): 'startCursor': 'YXJyYXljb25uZWN0aW9uOjA=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'hasPreviousPage': False, - 'hasNextPage': False, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -283,7 +283,7 @@ def test_first_after_before_few(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -312,8 +312,8 @@ def test_first_after_before_many(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, - 'hasNextPage': False, + 'hasPreviousPage': True, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -341,8 +341,8 @@ def test_first_after_before_exact(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, - 'hasNextPage': False, + 'hasPreviousPage': True, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -367,7 +367,7 @@ def test_last_after_before_few(): 'startCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', 'hasPreviousPage': True, - 'hasNextPage': False, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -395,8 +395,8 @@ def test_last_after_before_many(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, - 'hasNextPage': False, + 'hasPreviousPage': True, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -424,8 +424,8 @@ def test_last_after_before_exact(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, - 'hasNextPage': False, + 'hasPreviousPage': True, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -528,8 +528,8 @@ def test_no_elements_cursors_cross(): 'pageInfo': { 'startCursor': None, 'endCursor': None, - 'hasPreviousPage': False, - 'hasNextPage': False, + 'hasPreviousPage': True, + 'hasNextPage': True, } } assert c.to_dict() == expected @@ -627,7 +627,7 @@ def test_list_slice_works_with_a_just_right_array_slice(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -658,7 +658,7 @@ def test_list_slice_works_with_an_oversized_array_slice_left_side(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjE=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -685,7 +685,7 @@ def test_list_slice_works_with_an_oversized_array_slice_right_side(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -712,7 +712,7 @@ def test_list_slice_works_with_an_oversized_array_slice_both_sides(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjI=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -743,7 +743,7 @@ def test_list_slice_works_with_an_undersized_array_slice_left_side(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjM=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjQ=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': False, } } @@ -774,7 +774,7 @@ def test_list_slice_works_with_an_undersized_array_slice_right_side(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjI=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } } @@ -801,7 +801,7 @@ def test_list_slice_works_with_an_undersized_array_slice_both_sides(): 'pageInfo': { 'startCursor': 'YXJyYXljb25uZWN0aW9uOjM=', 'endCursor': 'YXJyYXljb25uZWN0aW9uOjM=', - 'hasPreviousPage': False, + 'hasPreviousPage': True, 'hasNextPage': True, } }