1 | |
2 |
|
3 | |
4 | * Cancel Fulfillment |
5 | * Cancel a fulfillment. Its items go back to being unfulfilled, on new or reopened fulfillment orders. |
6 | */ |
7 | export async function main(auth: RT.Shopify, fulfillment_id: 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 ($id: ID!) { fulfillmentCancel(id: $id) { fulfillment { id name status displayStatus updatedAt } userErrors { field message } } }`, |
19 | variables: { |
20 | id: fulfillment_id.startsWith("gid://") |
21 | ? fulfillment_id |
22 | : `gid://shopify/Fulfillment/${fulfillment_id}`, |
23 | }, |
24 | }), |
25 | } |
26 | ) |
27 |
|
28 | if (!response.ok) { |
29 | throw new Error(`${response.status} ${await response.text()}`) |
30 | } |
31 |
|
32 | const { data, errors } = await response.json() |
33 | if (errors) { |
34 | throw new Error(JSON.stringify(errors)) |
35 | } |
36 |
|
37 | if (data.fulfillmentCancel.userErrors.length) { |
38 | throw new Error(JSON.stringify(data.fulfillmentCancel.userErrors)) |
39 | } |
40 |
|
41 | return data.fulfillmentCancel |
42 | } |
43 |
|