使用Shopify JavaScript Buy SDK自定义查询检索文章对象,包括其图像

使用Shopify JavaScript Buy SDK自定义查询检索文章对象,包括其图像,javascript,shopify,shopify-javascript-buy-sdk,Javascript,Shopify,Shopify Javascript Buy Sdk,我正在使用shopify buy SDK尝试在前端使用JavaScript从我的shopify商店获取文章,请遵循此处的“扩展SDK”说明: 使用下面的代码,我能够检索我的文章和我需要的一些字段 // Build a custom query using the unoptimized version of the SDK const articlesQuery = client.graphQLClient.query((root) => { root.addConnection('a

我正在使用shopify buy SDK尝试在前端使用JavaScript从我的shopify商店获取文章,请遵循此处的“扩展SDK”说明:

使用下面的代码,我能够检索我的文章和我需要的一些字段

// Build a custom query using the unoptimized version of the SDK
const articlesQuery = client.graphQLClient.query((root) => {
  root.addConnection('articles', {args: {first: 10}}, (article) => {
    article.add('title')
    article.add('handle')
    article.add('url')
    article.add('contentHtml')
  })
})

// Call the send method with the custom query
client.graphQLClient.send(articlesQuery).then(({model, data}) => {
  console.log('articles data')
  console.log(data)
})
然而,我也确实需要为每篇文章提取特色图片,不幸的是,当我在我的articlesQuery中添加行
article.add('image')
,结果文章数据日志
null
。我尝试构建一个定制productsQuery,但也有一个类似的问题-我可以检索一些product字段,但是当我尝试添加行
product.add('images')
,我只是从storefront API中返回
null

是否有人具有构建自定义/扩展查询并成功检索图像的经验?

请尝试以下操作:

// Fetch all products in your shop
client.graphQLClient.fetchAll().then((acticles) => {
  console.log(acticles);
});

然后在控制台中检查您的文章具有哪种类型的可用属性名称。如果SDK允许您获取任何图像数据,那么肯定会有类似于
imageSrc
|imageUrl| |
img

的内容,感谢Rebecca Friedman在js buy SDK repo的github问题部分提供了此工作解决方案:

const articlesQuery = client.graphQLClient.query((root) => {
  root.addConnection('articles', {args: {first: 10}}, (article) => {
    article.add('title')
    article.add('handle')
    article.add('url')
    article.add('contentHtml')
    article.addField('image', {}, (image) => {
      image.add('id')
      image.add('originalSrc')
    })
  })
})

// Call the send method with the custom query
client.graphQLClient.send(articlesQuery).then(({model, data}) => {
  console.log('articles data')
  console.log(data) // works!
})


因为字段是它自己的对象,所以必须添加回调函数来指定所需的字段

您是否可以添加
console.log(data)
的输出?是否尝试了
img
?如果返回null,则项目没有您尝试访问的图像或属性名称不存在。你在任何地方找到了正确的关键词来检索图像,或者在文章中添加了
image
,这是你的想法吗?