Remix Example

Remix example demonstrates how to integrate WunderGraph with Remix.run.

Installation

1
npx create-wundergraph-app --example remix

Configuration

Let's start by configuring WunderGraph. We will use the SpaceX API as an example.

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

What's notable here is that we're using templates.typescript.client to generate our base client that is used by SWR @wundergraph/swr package.

Next up is setting up the client and SWR hooks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// lib/wundergraph.ts
import { createClient, Operations } from '../generated/client';
import { createHooks } from '@wundergraph/swr';
export const client = createClient();
export const createClientFromCookies = (request: Request) => {
const cookieHeader = request.headers.get('Cookie');
const client = createClient({
extraHeaders: {
cookie: cookieHeader ?? '',
},
});
return client;
};
// Use these hooks for any client side operations
export const { useQuery, useMutation, useSubscription, useUser, useAuth } = createHooks<Operations>(client);

WunderGraph comes with a powerful framework for generating code. Here we are creating fully typed SWR hooks based on the operations of our WunderGraph application.

Define an Operation

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

Run a Query

Fetch data inside a loader, using the WunderGraph client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// app/routes/index.tsx
import { json } from '@remix-run/node';
import { useCatch, useLoaderData } from '@remix-run/react';
import { client } from 'lib/wundergraph';
export const loader = async () => {
const res = await client.query({
operationName: 'Dragons',
});
if (res.error) {
throw json(res.error);
}
return res.data;
};
export function CatchBoundary() {
const caught = useCatch();
return (
<div>
<h1>Error!</h1>
{JSON.stringify(caught)}
</div>
);
}
export default function Index() {
const { spacex_dragons } = useLoaderData<typeof loader>();
return (
<code className="p-3" data-testid="result">
{JSON.stringify(spacex_dragons, null, 2)}
</code>
);
}

Learn more

Deploy to WunderGraph Cloud

The easiest way to deploy your WunderGraph backend is to use WunderGraph Cloud. Enable the Vercel integration to deploy the Vite frontend to Vercel.

Deploy to WunderGraph

Was this article helpful to you?
Provide feedback

Edit this page