GraphQL の startCursor/endCursor は nullable でもよい

GraphQL で ページネーションを実装するとき、Relay GraphQL Cursor Connections Specification に則ると、 startCursor/endCursor は non-null にする必要がある。

GraphQL Cursor Connections Specification

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

ただ、要素が空の場合は指し示す cursor 自体存在しないので “” のような空の文字列を返すことになり、これがちょっと気持ち悪い。

2019年頃から startCursor/endCursor は nullable にしてはどうかという issue が経って議論されているんだけど、最近どうやら進展があって nullable が許容された様子。 現実的には nullable で実装されていることが多いので、仕様もそっちに寄せましたみたいな内容。

https://github.com/facebook/relay/pull/2655#pullrequestreview-857972245

It's clear to me that startCursor/endCursor should be nullable, are treated as nullable by Relay, and are generally implemented in the community as nullable. The specification should reflect this de facto reality.

ドキュメント自体はまだ更新されていない様子だけど、そのうち多分更新されそう。