1 | |
2 |
|
3 | |
4 | * Submit Cancellation Request |
5 | * Ask the fulfillment service assigned to a fulfillment order to cancel it. Only valid for fulfillment orders assigned to an API fulfillment service whose request was accepted; the service then accepts or rejects it, after which Cancel Fulfillment Order closes it. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | fulfillment_order_id: string, |
10 | message: string | undefined |
11 | ) { |
12 | const response = await fetch( |
13 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
14 | { |
15 | method: "POST", |
16 | headers: { |
17 | "X-Shopify-Access-Token": auth.token, |
18 | "Content-Type": "application/json", |
19 | Accept: "application/json", |
20 | }, |
21 | body: JSON.stringify({ |
22 | query: `mutation ($id: ID!, $message: String) { fulfillmentOrderSubmitCancellationRequest(id: $id, message: $message) { fulfillmentOrder { id status requestStatus } userErrors { field message } } }`, |
23 | variables: { |
24 | id: fulfillment_order_id.startsWith("gid://") |
25 | ? fulfillment_order_id |
26 | : `gid://shopify/FulfillmentOrder/${fulfillment_order_id}`, |
27 | message: message || undefined, |
28 | }, |
29 | }), |
30 | } |
31 | ) |
32 |
|
33 | if (!response.ok) { |
34 | throw new Error(`${response.status} ${await response.text()}`) |
35 | } |
36 |
|
37 | const { data, errors } = await response.json() |
38 | if (errors) { |
39 | throw new Error(JSON.stringify(errors)) |
40 | } |
41 |
|
42 | if (data.fulfillmentOrderSubmitCancellationRequest.userErrors.length) { |
43 | throw new Error( |
44 | JSON.stringify(data.fulfillmentOrderSubmitCancellationRequest.userErrors) |
45 | ) |
46 | } |
47 |
|
48 | return data.fulfillmentOrderSubmitCancellationRequest |
49 | } |
50 |
|