1 | |
2 |
|
3 | export type DynSelect_blog_id = string |
4 |
|
5 | export async function blog_id(auth: RT.Shopify) { |
6 | const response = await fetch( |
7 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
8 | { |
9 | method: "POST", |
10 | headers: { |
11 | "X-Shopify-Access-Token": auth.token, |
12 | "Content-Type": "application/json", |
13 | Accept: "application/json", |
14 | }, |
15 | body: JSON.stringify({ |
16 | query: `query { blogs(first: 250, sortKey: TITLE) { nodes { id title } } }`, |
17 | }), |
18 | } |
19 | ) |
20 | if (!response.ok) { |
21 | throw new Error(`${response.status} ${await response.text()}`) |
22 | } |
23 | const { data, errors } = await response.json() |
24 | if (errors) { |
25 | throw new Error(JSON.stringify(errors)) |
26 | } |
27 | return data.blogs.nodes.map((b: { id: string; title: string }) => ({ |
28 | value: b.id, |
29 | label: b.title, |
30 | })) |
31 | } |
32 |
|
33 | |
34 | * Create Article |
35 | * Create an article in a blog. image_url must be a publicly reachable image; publish_date (ISO 8601) schedules publication. Needs the write_content scope. |
36 | */ |
37 | export async function main( |
38 | auth: RT.Shopify, |
39 | blog_id: DynSelect_blog_id, |
40 | title: string, |
41 | author_name: string, |
42 | body_html: string | undefined, |
43 | summary_html: string | undefined, |
44 | tags: string[] | undefined, |
45 | image_url: string | undefined, |
46 | image_alt_text: string | undefined, |
47 | is_published: boolean = true, |
48 | publish_date: string | undefined |
49 | ) { |
50 | const blogId = blog_id.startsWith("gid://") |
51 | ? blog_id |
52 | : `gid://shopify/Blog/${blog_id}` |
53 |
|
54 | const response = await fetch( |
55 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
56 | { |
57 | method: "POST", |
58 | headers: { |
59 | "X-Shopify-Access-Token": auth.token, |
60 | "Content-Type": "application/json", |
61 | Accept: "application/json", |
62 | }, |
63 | body: JSON.stringify({ |
64 | query: `mutation ($article: ArticleCreateInput!) { articleCreate(article: $article) { article { id title handle isPublished publishedAt author { name } blog { id title } tags summary image { url altText } createdAt updatedAt } userErrors { field message } } }`, |
65 | variables: { |
66 | article: { |
67 | blogId, |
68 | title, |
69 | author: { name: author_name }, |
70 | body: body_html || undefined, |
71 | summary: summary_html || undefined, |
72 | tags: tags?.length ? tags : undefined, |
73 | image: image_url |
74 | ? { url: image_url, altText: image_alt_text || undefined } |
75 | : undefined, |
76 | isPublished: is_published ?? undefined, |
77 | publishDate: publish_date || undefined, |
78 | }, |
79 | }, |
80 | }), |
81 | } |
82 | ) |
83 |
|
84 | if (!response.ok) { |
85 | throw new Error(`${response.status} ${await response.text()}`) |
86 | } |
87 |
|
88 | const { data, errors } = await response.json() |
89 | if (errors) { |
90 | throw new Error(JSON.stringify(errors)) |
91 | } |
92 |
|
93 | if (data.articleCreate.userErrors.length) { |
94 | throw new Error(JSON.stringify(data.articleCreate.userErrors)) |
95 | } |
96 |
|
97 | return data.articleCreate |
98 | } |
99 |
|