0

Execute GraphQL Query

by
Published Jun 6, 2022
Script faunadb Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
type Faunadb = {
4
  region: string;
5
  secret: string;
6
};
7
export async function main(
8
  auth: Faunadb,
9
  query: string,
10
  variables?: Record<string, any>,
11
  operationName?: string,
12
) {
13
  const region = ["us", "eu"].includes(auth.region) ? auth.region : "";
14
  const response = await fetch(`https://graphql.${region}.fauna.com/graphql`, {
15
    method: "POST",
16
    headers: {
17
      Authorization: "Bearer " + auth.secret,
18
    },
19
    body: JSON.stringify({
20
      query,
21
      variables: variables || {},
22
      operationName: operationName || null,
23
    }),
24
  });
25
  return await response.json();
26
}
27

Other submissions
  • Submitted by adam186 Deno
    Created 398 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