0
Execute GraphQL Query
One script reply has been approved by the moderators Verified
Created by danielkelbelle11 694 days ago Viewed 5407 times
0
Submitted by adam186 Deno
Verified 495 days ago
1
type Faunadb = {
2
  region: string;
3
  secret: string;
4
};
5
export async function main(
6
  auth: Faunadb,
7
  query: string,
8
  variables?: Record<string, any>,
9
  operationName?: string,
10
) {
11
  const region = ["us", "eu"].includes(auth.region) ? auth.region : "";
12
  const response = await fetch(`https://graphql.${region}.fauna.com/graphql`, {
13
    method: "POST",
14
    headers: {
15
      Authorization: "Bearer " + auth.secret,
16
    },
17
    body: JSON.stringify({
18
      query,
19
      variables: variables || {},
20
      operationName: operationName || null,
21
    }),
22
  });
23
  return await response.json();
24
}
25