Edits history of script submission #23712 for ' Get Product (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Product
     * Get a product by numeric ID or GID, with its options, its first 100 variants (price, SKU, inventory item) and its first 10 media.
     */
    export async function main(auth: RT.Shopify, product_id: string) {
      const id = product_id.startsWith("gid://")
        ? product_id
        : `gid://shopify/Product/${product_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: `query ($id: ID!) { product(id: $id) { id legacyResourceId title handle status vendor productType tags descriptionHtml createdAt updatedAt publishedAt onlineStoreUrl totalInventory tracksInventory hasOnlyDefaultVariant seo { title description } category { id fullName } priceRangeV2 { minVariantPrice { amount currencyCode } maxVariantPrice { amount currencyCode } } options { id name position optionValues { id name hasVariants } } variantsCount { count } variants(first: 100) { nodes { id legacyResourceId title displayName sku price compareAtPrice position inventoryQuantity inventoryPolicy availableForSale taxable selectedOptions { name value } barcodes(first: 5) { nodes { value type } } inventoryItem { id tracked requiresShipping measurement { weight { value unit } } } } pageInfo { hasNextPage endCursor } } media(first: 10) { nodes { id alt mediaContentType status preview { image { url width height } } } } } }`,
            variables: { id },
          }),
        }
      )
    
      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.product
    }
    

    Submitted by hugo989 3 hours ago