Edits history of script submission #23758 for ' Update Page (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Page
     * 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.
     */
    export async function main(
      auth: RT.Shopify,
      page_id: string,
      title: string | undefined,
      body_html: string | undefined,
      handle: string | undefined,
      published: "keep" | "publish" | "unpublish" = "keep",
      publish_date: string | undefined
    ) {
      const id = page_id.startsWith("gid://")
        ? page_id
        : `gid://shopify/Page/${page_id}`
    
      const response = await fetch(
        `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
        {
          method: "POST",
          headers: {
            "X-Shopify-Access-Token": auth.token,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            query: `mutation ($id: ID!, $page: PageUpdateInput!) { pageUpdate(id: $id, page: $page) { page { id title handle isPublished publishedAt templateSuffix createdAt updatedAt } userErrors { field message } } }`,
            variables: {
              id,
              page: {
                title: title || undefined,
                body: body_html || undefined,
                handle: handle || undefined,
                isPublished:
                  published === "publish"
                    ? true
                    : published === "unpublish"
                      ? false
                      : undefined,
                publishDate: publish_date || undefined,
              },
            },
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
    
      if (data.pageUpdate.userErrors.length) {
        throw new Error(JSON.stringify(data.pageUpdate.userErrors))
      }
    
      return data.pageUpdate
    }
    

    Submitted by hugo989 3 hours ago