Skip to content

Commit

Permalink
Improve message when the cursor in pagination is invalid (saleor#8594)
Browse files Browse the repository at this point in the history
  • Loading branch information
IKarbowiak authored Nov 22, 2021
1 parent 71c51c4 commit 3a447b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion saleor/graphql/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ def connection_from_queryset_slice(

requested_count = first or last
end_margin = requested_count + 1 if requested_count else None

cursor = after or before
cursor = from_global_cursor(cursor) if cursor else None
try:
cursor = from_global_cursor(cursor) if cursor else None
except ValueError:
raise GraphQLError("Received cursor is invalid.")

sort_by = args.get("sort_by", {})
sorting_fields = _get_sorting_fields(sort_by, qs)
Expand Down
11 changes: 11 additions & 0 deletions saleor/graphql/core/tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,14 @@ def test_pagination_backward_last_page_info(books):
page_info = content["books"]["pageInfo"]
assert page_info["hasNextPage"]
assert page_info["hasPreviousPage"] is False


def test_pagination_invalid_cursor(books):
cursor = graphene.Node.to_global_id("BookType", -1)
variables = {"first": 5, "after": cursor}

result = schema.execute(QUERY_PAGINATION_TEST, variables=variables)

assert result.errors
assert len(result.errors) == 1
assert str(result.errors[0]) == "Received cursor is invalid."

0 comments on commit 3a447b7

Please sign in to comment.