Clients

Next.js client

The Next.js client uses SWR under the hood.

Installation

1
npm i @wundergraph/nextjs swr

Configuration

Add NextJsTemplate to your WunderGraph configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { NextJsTemplate } from '@wundergraph/nextjs/dist/template';
// wundergraph.config.ts
configureWunderGraphApplication({
// ... your config
generate: {
codeGenerators: [
{
templates: [new NextJsTemplate()],
path: '../components/generated',
},
],
},
});

Hooks

useQuery

This hook accepts most useSWR Options except for key and fetcher.

1
2
3
4
5
6
7
const { data, error, isValidating, isLoading, mutate } = useQuery({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
enabled: true,
});

Calling mutate will invalidate and refetch the query.

1
2
3
4
5
6
7
8
const { data, mutate } = useQuery({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
});
mutate();

useQuery (Live query)

You can turn any query into a live query by adding the liveQuery option.

1
2
3
4
5
6
7
const { data, error, isLoading, isSubscribed, mutate } = useQuery({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
liveQuery: true,
});

useMutation

This hook accepts most useSWRMutation Options except for key and fetcher.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const { data, error, trigger } = useMutation({
operationName: 'SetName',
});
await trigger({
name: 'test',
});
trigger(
{
name: 'test',
},
{
throwOnError: false,
}
);

useSubscription

1
2
3
4
5
6
7
8
9
10
11
12
13
const { data, error, isLoading, isSubscribed } = useSubscription({
operationName: 'Weather',
input: {
forCity: 'Berlin',
},
enabled: true,
onSuccess(data, key, config) {
// called when the subscription is established.
},
onError(data, key, config) {
// called when the subscription failed to establish.
},
});

useAuth

1
2
3
4
5
const { login, logout } = useAuth();
login('github');
logout({ logoutOpenidConnectProvider: true });

useUser

This hook accepts most useSWR Options except for key and fetcher.

1
const { data, error, isLoading } = useUser();

File upload

This hook accepts most useSWRMutation Options except for key and fetcher.

1
2
3
4
5
6
7
8
9
10
11
const { upload, data, error } = useFileUpload();
upload(
{
provider: 'minio',
files: [new File([''], 'test.txt')],
},
{
throwOnError: false,
}
);

SSR

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

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);

Global Configuration

You can configure the hooks globally by using the SWRConfig context.

In case the context configuration isn't working, it's likely due to multiple versions of SWR being installed or due to how PNPM or Yarn PnP link packages. To resolve this you can import SWR directly from @wundergraph/nextjs to make sure the same instance is used.

1
import { SWRConfig, useSWRConfig } from '@wundergraph/nextjs';
Previous
SWR

Was this article helpful to you?
Provide feedback

Edit this page