Shopify GraphQL响应不正确

问题描述

我正在使用Shopify的GraphQL工具来验证我的查询正确无误,并且其响应完全是我期望看到的

screenshot of shopify graphql tool

这是我在店面中使用的代码

const query = `{
  products(first: 1,query: "sku:[DUMMY_SKU_HERE]") {
    edges {
      node {
        id
        handle
        onlinestoreUrl
        title
        images(first: 1) {
          edges {
            node {
              transformedSrc(maxWidth: 100,maxHeight: 100)
            }
          }
        }
      }
    }
  }
}`;

fetch("https://dummystore.myshopify.com/api/2020-07/graphql",{
  method: "POST",headers: {
    Accept: "application/json","Content-Type": "application/graphql","X-Shopify-Storefront-Access-Token": access_token,},body: query,})
.then((response) => response.json())
.then((response) => {
  console.log(response);
})
.catch((error) => console.error(error));

,这是我从Shopify获得的非常不正确的响应。不仅不正确,而且无论我为查询传递什么SKU,它都会返回完全相同的产品数据。

screenshot of incorrect shopify response

解决方法

您正在将Admin GraphQL与店面GraphQL混淆。

query可用的Storefront GraphQL products参数是:

available_for_sale
created_at
product_type
tag
title
updated_at
variants.price
vendor

那里没有SKU。

您的屏幕快照似乎显示了与Admin GraphQL API(而不是店面GraphQL API)绑定的GraphiQL Shopify应用。

这就是为什么您得到相同结果的原因,因为查询只是被忽略了。