在 Relay 中实现 `startCursor` 和 `endCursor`

Implementing `startCursor` and `endCursor` in Relay

我们有一个未用 javascript 编写的 graphql 服务器,我们正在努力使其符合中继规范。 startCursorendCursor 出现在一些例子中,但没有出现在任何官方文档中;根据我对 https://github.com/facebook/relay/issues/372 的阅读,这些字段基本上已被弃用,但它们仍然出现在某些代码中。我们是否必须实施它们:

不,它们没有被弃用,它们确实出现在 the docs 中。该问题表明,如果您不想直接在您的应用程序中使用它们,则您 没有 来实现它们,因为 Relay 将查询 cursor自动为连接中的每个边缘,并将在分页期间发出请求时使用它。

to be spec compliant?

正如您所指出的,这些字段没有出现在规范中,因此不一定要求它们符合规范。我得出这个结论是因为我认为这是任何认真的规范作者应该希望您从他们的规范中缺少某些内容的情况中得出的唯一结论。

to work with existing clients?

当然,这是一个不同的、更实际的问题:)。据我所知,唯一使用连接规范的客户端是 Relay,而 Relay Modern 需要这些字段。由于这些值是 used by the PaginationContainer, the Relay Modern compiler requires them on any field marked with the @connection directive:

[END_CURSOR, HAS_NEXT_PAGE, HAS_PREV_PAGE, START_CURSOR].forEach(
  fieldName => {
    const pageInfoField = pageInfoType.getFields()[fieldName];
    invariant(
      pageInfoField &&
        SchemaUtils.getNullableType(pageInfoField.type) instanceof
          GraphQLScalarType,
      'RelayConnectionTransform: Expected type `%s` to have an ' +
        '%s field for which the type is an scalar in document `%s`.',
      pageInfo.type,
      fieldName,
      definitionName,
    );
  }
);

一直不记得endCursorstartCursor哪个对应哪个分页方向。由于它们未记录在规范中,您可以查看 graphql-relay-js 以获取此信息:

startCursor: {
  type: GraphQLString,
  description: 'When paginating backwards, the cursor to continue.'
},
endCursor: {
  type: GraphQLString,
  description: 'When paginating forwards, the cursor to continue.'
},