0

GraphQL Query

by
Published today

Run any query or mutation against the Shopify GraphQL Admin API (version 2026-10), with optional variables. Throws on GraphQL errors and returns data. Mutation userErrors are returned as-is, so check them.

Script shopify Verified

The script

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

3
/**
4
 * GraphQL Query
5
 * Run any query or mutation against the Shopify GraphQL Admin API (version 2026-10), with optional variables. Throws on GraphQL errors and returns data. Mutation userErrors are returned as-is, so check them.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  query: string,
10
  variables: { [key: string]: any } | undefined
11
) {
12
  const response = await fetch(
13
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
14
    {
15
      method: "POST",
16
      headers: {
17
        "X-Shopify-Access-Token": auth.token,
18
        "Content-Type": "application/json",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({ query, variables: variables ?? undefined }),
22
    }
23
  )
24

25
  if (!response.ok) {
26
    throw new Error(`${response.status} ${await response.text()}`)
27
  }
28

29
  const { data, errors } = await response.json()
30
  if (errors) {
31
    throw new Error(JSON.stringify(errors))
32
  }
33

34
  return data
35
}
36