Edits history of script submission #23729 for ' Create Article (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_blog_id = string
    
    export async function blog_id(auth: RT.Shopify) {
      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: `query { blogs(first: 250, sortKey: TITLE) { nodes { id title } } }`,
          }),
        }
      )
      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))
      }
      return data.blogs.nodes.map((b: { id: string; title: string }) => ({
        value: b.id,
        label: b.title,
      }))
    }
    
    /**
     * Create Article
     * 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.
     */
    export async function main(
      auth: RT.Shopify,
      blog_id: DynSelect_blog_id,
      title: string,
      author_name: string,
      body_html: string | undefined,
      summary_html: string | undefined,
      tags: string[] | undefined,
      image_url: string | undefined,
      image_alt_text: string | undefined,
      is_published: boolean = true,
      publish_date: string | undefined
    ) {
      const blogId = blog_id.startsWith("gid://")
        ? blog_id
        : `gid://shopify/Blog/${blog_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 ($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 } } }`,
            variables: {
              article: {
                blogId,
                title,
                author: { name: author_name },
                body: body_html || undefined,
                summary: summary_html || undefined,
                tags: tags?.length ? tags : undefined,
                image: image_url
                  ? { url: image_url, altText: image_alt_text || undefined }
                  : undefined,
                isPublished: is_published ?? 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.articleCreate.userErrors.length) {
        throw new Error(JSON.stringify(data.articleCreate.userErrors))
      }
    
      return data.articleCreate
    }
    

    Submitted by hugo989 3 hours ago