1 | |
2 |
|
3 | |
4 | * Update Product |
5 | * Update a product's fields. Only the fields you fill in are changed; tags, when given, replace all existing tags (use Add Tags / Remove Tags to edit them incrementally). |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | product_id: string, |
10 | title: string | undefined, |
11 | description_html: string | undefined, |
12 | vendor: string | undefined, |
13 | product_type: string | undefined, |
14 | tags: string[] | undefined, |
15 | status: "DRAFT" | "ACTIVE" | "UNLISTED" | "ARCHIVED" | undefined, |
16 | handle: string | undefined, |
17 | seo_title: string | undefined, |
18 | seo_description: string | undefined |
19 | ) { |
20 | const id = product_id.startsWith("gid://") |
21 | ? product_id |
22 | : `gid://shopify/Product/${product_id}` |
23 |
|
24 | const response = await fetch( |
25 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
26 | { |
27 | method: "POST", |
28 | headers: { |
29 | "X-Shopify-Access-Token": auth.token, |
30 | "Content-Type": "application/json", |
31 | Accept: "application/json", |
32 | }, |
33 | body: JSON.stringify({ |
34 | query: `mutation ($product: ProductUpdateInput!) { productUpdate(product: $product) { product { id legacyResourceId title handle status vendor productType tags descriptionHtml updatedAt seo { title description } } userErrors { field message } } }`, |
35 | variables: { |
36 | product: { |
37 | id, |
38 | title: title || undefined, |
39 | descriptionHtml: description_html || undefined, |
40 | vendor: vendor || undefined, |
41 | productType: product_type || undefined, |
42 | tags: tags?.length ? tags : undefined, |
43 | status: status || undefined, |
44 | handle: handle || undefined, |
45 | seo: |
46 | seo_title || seo_description |
47 | ? { |
48 | title: seo_title || undefined, |
49 | description: seo_description || undefined, |
50 | } |
51 | : undefined, |
52 | }, |
53 | }, |
54 | }), |
55 | } |
56 | ) |
57 |
|
58 | if (!response.ok) { |
59 | throw new Error(`${response.status} ${await response.text()}`) |
60 | } |
61 |
|
62 | const { data, errors } = await response.json() |
63 | if (errors) { |
64 | throw new Error(JSON.stringify(errors)) |
65 | } |
66 | if (data.productUpdate.userErrors.length) { |
67 | throw new Error(JSON.stringify(data.productUpdate.userErrors)) |
68 | } |
69 |
|
70 | return data.productUpdate |
71 | } |
72 |
|