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 | * List Articles |
35 | * List blog articles, from one blog or all of them, optionally filtered with Shopify's search syntax (e.g. `tag:news author:Jane published_status:published`). Returns one page; pass pageInfo.endCursor as after for the next. Needs the read_content scope. |
36 | */ |
37 | export async function main( |
38 | auth: RT.Shopify, |
39 | blog_id: DynSelect_blog_id | undefined, |
40 | query: string | undefined, |
41 | sort_key: |
42 | | "UPDATED_AT" |
43 | | "PUBLISHED_AT" |
44 | | "TITLE" |
45 | | "AUTHOR" |
46 | | "BLOG_TITLE" |
47 | | "ID" |
48 | | undefined, |
49 | reverse: boolean | undefined, |
50 | first: number | undefined, |
51 | after: string | undefined |
52 | ) { |
53 | const filter = [ |
54 | blog_id ? `blog_id:${blog_id.split("/").pop()}` : "", |
55 | query || "", |
56 | ] |
57 | .filter(Boolean) |
58 | .join(" ") |
59 |
|
60 | const response = await fetch( |
61 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
62 | { |
63 | method: "POST", |
64 | headers: { |
65 | "X-Shopify-Access-Token": auth.token, |
66 | "Content-Type": "application/json", |
67 | Accept: "application/json", |
68 | }, |
69 | body: JSON.stringify({ |
70 | query: `query ($first: Int!, $after: String, $query: String, $sortKey: ArticleSortKeys, $reverse: Boolean) { articles(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id title handle isPublished publishedAt author { name } blog { id title } tags summary image { url altText } createdAt updatedAt } pageInfo { hasNextPage endCursor } } }`, |
71 | variables: { |
72 | first: first || 50, |
73 | after: after || undefined, |
74 | query: filter || undefined, |
75 | sortKey: sort_key || undefined, |
76 | reverse: reverse || undefined, |
77 | }, |
78 | }), |
79 | } |
80 | ) |
81 |
|
82 | if (!response.ok) { |
83 | throw new Error(`${response.status} ${await response.text()}`) |
84 | } |
85 |
|
86 | const { data, errors } = await response.json() |
87 | if (errors) { |
88 | throw new Error(JSON.stringify(errors)) |
89 | } |
90 |
|
91 | return data.articles |
92 | } |
93 |
|