1 | |
2 |
|
3 | |
4 | * Update Page |
5 | * Update a page. Empty fields are left unchanged, and visibility only changes when published is set to publish or unpublish. Accepts a numeric ID or a GID. Needs the write_online_store_pages scope. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | page_id: string, |
10 | title: string | undefined, |
11 | body_html: string | undefined, |
12 | handle: string | undefined, |
13 | published: "keep" | "publish" | "unpublish" = "keep", |
14 | publish_date: string | undefined |
15 | ) { |
16 | const id = page_id.startsWith("gid://") |
17 | ? page_id |
18 | : `gid://shopify/Page/${page_id}` |
19 |
|
20 | const response = await fetch( |
21 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
22 | { |
23 | method: "POST", |
24 | headers: { |
25 | "X-Shopify-Access-Token": auth.token, |
26 | "Content-Type": "application/json", |
27 | Accept: "application/json", |
28 | }, |
29 | body: JSON.stringify({ |
30 | query: `mutation ($id: ID!, $page: PageUpdateInput!) { pageUpdate(id: $id, page: $page) { page { id title handle isPublished publishedAt templateSuffix createdAt updatedAt } userErrors { field message } } }`, |
31 | variables: { |
32 | id, |
33 | page: { |
34 | title: title || undefined, |
35 | body: body_html || undefined, |
36 | handle: handle || undefined, |
37 | isPublished: |
38 | published === "publish" |
39 | ? true |
40 | : published === "unpublish" |
41 | ? false |
42 | : undefined, |
43 | publishDate: publish_date || undefined, |
44 | }, |
45 | }, |
46 | }), |
47 | } |
48 | ) |
49 |
|
50 | if (!response.ok) { |
51 | throw new Error(`${response.status} ${await response.text()}`) |
52 | } |
53 |
|
54 | const { data, errors } = await response.json() |
55 | if (errors) { |
56 | throw new Error(JSON.stringify(errors)) |
57 | } |
58 |
|
59 | if (data.pageUpdate.userErrors.length) { |
60 | throw new Error(JSON.stringify(data.pageUpdate.userErrors)) |
61 | } |
62 |
|
63 | return data.pageUpdate |
64 | } |
65 |
|