0

Run Bulk Query

by
Published today

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).

Script shopify Verified

The script

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

3
/**
4
 * Run Bulk Query
5
 * 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).
6
 */
7
export async function main(auth: RT.Shopify, bulk_query: string) {
8
  const response = await fetch(
9
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
10
    {
11
      method: "POST",
12
      headers: {
13
        "X-Shopify-Access-Token": auth.token,
14
        "Content-Type": "application/json",
15
        Accept: "application/json",
16
      },
17
      body: JSON.stringify({
18
        query: `mutation ($query: String!) { bulkOperationRunQuery(query: $query) { bulkOperation { id status type query createdAt } userErrors { field message } } }`,
19
        variables: { query: bulk_query },
20
      }),
21
    }
22
  )
23

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

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

36
  return data.bulkOperationRunQuery
37
}
38