Next.js + Relay Example

The Next.js + Relay example demonstrates how to use Relay with a Next.js based WunderGraph project.

Getting Started

First, run the development server:

1
2
3
4
5
npm run dev
# or
yarn dev
# or
pnpm dev

Open http://localhost:3000 with your browser to see the result.

You can start editing the page by modifying pages/index.tsx. The page auto-updates as you edit the file.

Using WunderGraph with SSR

Create the utility functions needed to work with Relay

1
2
3
4
// in src/lib/wundergraph/index.ts
export const { WunderGraphRelayProvider, useLiveQuery, fetchWunderGraphSSRQuery } = createWunderGraphRelayApp({
client,
});

Wrap your _app.tsx with WunderGraphRelayProvider

1
2
3
4
5
6
7
8
// in src/pages/_app.tsx
export default function App({ Component, pageProps }: AppProps) {
return (
<WunderGraphRelayProvider initialRecords={pageProps.initialRecords}>
<Component {...pageProps} />
</WunderGraphRelayProvider>
);
}

In your pages, use fetchWunderGraphSSRQuery inside getServerSideProps to fetch data on the server

1
2
3
4
5
6
7
8
// in src/pages/index.tsx
export async function getServerSideProps() {
const relayData = await fetchWunderGraphSSRQuery<PagesDragonsQueryType>(PagesDragonsQuery);
return {
props: relayData,
};
}

Learn more about WunderGraph + Relay

To learn more about WunderGraph Relay integration, read our Quickstart Guide

Learn More about Next.js

To learn more about Next.js, take a look at the following resources:

You can check out the Next.js GitHub repository - your feedback and contributions are welcome!

Deploy to WunderGraph

Was this article helpful to you?
Provide feedback

Edit this page