Relay Examples
repository·main·Indexed 22 days ago
https://github.com/relayjs/relay-examplesA collection of reference implementations and example applications demonstrating Relay best practices and architectural patterns. Includes examples for Next.js 13 React Server Components (RSC) using loadSerializableQuery, data-driven dependencies for dynamic module loading, and full-stack applications like a GitHub Issues clone (issue-tracker), a Newsfeed app, and a TodoMVC implementation using GraphQL Yoga and Vite.
What's inside relay-examples
- This repository is a collection of example applications designed to demonstrate various ways to use Relay. It includes implementations of different application types, such as issue trackers, newsfeeds, and todo lists, to showcase Relay's capabilities in real-world scenarios.
Understand the Newsfeed project structure
mainThe project follows a standard React/TypeScript structure:
src/: Main source code directory.src/components/: React components used in the application.src/index.tsx: The application entry point.webpack.config.js: Webpack configuration.tsconfig.json: TypeScript configuration..prettierrc.json: Prettier configuration..eslintrc.json: ESLint configuration.
Configure GraphQL Schema for Data-Driven Dependencies
mainTo support dynamic module loading, your GraphQL schema must include specific fields and types to communicate module requirements from the server to the client:
- Type or Interface requirement: Must include a
jsfield with the following signature:js(module: String!, id: String): JSDependency
- Scalar Type: A scalar type named
JSDependencymust be defined.
- Type or Interface requirement: Must include a
How the issue-tracker app uses Relay and React features
mainThe
issue-trackerapp is designed to demonstrate how Relay Hooks integrate with modern React features. Key architectural patterns include:- Render-as-you-fetch: The app implements this pattern using React Suspense for data-fetching. During route transitions, the app loads both code and data for new routes in parallel.
- Relay Hooks: Uses
useFragment()and related hooks to colocate data dependencies directly within the components that need them. - Concurrent Mode & Suspense: Uses
useTransition()for route transitions, allowing the app to continue showing the previous route while the next route's data and code are being prepared. - Custom Router Integration: Because React Router does not yet natively support data preloading for routes, this app uses React Router primitives (
historyandreact-router-config) to implement custom preloading of code and data.
Understand the Relay TodoMVC architecture
mainThe Relay TodoMVC application uses a full-stack architecture composed of the following layers:
- Schema: The source of truth is defined using Grats annotations within the
data/directory. TypeScript types drive the schema generation. - Server: A GraphQL Yoga server running on port 3000.
- Client: A React application using Relay for data fetching, bundled with Vite.
- Schema: The source of truth is defined using Grats annotations within the
Understand Data-Driven Dependencies in Relay
mainData-Driven Dependencies allow you to exclude specific React components and Relay artifacts from the initial JavaScript bundle. Instead of loading everything upfront, these modules are loaded dynamically on the client only when the server determines that a specific GraphQL type is about to be rendered. This is achieved through a combination of GraphQL schema extensions, a custom RelayoperationLoader, and a specialized React component calledMatchContainer.How to fetch Relay queries in Next.js 13 React Server Components
mainThis example demonstrates a pattern for fetching Relay queries within Next.js 13 React Server Components (RSC) and hydrating them on the client.
- Server-side Fetching: Use
loadSerializableQuerywithin an asyncpage.tsx(the RSC) to fetch the root query data. This method returns serialized query results. - Data Transfer: Pass these serialized results from the RSC to a Root Client Component (e.g.,
MainViewClientComponent) via props. - Client-side Hydration: In the Client Component, use the
useSerializablePreloadedQueryhook to convert the serialized results into a RelayPreloadedQueryobject. - Rendering: Pass the
PreloadedQueryto a Root Relay Component, which uses theusePreloadedQueryhook to render the data.
flowchart LR; RSC(Root React Server Component)--Serialized Query Results-->RCC(Root Client Component); RCC(Root Client Component)--Preloaded Query-->RRC(Root Relay Component);- Server-side Fetching: Use
Run the Data-Driven Dependencies Example
mainTo run the reference implementation of the Relay and Next.js integration, use the following commands in your terminal:
npm run dev # or yarn devRun the issue-tracker app
mainNavigate to the
issue-tracker/directory and use one of the following commands to start the development server. This will also run the Relay Compiler and open the app athttp://localhost:3000.Using npm:
npm startUsing yarn:
yarn startnpm start # or yarn startSetup the issue-tracker example app
mainTo run the GitHub Issues clone example, follow these steps to clone the repository, install dependencies, and configure the environment.
Clone the repository:
git clone git@github.com:relayjs/relay-examples.git cd relay-examples/issue-trackerInstall dependencies: Using npm:
npm installUsing yarn:
yarnInstall Watchman: Watchman is required as a file watching service.
- macOS/Linux (Homebrew):
brew install watchman - Windows (Chocolatey):
choco install watchman
- macOS/Linux (Homebrew):
Configure GitHub Authentication: The app queries GitHub's public GraphQL API. You must provide a GitHub personal access token with at least the
reposcope.- Create a file named
.env.localin theissue-tracker/directory. - Add the following line, replacing
<TOKEN>with your actual token:REACT_APP_GITHUB_AUTH_TOKEN=<TOKEN>
- Create a file named
git clone git@github.com:relayjs/relay-examples.git cd relay-examples/issue-tracker npm install # Create issue-tracker/.env.local with: # REACT_APP_GITHUB_AUTH_TOKEN=<TOKEN>Install the Simple Issue Tracker Example
mainTo install this example, follow the setup instructions provided in the original issue-tracker repository. This specific version includes TypeScript setup and Next.js 13 React Server Component integration.Regenerate the GraphQL schema
mainIf you have made changes to your GraphQL types or schema definitions and need to update the generated schema file, run the
print-schemascript.yarn print-schema