Clients
React Relay client
WunderGraph Relay Integration
This package provides a type-safe integration of react-relay with WunderGraph. Relay is a JavaScript framework for building data-driven React applications, efficiently fetching and managing data from GraphQL APIs. It optimizes network requests, simplifies client-side data management, and enables performant, scalable apps.
Warning: Only works with WunderGraph.
Getting Started
The easiest way to get started with WunderGraph + Relay is to use one of our templates
Next.js
React with Vite
Add relay to an existing WunderGraph + Relay project
Install WunderGraph Relay sdk into your project
Make sure you have set your code generation to include the base typescript client.
In your relay config add the persistConfig
option and make sure the persisted.json
file is in your .wundergraph/operations/relay
directory (you might have to create an empty file in the target location for the first time)
WunderGraph will use the persisted.json
file to generate the necessary Relay code. Now you can use the WunderGraph Relay provider & utility functions.
Now, in your React App, make sure the whole application is Wrapped under WunderGraphRelayProvider
.
All the existing Relay APIs will work normally, the provider will make sure that Relay uses the WunderGraph generated client behind the scenes.
The Relay integration also provides two very powerful utilities
Live Queries
The useLiveQuery
hooks lets you leverage the power of WunderGraph's Live Queries feature to make your application realtime!
The useLiveQuery
hook syncs with the Relay store which means if the query is already fetched in SSR, it will render immediately.
Render As You Fetch
To implement the Render-As-You-Fetch pattern for your queries in React, you can use the Relay's loadQuery
utility along with the getEnvironment
utility function provided by WunderGraph.
As shown in the above example, the loadQuery
function has to be called outside of the React Component to ensure the fetching happens as soon as the component is loaded into memory. Works well when prefetching pages using React Router or Next Link components.
SSR Support
The fetchWunderGraphSSRQuery
lets you fetch queries on the serverside and hydrate your Relay store on the client using the WunderGraphRelayProvider
On your Server:
On your client:
You can hydrate relay store for the whole application using initialRecords
:
SSG Support (Experimental)
If you are planning to use Relay for SSG projects where pages are generated only on build time & doesn't have a Relay client on the generated pages (SSG projects created with frameworks like Astro or 11ty ), you can use the fetchWunderGraphSSGQuery
which will make the API calls during builds & completely bypasses the need for Relay Store by directly returning the JSON data.
Learn more
To learn more about Relay, read our Quickstart Guide