0
Execute GraphQL Query
One script reply has been approved by the moderators Verified
Created by danielkelbelle11 685 days ago Viewed 4559 times
0
Submitted by adam186 Deno
Verified 486 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