1 | |
2 |
|
3 | |
4 | * Search Draft Orders |
5 | * Search draft orders with Shopify's search syntax (e.g. `status:open`, `customer_id:123`, `tag:quote updated_at:>2026-01-01`). Leave query empty to list them all. Returns one page; pass pageInfo.endCursor as after to get the next one. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | query: string | undefined, |
10 | sort_key: |
11 | | "UPDATED_AT" |
12 | | "NUMBER" |
13 | | "STATUS" |
14 | | "TOTAL_PRICE" |
15 | | "CUSTOMER_NAME" |
16 | | "ID" |
17 | | "RELEVANCE" |
18 | | undefined, |
19 | reverse: boolean | undefined, |
20 | first: number | undefined, |
21 | after: string | undefined |
22 | ) { |
23 | const response = await fetch( |
24 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
25 | { |
26 | method: "POST", |
27 | headers: { |
28 | "X-Shopify-Access-Token": auth.token, |
29 | "Content-Type": "application/json", |
30 | Accept: "application/json", |
31 | }, |
32 | body: JSON.stringify({ |
33 | query: `query ($first: Int!, $after: String, $query: String, $sortKey: DraftOrderSortKeys, $reverse: Boolean) { draftOrders(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id legacyResourceId name status email phone createdAt updatedAt completedAt invoiceUrl invoiceSentAt tags note2 ready reserveInventoryUntil subtotalPriceSet { shopMoney { amount currencyCode } } totalDiscountsSet { shopMoney { amount currencyCode } } totalShippingPriceSet { shopMoney { amount currencyCode } } totalTaxSet { shopMoney { amount currencyCode } } totalPriceSet { shopMoney { amount currencyCode } } customer { id displayName defaultEmailAddress { emailAddress } } order { id name } } pageInfo { hasNextPage endCursor } } }`, |
34 | variables: { |
35 | first: first || 50, |
36 | after: after || undefined, |
37 | query: query || undefined, |
38 | sortKey: sort_key || undefined, |
39 | reverse: reverse || undefined, |
40 | }, |
41 | }), |
42 | } |
43 | ) |
44 |
|
45 | if (!response.ok) { |
46 | throw new Error(`${response.status} ${await response.text()}`) |
47 | } |
48 |
|
49 | const { data, errors } = await response.json() |
50 | if (errors) { |
51 | throw new Error(JSON.stringify(errors)) |
52 | } |
53 |
|
54 | return data.draftOrders |
55 | } |
56 |
|