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

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Cancel Fulfillment
     * Cancel a fulfillment. Its items go back to being unfulfilled, on new or reopened fulfillment orders.
     */
    export async function main(auth: RT.Shopify, fulfillment_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!) { fulfillmentCancel(id: $id) { fulfillment { id name status displayStatus updatedAt } userErrors { field message } } }`,
            variables: {
              id: fulfillment_id.startsWith("gid://")
                ? fulfillment_id
                : `gid://shopify/Fulfillment/${fulfillment_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.fulfillmentCancel.userErrors.length) {
        throw new Error(JSON.stringify(data.fulfillmentCancel.userErrors))
      }
    
      return data.fulfillmentCancel
    }
    

    Submitted by hugo989 3 hours ago