docs/docs/schema/postgres/relay-schema.mdx
import Thumbnail from '@site/src/components/Thumbnail';
The Hasura GraphQL Engine serves a Relay schema for Postgres tables which have a primary key
defined. The Relay schema can be accessed through the /v1/relay endpoint.
:::tip Supported from
Relay is supported from versions v1.3.0 and above.
:::
Relay is an opinionated JavaScript framework for declaratively fetching and managing GraphQL data. Relay's strength lies in how it removes opportunities for developer errors, by providing conventions that result in performant and type-safe apps.
While using Relay saves you work on the client side with tasks like pagination, using Relay and Hasura together saves you even more work, because Hasura automatically sets up the Relay backend for you on top of your database.
Relay's client-side benefits include:
To learn more about these and other Relay features, like persisted queries, local state management, passing arguments to fragments, and fetching data as early as possible, check out the Relay docs.
:::info Note
For a more detailed breakdown of Relay and its benefits, check out our deep-dive on Relay.
:::
To support the above client-side benefits, Relay has a particular server specification.
Hasura's Relay API sets up this spec for you automatically, so you don't have to implement it manually.
According to the spec, the server must provide:
id field, as
well as a root field called node, which allows fetching data by this id. This is great for performance on the
client side, but hard to implement on the server side, since you have to make sure id's are globally unique, objects
can be re-fetched via their id's, etc.:::info Note
Check out this example repo to see how to set up pagination with Hasura and Relay.
:::
The Node interface in the Relay schema has exactly one field which returns a non-null ID value. Each table object
type in the Relay schema should implement the Node interface to provide
global object identification.
To identify each row in a table, the id field value is encoded with table information (schema and name) and primary
key column values. The GraphQL Engine uses base64 encoded JSON string and the JSON schema looks as follows:
[<version-integer>, "<table-schema>", "<table-name>", "column-1", "column-2", ... "column-n"]
version-integer: The JSON schema version (the current version is 1). This is to enable any backward compatibility
if the JSON representation has to change.table-schema: The table's schema.table-name: The table's name.column-1.. column-n: The primary key column values. The order is Postgres dependent.The same base64 encoded JSON string is accepted for the root node field resolver's id input.
For the author table in the public schema whose primary key column is author_id, of type uuid.
[1, "public", "author", "296d30b1-474d-4011-a907-2701992b04c1"]
And base64 encoded value is
WzEsICJwdWJsaWMiLCAiYXV0aG9yIiwgIjI5NmQzMGIxLTQ3NGQtNDAxMS1hOTA3LTI3MDE5OTJiMDRjMSJd
You can export the Relay schema in the same way as you can
export the GraphQL schema. But instead of the GraphQL endpoint, you
can specify the Relay endpoint, which will end in /v1/relay.
At this time, Hasura's Relay implementation only supports Postgres tables with a primary key defined, and custom SQL functions whose returning table has a primary key defined.
Persisted queries will be supported soon.
:::info Note
Currently, Hasura's Relay schema doesn't expose Remote Schemas or actions. This will be fixed in future releases.
:::