1 | |
2 |
|
3 | |
4 | * List Fulfillment Orders |
5 | * List fulfillment orders across the shop (filtered with Shopify's search syntax, e.g. `status:open`), or the fulfillment orders of one order when order_id is set. Each one shows its status, holds, supported actions and line item ids (the first 10). Returns one page; pass pageInfo.endCursor as after for the next. Needs the read_merchant_managed_fulfillment_orders scope (plus read_assigned/read_third_party for other locations). |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | order_id: string | undefined, |
10 | query: string | undefined, |
11 | include_closed: boolean | undefined, |
12 | sort_key: "UPDATED_AT" | "ID" | undefined, |
13 | reverse: boolean | undefined, |
14 | first: number | undefined, |
15 | after: string | undefined |
16 | ) { |
17 | const response = await fetch( |
18 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
19 | { |
20 | method: "POST", |
21 | headers: { |
22 | "X-Shopify-Access-Token": auth.token, |
23 | "Content-Type": "application/json", |
24 | Accept: "application/json", |
25 | }, |
26 | body: JSON.stringify( |
27 | order_id |
28 | ? { |
29 | query: `query ($id: ID!, $first: Int!, $after: String, $query: String, $reverse: Boolean) { order(id: $id) { fulfillmentOrders(first: $first, after: $after, query: $query, reverse: $reverse) { nodes { id status requestStatus orderId orderName createdAt updatedAt fulfillAt fulfillBy assignedLocation { name location { id } } fulfillmentHolds { id reason reasonNotes handle } supportedActions { action } lineItems(first: 10) { nodes { id sku productTitle variantTitle totalQuantity remainingQuantity lineItem { id } } } } pageInfo { hasNextPage endCursor } } } }`, |
30 | variables: { |
31 | id: order_id.startsWith("gid://") |
32 | ? order_id |
33 | : `gid://shopify/Order/${order_id}`, |
34 | first: first || 50, |
35 | after: after || undefined, |
36 | query: query || undefined, |
37 | reverse: reverse || undefined, |
38 | }, |
39 | } |
40 | : { |
41 | query: `query ($first: Int!, $after: String, $query: String, $includeClosed: Boolean, $sortKey: FulfillmentOrderSortKeys, $reverse: Boolean) { fulfillmentOrders(first: $first, after: $after, query: $query, includeClosed: $includeClosed, sortKey: $sortKey, reverse: $reverse) { nodes { id status requestStatus orderId orderName createdAt updatedAt fulfillAt fulfillBy assignedLocation { name location { id } } fulfillmentHolds { id reason reasonNotes handle } supportedActions { action } lineItems(first: 10) { nodes { id sku productTitle variantTitle totalQuantity remainingQuantity lineItem { id } } } } pageInfo { hasNextPage endCursor } } }`, |
42 | variables: { |
43 | first: first || 50, |
44 | after: after || undefined, |
45 | query: query || undefined, |
46 | includeClosed: include_closed || undefined, |
47 | sortKey: sort_key || undefined, |
48 | reverse: reverse || undefined, |
49 | }, |
50 | } |
51 | ), |
52 | } |
53 | ) |
54 |
|
55 | if (!response.ok) { |
56 | throw new Error(`${response.status} ${await response.text()}`) |
57 | } |
58 |
|
59 | const { data, errors } = await response.json() |
60 | if (errors) { |
61 | throw new Error(JSON.stringify(errors)) |
62 | } |
63 |
|
64 | if (order_id) { |
65 | if (!data.order) { |
66 | throw new Error(`Order ${order_id} not found`) |
67 | } |
68 | return data.order.fulfillmentOrders |
69 | } |
70 |
|
71 | return data.fulfillmentOrders |
72 | } |
73 |
|