graphqlpaginationrelay

Correct value for `startCursor` and `endCursor` in `PageInfo` when there are 0 items/edges? Is the Relay pagination spec incorrect?


The Relay pagination specification says the following about PageInfo:

It must also contain fields startCursor and endCursor, both of which return non-null opaque strings.

[...]

startCursor and endCursor must be the cursors corresponding to the first and last nodes in edges, respectively.

But what if I want to return zero items? There are lots of reason why a request to that specific endpoint would return an empty connection.

I don't see what values startCursor and endCursor should have in that case. The obvious answer is null, but the spec explicitly says "non-null". What's up with that?

For what it's worth, I looked at roughly 15 articles about the topic of "graphql pagination" and all of those either ignore the issue or assign null in the case of an empty list. So at this point it seems that the spec is just wrong or incompatible with the real world?


Solution

  • Update: (31-01-2022): The spec was updated and these fields are now officially nullable.

    (with this pull request )


    I Filed a bug on the relay Github: https://github.com/facebook/relay/issues/3708

    It seems the canonical solution is to ignore the spec and make them non-null as both the JavaScript and java implementations do that.

    Graphql-Relay-JS: https://github.com/graphql/graphql-relay-js/blob/3796e0904fbfe0dd86e540d5b4df236330c8a5e7/src/connection/arrayConnection.ts

    Java: https://github.com/graphql-java/graphql-java/blob/1ee9630c7d25ccb55a5b1d9b1ccf7bf7895a8c7f/src/main/java/graphql/relay/Relay.java#L54