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

Connections no longer return all items when the "before" argument is the first cursor #4012

Merged
merged 2 commits into from
Mar 31, 2022

Conversation

d-arrington
Copy link

@d-arrington d-arrington commented Mar 29, 2022

Problem

Consider the following request and response:

query {
  animals (first: 5, before: null) {
    edges {
      cursor
    }
  }
}
{
  "data": {
    "animals": {
      "edges": [
        { "cursor": "1" },
        { "cursor": "2" },
        { "cursor": "3" },
        { "cursor": "4" },
        { "cursor": "5" }
      ]
    }
  }
}

What if we pass in a before cursor that's equal to the first item? We get this:

query {
  animals (first: 5, before: "1") {
    edges {
      cursor
    }
  }
}
{
  "data": {
    "animals": {
      "edges": [
        { "cursor": "1" },
        { "cursor": "2" },
        { "cursor": "3" },
        { "cursor": "4" },
        { "cursor": "5" }
      ]
    }
  }
}

That's not correct. Those edges are not before the first edge. The actual response should be this:

{
  "data": {
    "animals": {
      "edges": []
    }
  }
}

Cause

The calculation for the range of nodes that should be loaded is items[0..index_from_cursor(before)-2] || []. So if the before cursor is the first cursor, then the node range becomes [0..-1], encompassing all items in the list.

@d-arrington d-arrington changed the title Fixed an edge case (get it?) on connections where the "before" option was functioning incorrectly. Connections no longer return all items when the "before" argument is the first cursor. Mar 29, 2022
@d-arrington d-arrington changed the title Connections no longer return all items when the "before" argument is the first cursor. Connections no longer return all items when the "before" argument is the first cursor Mar 29, 2022
@rmosolgo rmosolgo added this to the 2.0.6 milestone Mar 30, 2022
@rmosolgo
Copy link
Owner

Thanks for the fix on this! It looks like an existing test or two was broken, do you mind looking into it?

@d-arrington
Copy link
Author

And that's why we have tests! It was a silly typo, but it's been fixed.

@rmosolgo
Copy link
Owner

✨ Thanks!

@rmosolgo rmosolgo merged commit 0518113 into rmosolgo:master Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants