Relay Compatibility

The Relay GraphQL Server Specification states that in order for a server to be compatible with Relay, it must provide:

  1. A mechanism for refetching an object.

  2. A description of how to page through connections.

The Neo4j GraphQL Library provides a description of how to page through connections out of the box. However, configuration is required for it to provide a mechanism for refetching an object, which this page details.

Object identification

In order for a Relay client to be able to refetch objects, each type must have a unique object identifier which can be used for this purpose. A server then informs a Relay client that this requirement has been satisfied by having types that implement the Node directive, which has the following definition:

interface Node {
  id: ID!
}

So for example, a type Book might have the definition:

type Book implements Node {
  id: ID!
  isbn: String!
}

With that, Relay clients are able to refetch Book objects using the node query field. The following section dives into how to do this with the Neo4j GraphQL Library.

The @relayId directive

The Neo4j GraphQL Library provides an abstraction over the required id field, allowing the value of any unique property in the database to be used as the object identifier.

This is provided by the @relayId directive, which has the following definition:

directive @relayId on FIELD_DEFINITION

Considering the same Book example, ISBN is normally a unique identifier. You can configure this like so:

type Book {
  isbn: String! @relayId
}

When the schema is augmented, this type will be output as:

type Book implements Node {
  id: ID!
  isbn: String!
}

When the id field is used, isbn is the underlying property that is used in the database. Also, the Book type is now refetchable via the node query field, and is ready to be used with a Relay client.

The @relayId directive does not guarantee uniqueness. You should configure a unique node property constraint using the @unique directive.