Edits history of script submission #23773 for ' GraphQL Query (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * GraphQL Query
     * 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.
     */
    export async function main(
      auth: RT.Shopify,
      query: string,
      variables: { [key: string]: any } | undefined
    ) {
      const response = await fetch(
        `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
        {
          method: "POST",
          headers: {
            "X-Shopify-Access-Token": auth.token,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ query, variables: variables ?? undefined }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
    
      return data
    }
    

    Submitted by hugo989 3 hours ago