Edits history of script submission #23779 for ' Run Bulk Query (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Run Bulk Query
     * Start an asynchronous bulk export of a GraphQL query with no pagination arguments, selecting connections through edges { node } (nodes is rejected), e.g. `{ products { edges { node { id title } } } }`. Returns the bulk operation; poll it with Get Bulk Operation until status is COMPLETED, then download its url (a JSONL file, one object per line, nested objects linked by __parentId).
     */
    export async function main(auth: RT.Shopify, bulk_query: string) {
      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: `mutation ($query: String!) { bulkOperationRunQuery(query: $query) { bulkOperation { id status type query createdAt } userErrors { field message } } }`,
            variables: { query: bulk_query },
          }),
        }
      )
    
      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))
      }
      if (data.bulkOperationRunQuery.userErrors.length) {
        throw new Error(JSON.stringify(data.bulkOperationRunQuery.userErrors))
      }
    
      return data.bulkOperationRunQuery
    }
    

    Submitted by hugo989 3 hours ago