Next.js Example

The NextJS example demonstrates the power of code generation, when it comes to integrating WunderGraph with frontend frameworks like Next.js.

Configuration

Let's start by configuring WunderGraph.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// wundergraph.config.ts
const spaceX = introspect.graphql({
apiNamespace: 'spacex',
url: 'https://spacex-api.fly.dev/graphql/',
});
configureWunderGraphApplication({
apis: [spaceX],
server,
operations,
generate: {
codeGenerators: [
{
templates: [new NextJsTemplate()],
path: '../components/generated',
},
],
},
});

What's notable here is that we're using the NextJsTemplate to generate a Next.js client.

WunderGraph comes with a powerful framework for generating code. In this case, we're generating a TypeScript client, wrapped with React Hooks and an "App" wrapper to enable Server Side Rendering (SSR) out of the box.

Define an Operation

1
2
3
4
5
6
7
# .wundergraph/operations/Dragons.graphql
query Dragons {
spacex_dragons {
name
active
}
}

Use from Next.js

1
2
3
4
5
6
7
8
import { NextPage } from 'next';
import { useQuery, withWunderGraph } from '../components/generated/nextjs';
const Home: NextPage = () => {
const dragons = useQuery({ operationName: 'Dragons' });
return <div>{JSON.stringify(dragons)}</div>;
};
export default withWunderGraph(Home);

Your operations will be compiled into RPC endpoints. The template will generate the NextJS client, so all you have to do is to import the useQuery hook and call your newly created API.

Wrapping the Page in withWunderGraph will make sure that Server Side Rendering (SSR) works, that's it.

Learn more

Deploy to WunderGraph Cloud

The easiest way to deploy your WunderGraph app is to use WunderGraph Cloud. Enable the Vercel integration to deploy the Next.js frontend to Vercel.

Deploy to WunderGraph

Was this article helpful to you?
Provide feedback

Edit this page