Relay integration for Next.js apps
relay-nextjs is a tool that simplifies the process of connecting page components with GraphQL queries and data fetching using Relay in Next.js applications. It automatically sets up a Relay environment, fetches data server-side on initial load, serializes the resulting state, and creates a new Relay environment on the client for subsequent navigations. It supports both client-side and server-side rendering but does not currently support Next.js 13 App Router.
To install relay-nextjs, use npm or your preferred package manager:
npm install relay-nextjs
To configure the Relay environment, define the functions getClientEnvironment and createServerEnvironment. For client-side environment:
const getClientEnvironment = () => {
// Define client environment here
}
For server-side environment:
const createServerEnvironment = () => {
// Define server environment here
}
Note: The server environment example creates an environment against a local schema, but it can be configured to fetch from a remote API.
relay-nextjs simplifies data fetching using Relay for page components in Next.js applications by handling the setup of Relay environments, server-side data fetching, and client-side navigation. It allows developers to configure both client-side and server-side Relay environments, making it easier to integrate Relay with Next.js for efficient data management.