urql and Relay
Two alternatives to Apollo Client: urql with its small core and document cache, and Relay with its compiler and fragment colocation for large apps.
urql and Relay
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- urql is a small client built from exchanges, a pipeline you can add to or replace, and it ships with a document cache by default.
- urql's document cache invalidates a cached query when a mutation returns a type that the query contains, which is simple and sometimes too eager.
- Relay is built around fragments: each component declares the data it needs, and a compiler assembles the queries.
- Relay expects schema conventions, namely the Node interface for global ids and connections for pagination.
- Choose by team and app size. Apollo is the common default, urql is lighter, Relay pays off in large apps with many components sharing data.
Same wire, different opinions
Every GraphQL client sends the same POST you wrote by hand in the fetch lesson. They differ in what they remember, how they decide a result is stale, and how much structure they impose on your components. Apollo Client chose a normalised cache with a large feature surface. Two others take different bets.
urql: a small core and a pipeline
urql, originally from Formidable, starts from a minimal client and makes behaviour pluggable through exchanges. An operation goes down a chain of exchanges and the result comes back up. The usual chain is a cache exchange followed by a fetch exchange. Swap in a normalised cache, add retry, add persisted queries, or add an auth exchange, all without touching components.
I ran urql’s core client (@urql/core 6.0.3, the engine behind urql 5.0.4) against a local Yoga server:
import { Client, cacheExchange, fetchExchange, gql } from "@urql/core";
const client = new Client({
url: "http://localhost:4000/graphql",
exchanges: [cacheExchange, fetchExchange],
});
await client.query(gql`query { books { id title } }`, {}).toPromise();
The default request policy was cache-first, the same idea as Apollo’s. The interesting part came next. I ran a mutation, addBook, that returned a Book, then ran the books query again with the default policy. It returned three books, not two, without my asking for a refetch.
That is urql’s document cache at work. It keys results by the query document and variables, and it tracks which __typenames each result contains. A mutation result that includes Book invalidates every cached query that also contained Book. No normalisation, no update function, and lists refresh without you thinking about them. The cost is bluntness: a mutation touching one book refetches every query that mentions books, and two components that show the same book can still hold separate copies until then. If that bites, urql’s optional @urql/exchange-graphcache adds a normalised cache with the same ideas as Apollo’s, at the cost of more configuration.
The React package exposes hooks like useQuery and useMutation over this client. I did not render React with it in this session, so treat component code as docs territory.
Relay: the client Meta built for scale
Relay is a different kind of tool. Where Apollo and urql let a component write its own query, Relay asks every component to declare a fragment describing the fields it needs, and only the route or page entry point writes a query. A BookRow component says “I need title and author { name }”. The page query composes those fragments.
fragment BookRow_book on Book {
title
author { name }
}
Two consequences follow. First, a component sees only the data it asked for, so a parent cannot accidentally depend on a child’s fields, and a child’s change does not force edits up the tree. This is the fragment colocation idea from the fragments lesson taken to its conclusion. Second, because queries are assembled from fragments, Relay does it at build time with the Relay compiler, which also generates TypeScript types and optimises what is sent. Queries are static, which makes persisted queries nearly free.
Relay also leans on schema conventions. Objects that can be refetched implement the Node interface with a globally unique id (global object identification), and lists are exposed as connections (Relay connections). Relay is the reason those two conventions exist. With a server that follows them, Relay can refetch any entity by id and paginate any list uniformly. With a server that does not, you will fight the client.
I did not set up Relay here: it needs a compiler configuration and a React app, so everything in this section is docs-derived and from the video.
Choosing
- Apollo Client: the common default, richest ecosystem, normalised cache, most tutorials.
- urql: smaller, easy to reason about, good when a document cache is enough or you want to compose your own pipeline.
- Relay: best at scale, with many components and a team willing to adopt its conventions and build step. Overkill for a small app.
- No client: a fetch helper plus a server-state library, if your data is mostly request-response.
None of these is wrong. The decision is about how much of the cache you want to manage by hand, and the answer usually changes when the app grows.
What the video covers
PromptQL’s “Deep dive on Relay GraphQL client for React”, from July 2020, runs about 13 minutes. Its chapters are: intro, what is Relay, the Relay compiler, GraphQL fragments, and Relay GraphQL endpoint. It is a Relay overview only; urql is not covered, which is why two urql videos sit under “go deeper”. Being from 2020, the code it shows may differ from current Relay releases. Treat it as the mental model, not a copy-paste setup. I could not read a transcript.
Where this fits
This ends the “which client” question. The remaining client lesson, code generation and typed operations, applies to all of them, because it turns a schema and your operations into TypeScript types.
Check yourself
What is an exchange in urql?
A step in the pipeline that every operation and result passes through, such as the cache exchange or the fetch exchange. You can add, remove or reorder them.
Why does Relay need a compiler?
Components declare fragments, and the compiler stitches them into whole queries at build time and generates types, so no query is assembled at runtime.
What does Relay require of your schema?
Conventions: objects that implement Node with a global id, and list fields exposed as connections with cursors.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.