Fix: In src/graphql_relay/connection/array_connection.py's... - #51
Open
M001N wants to merge 1 commit into
Open
Conversation
connection_from_array_slice gated hasPreviousPage entirely on whether last was supplied and hasNextPage entirely on whether irst was supplied, ignoring the after/before cursors which are, per the Relay Cursor Connections Specification, an equally valid (and in this offset-based implementation, efficiently determinable) signal that a previous/next page exists. Now, when last is not given, hasPreviousPage is true whenever a valid �fter cursor (pointing at an existing element) was supplied. Symmetrically, when irst is not given, hasNextPage is true whenever a valid �efore cursor was supplied. This matches the algorithm in graphql-relay-js PR #400, which proposed the same fix upstream (still unmerged as of this writing). Updates existing test expectations that encoded the old, incorrect behavior, and adds regression tests for the two scenarios from the bug report: hasPreviousPage on a second page reached via first+after, and hasNextPage on a page reached via last+before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This exact fix was already implemented and committed on this branch (commit 5fa726a, present at task start): hasPreviousPage now falls back to
0 <= after_offset < array_length(a valid after cursor was given) whenlastis not supplied, instead of unconditionally False. Symmetrically hasNextPage falls back to0 <= before_offset < array_lengthwhenfirstis not supplied. I independently reviewed the code, confirmed it matches the Relay spec's intent and the graphql-relay-js reference behavior, and re-ran the full test suite to verify correctness -- no further code changes were needed.Problem
graphql-python/graphql-relay-py issue reference: graphql-python/graphql-relay-py (reported upstream against graphene#395, but the actual code lives here per the reporter's own correct diagnosis; verified independently on the real target repo)
Root Cause
In src/graphql_relay/connection/array_connection.py's connection_from_array_slice, hasPreviousPage was computed as
isinstance(last, int) and start_offset > lower_boundand hasNextPage asisinstance(first, int) and end_offset < upper_bound. These gated the flags entirely on whether the opposite-direction pagination argument (last/first) was supplied, ignoring the after/before cursors, which per the Relay Cursor Connections Spec are themselves sufficient evidence of a previous/next page.Testing
PASS - 50/50 tests in test_array_connection.py pass (including describe_has_previous_next_page_regression covering forward-pagination first+after and backward-pagination last+before cases plus the first-page/last-page False cases), and the full suite of 118 tests passes.
Related Issue
graphql-python/graphql-relay-py (reported upstream against graphene#395, but the actual code lives here per the reporter's own correct diagnosis; verified independently on the real target repo)