1 | |
2 |
|
3 | |
4 | * Update Article |
5 | * Update an article. Empty fields are left unchanged; tags, when given, replace the existing list. Visibility only changes when published is set to publish or unpublish. Accepts a numeric ID or a GID. Needs the write_content scope. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | article_id: string, |
10 | title: string | undefined, |
11 | author_name: string | undefined, |
12 | body_html: string | undefined, |
13 | summary_html: string | undefined, |
14 | tags: string[] | undefined, |
15 | image_url: string | undefined, |
16 | image_alt_text: string | undefined, |
17 | published: "keep" | "publish" | "unpublish" = "keep", |
18 | publish_date: string | undefined |
19 | ) { |
20 | const id = article_id.startsWith("gid://") |
21 | ? article_id |
22 | : `gid://shopify/Article/${article_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 ($id: ID!, $article: ArticleUpdateInput!) { articleUpdate(id: $id, article: $article) { article { id title handle isPublished publishedAt author { name } blog { id title } tags summary image { url altText } createdAt updatedAt } userErrors { field message } } }`, |
35 | variables: { |
36 | id, |
37 | article: { |
38 | title: title || undefined, |
39 | author: author_name ? { name: author_name } : undefined, |
40 | body: body_html || undefined, |
41 | summary: summary_html || undefined, |
42 | tags: tags?.length ? tags : undefined, |
43 | image: image_url |
44 | ? { url: image_url, altText: image_alt_text || undefined } |
45 | : undefined, |
46 | isPublished: |
47 | published === "publish" |
48 | ? true |
49 | : published === "unpublish" |
50 | ? false |
51 | : undefined, |
52 | publishDate: publish_date || undefined, |
53 | }, |
54 | }, |
55 | }), |
56 | } |
57 | ) |
58 |
|
59 | if (!response.ok) { |
60 | throw new Error(`${response.status} ${await response.text()}`) |
61 | } |
62 |
|
63 | const { data, errors } = await response.json() |
64 | if (errors) { |
65 | throw new Error(JSON.stringify(errors)) |
66 | } |
67 |
|
68 | if (data.articleUpdate.userErrors.length) { |
69 | throw new Error(JSON.stringify(data.articleUpdate.userErrors)) |
70 | } |
71 |
|
72 | return data.articleUpdate |
73 | } |
74 |
|