Edits history of script submission #23753 for ' Cancel Fulfillment Order (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Cancel Fulfillment Order
     * Cancel a fulfillment order: the original is closed and a new replacementFulfillmentOrder holds the unfulfilled work, e.g. to reassign it. Works on unsubmitted fulfillment orders and, for a fulfillment service, while the request is SUBMITTED or CANCELLATION_REQUESTED; once the service has accepted it, use Submit Cancellation Request instead.
     */
    export async function main(auth: RT.Shopify, fulfillment_order_id: string) {
      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: `mutation ($id: ID!) { fulfillmentOrderCancel(id: $id) { fulfillmentOrder { id status requestStatus } replacementFulfillmentOrder { id status requestStatus } userErrors { field message } } }`,
            variables: {
              id: fulfillment_order_id.startsWith("gid://")
                ? fulfillment_order_id
                : `gid://shopify/FulfillmentOrder/${fulfillment_order_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))
      }
    
      if (data.fulfillmentOrderCancel.userErrors.length) {
        throw new Error(JSON.stringify(data.fulfillmentOrderCancel.userErrors))
      }
    
      return data.fulfillmentOrderCancel
    }
    

    Submitted by hugo989 3 hours ago