Edits history of script submission #23772 for ' Get Bulk Operation (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Bulk Operation
     * Get a bulk operation by ID (numeric ID or BulkOperation GID): its status (CREATED, RUNNING, COMPLETED, FAILED, ...), object count and, once COMPLETED, the url of the JSONL result (partialDataUrl when it FAILED part-way).
     */
    export async function main(auth: RT.Shopify, bulk_operation_id: string) {
      const id = bulk_operation_id.startsWith("gid://")
        ? bulk_operation_id
        : `gid://shopify/BulkOperation/${bulk_operation_id}`
    
      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: `query ($id: ID!) { bulkOperation(id: $id) { id status type errorCode createdAt completedAt objectCount rootObjectCount fileSize url partialDataUrl query } }`,
            variables: { id },
          }),
        }
      )
    
      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.bulkOperation
    }
    

    Submitted by hugo989 3 hours ago