graphql-ruby icon indicating copy to clipboard operation
graphql-ruby copied to clipboard

Why is `endCursor` nilable if there's also a `hasNextPage` boolean?

Open amomchilov opened this issue 3 years ago • 2 comments
trafficstars

Not really an issue/bug, but I'm just trying to gather some context.

In my code, I had an assertion like so:

if page_info.fetch("hasNextPage") == page_info.fetch("endCursor").nil?
  raise "If 'hasNextPage' is true, then the cursor 'endCursor' must be non-nil!"
end

I figured that hasNextPage: true should imply endCusor: nil, and vice-versa, but that's not the case for ArrayConnection.

I'll just remove this assertion, but I'm curious: what's the purpose of hasNextPage? Couldn't clients imply there's no next page, simply from seeing the endCursor is nil?

amomchilov avatar Sep 20 '22 13:09 amomchilov

Well... went to look up the Relay server spec, and I found that these cursors are actually supposed to be null: false: https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo.Introspection

Maybe that answers the question: endCursor is always supposed to have a value, but hasNextPage will tell you whether or not to expect more items in the list.

I think GraphQL-Ruby's PageInfo type should be fixed, but I'll have to double-check the history on that; maybe there's some reason it's defined that way 🤔

rmosolgo avatar Sep 21 '22 14:09 rmosolgo

Well... went to look up the Relay server spec, and I found that these cursors are actually supposed to be null: false: https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo.Introspection

Maybe that answers the question: endCursor is always supposed to have a value, but hasNextPage will tell you whether or not to expect more items in the list.

I think it's actually a bug in the relay spec, as you discussed in this previous conversation:

https://github.com/rmosolgo/graphql-ruby/pull/2886#issuecomment-618414736

endCursor needs to be nilable for the case where the connection has an empty list of edges.

myronmarston avatar Sep 21 '22 21:09 myronmarston

Interesting find. Thanks for the context!

amomchilov avatar Sep 23 '22 16:09 amomchilov

Thanks for sharing that, @myronmarston -- I guess that answers it 🍻

rmosolgo avatar Sep 26 '22 12:09 rmosolgo