The Next.js + Relay example demonstrates how to use Relay with a Next.js based WunderGraph project.
First, run the development server:
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.
Create the utility functions needed to work with Relay
// in src/lib/wundergraph/index.ts
export const { WunderGraphRelayProvider, useLiveQuery, fetchWunderGraphSSRQuery } = createWunderGraphRelayApp({
client,
});
Wrap your _app.tsx
with WunderGraphRelayProvider
// 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
// in src/pages/index.tsx
export async function getServerSideProps() {
const relayData = await fetchWunderGraphSSRQuery<PagesDragonsQueryType>(PagesDragonsQuery);
return {
props: relayData,
};
}
To learn more about WunderGraph Relay integration, read our Quickstart Guide
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!