-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgraphql.ts
41 lines (34 loc) · 1.53 KB
/
graphql.ts
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
38
39
40
41
/**
* This api route exposes a graphql api that routes requests to the Graph Network Arbitrum Subgraph on The Graph Network.
* With this as an API route, the client never sees or exposes your API Key, securing it to help prevent leaks.
*
* This is exposed at `/api/graphql` route.
*/
import { GraphQLClient } from 'graphql-request';
import type { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';
import { env } from '@/env/server';
// replace this with your Subgrah URL
const subgraphQueryUrl =
'https://gateway-arbitrum.network.thegraph.com/api/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp';
const client = new GraphQLClient(subgraphQueryUrl, {
headers: {
Authorization: `Bearer ${env.API_KEY}`,
},
});
const GraphqlReqSchema = z.object({
query: z.string().min(1),
operationName: z.string().optional().nullable(),
variables: z.record(z.unknown()).optional().nullable(),
});
const ResponseSchema = z.union([z.object({ data: z.unknown() }), z.object({ error: z.unknown() })] as const);
type ResponseSchema = z.infer<typeof ResponseSchema>;
export default async function handler(req: NextApiRequest, res: NextApiResponse<ResponseSchema>) {
const parsedGqlRequest = GraphqlReqSchema.safeParse(req.body);
if (!parsedGqlRequest.success) {
return res.status(400).json({ error: parsedGqlRequest.error });
}
const gqlRequest = parsedGqlRequest.data;
const gqlResponse = await client.request(gqlRequest.query, gqlRequest.variables ?? undefined);
res.status(200).json({ data: gqlResponse });
}