Skip to content

Guide · Open source

How to use Relay: install, run the example and learn the basics

Updated · written with AI from the project's own documentation and checked against our catalogue data

Relay is a JavaScript framework for building data-driven React applications. You declare the data each component needs in GraphQL, and Relay decides how and when to fetch it. This guide shows you how to get the TodoMVC example running and explains the main ideas you need before building your own app.

Relaymeta/relay · ★ 19k · MIT

Quick start

  1. 1Clone the example. Run git clone https://github.com/relayjs/relay-examples.git to download the TodoMVC example.
  2. 2Open the todo folder. Run cd relay-examples/todo to enter the example project.
  3. 3Install dependencies. Run yarn to install the project's dependencies.
  4. 4Run the setup scripts. Run yarn grats and yarn relay as shown in the README before starting the app.
  5. 5Start the dev server. Run yarn dev, then open http://localhost:5173 in your browser.

What Relay does and who it is for

Relay lets React components declare their data requirements with GraphQL instead of calling a data store through an imperative API. Relay then works out how and when to fetch that data.

Relay is built for React developers who already use GraphQL, and for teams with large React apps that need consistent data across many components. It is not a good fit for projects that do not use React, for teams without a GraphQL API, or for beginners who are new to both React and GraphQL.

  • Declarative: you describe the data you need and Relay handles fetching.
  • Colocation: queries live next to the views that use them, and Relay combines them into efficient network requests.
  • Mutations: you change data with GraphQL mutations, with automatic data consistency, optimistic updates and error handling.

Install Relay by running the TodoMVC example

The simplest way to try Relay is the TodoMVC example in the relay-examples repository. Relay is installed by cloning and building this example, and Yarn is the package manager used in the instructions. The README does not state a Yarn version.

Clone the repository, move into the todo folder, then install dependencies and start the dev server.

  • Prerequisite: Yarn.
  • The example runs on a local dev server.
  • After starting it, open http://localhost:5173 in your browser.
git clone https://github.com/relayjs/relay-examples.git cd relay-examples/todo yarn yarn grats yarn relay yarn dev

Understand the commands in the example

The README lists four Yarn steps after cloning. yarn installs the dependencies. yarn dev starts the dev server. The README also runs yarn grats and yarn relay before the dev server starts, so follow the full sequence shown in the example.

The README does not explain what each of these scripts does internally, so check the example's own files if you need more detail.

  • yarn: install dependencies.
  • yarn grats: run before starting the app, as shown in the README.
  • yarn relay: run before starting the app, as shown in the README.
  • yarn dev: start the development server.

Learn the core ideas: declare data, colocate queries, mutate

In Relay, each component states the GraphQL data it depends on. Because the query sits beside the view, you can read a component and see exactly what it uses.

To change data, Relay uses GraphQL mutations on the client and server. Relay also supports optimistic updates, which the README lists as part of its mutation support.

  • Write a GraphQL query next to the component that renders its data.
  • Let Relay combine queries into network requests for only the data needed.
  • Use GraphQL mutations to change data, with optimistic updates and error handling.

Check Relay against other data libraries

Relay is one of several options for fetching data in React. The comparable projects listed for this page are Apollo Client, urql, React Query and relay-hooks.

Relay is the stronger choice when you want the framework to handle fetching and your team already uses GraphQL. If you do not use GraphQL or want a lighter setup, one of the other listed projects may suit you better.

What people use it for

  • Building a React app where many components show the same data and must stay consistent.
  • Adding optimistic updates so the interface reflects a user's change before the server responds.
  • Using a GraphQL API with React while letting the framework decide when to fetch data.
  • Studying the TodoMVC example to see how queries and mutations sit alongside React views.

Frequently asked questions

How do I install Relay?

The README shows a clone-and-build approach: clone the relay-examples repository, enter the todo folder, then run yarn, yarn grats, yarn relay and yarn dev. Yarn is the package manager the README uses, and it states no version.

Is Relay free to use?

Relay is MIT licensed, as stated in the README and its license file. The source does not mention pricing for any hosted service.

What do I need before using Relay?

You need Yarn for the example setup, and a GraphQL API because Relay's queries are written in GraphQL. Relay is built for React applications, so your project should use React.

Can I use Relay without React?

No. The source describes Relay as a framework for building data-driven React applications, and it is built for React.

What are the alternatives to Relay?

The comparable projects listed are Apollo Client, urql, React Query and relay-hooks. The source does not rank them against Relay.

Why won't the example load at localhost:5173?

The source does not cover troubleshooting. Check that you ran every step in order, including yarn grats and yarn relay, and that yarn dev is running before you open http://localhost:5173.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Relay suits React developers who already use GraphQL, especially on large apps where consistent data and optimistic updates matter. If your project does not use React or GraphQL, or you are new to both, pick a simpler data library such as React Query and come back to Relay later.

Install options & downloads