Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing `startCursor` and `endCursor` in Relay

Tags:

relayjs

We have a graphql server not written in javascript, which we're trying to conform to the relay specification. startCursor and endCursor show up in a few examples but not in any official docs; based on my reading of https://github.com/facebook/relay/issues/372 those fields are basically deprecated, but they do show up in some code still. Do we have to implement them:

  • to be spec compliant?
  • to work with existing clients?
like image 522
Evan Avatar asked Jan 22 '16 13:01

Evan


Video Answer


1 Answers

to be spec compliant?

As you point out, these fields don't appear in the spec, so they must not be required to conform to the spec. I conclude this because I think that's the only conclusion any serious authors of a spec should want you to draw from the absence of something from their spec.

to work with existing clients?

This, of course, is a different, more practical question :). The only client that I am aware of that uses the Connection spec is Relay, and Relay Modern requires these fields. Since these values are 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,
    );
  }
);

I never remember which of endCursor and startCursor corresponds to which pagination direction. Since they are not documented in the spec, you can look to graphql-relay-js for this information:

startCursor: {
  type: GraphQLString,
  description: 'When paginating backwards, the cursor to continue.'
},
endCursor: {
  type: GraphQLString,
  description: 'When paginating forwards, the cursor to continue.'
},
like image 193
Dmitry Minkovsky Avatar answered Oct 23 '22 05:10

Dmitry Minkovsky