javascript 使用Vanilla JavaScript Shopify GraphQL Storefront API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用Vanilla JavaScript Shopify GraphQL Storefront API相关的知识,希望对你有一定的参考价值。

// This example was used to pull in variant titles and IDs dynamically
// on a custom build of the ReCharge customer portal.
//
// Feel free to send me an email if you have any questions.
// 
// Kelly Vaughn -- The Taproom Agency
// kelly@thetaproom.com

// 1. Retrieve product ID in any format 
const productId = <pathToProductId>;

// 2. Define GraphQL input
const getVariantData = productId => `{
  products(query:"id:${productId}" first:1 ) {
    edges {
      node {
        variants(first:100) {
          edges {
            node {
              title
              id
            }
          }
        }
      }
    }
  } 
}`;

// 3. Add component to render variants
const renderVariants = ({data}) => {
  // fill in as needed
};

// 4. Define options
const options = {
  method: "post",
  headers: {
    "Content-Type": "application/graphql",
    "X-Shopify-Storefront-Access-Token": "<storefront-access-token>"
  },
  body: getVariantData(productId)
};

// 5. Fetch data
fetch(`https://<shop-name>.myshopify.com/api/graphql`, options)
  .then(res => res.json())
  .then(renderVariants);

以上是关于javascript 使用Vanilla JavaScript Shopify GraphQL Storefront API的主要内容,如果未能解决你的问题,请参考以下文章

javascript 使用Vanilla JavaScript Shopify GraphQL Storefront API

javascript 使用vanilla JS观察对象属性更改

将“Vanilla”Javascript 库加载到 Node.js 中

javascript 使用vanilla JS从URL查询字符串中获取值

使用 Vanilla Javascript 发布到 Php 文件 [重复]

如何使用 vanilla JavaScript 查找 div 的宽度?