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

Jam*_*ton 5 javascript shopify shopify-javascript-buy-sdk

我正在使用shopify-buy SDK尝试从我的Shopify商店中获取文章,只需使用前端的JavaScript,按照"扩展SDK"的说明进行操作:https://shopify.github.io/js-buy -sdk/#extended-the-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)
})
Run Code Online (Sandbox Code Playgroud)

但是,我真的需要为每篇文章提取特色图像,不幸的是,当我article.add('image')在articlesQuery中添加该行时,生成的文章数据日志null.我尝试构建自定义productsQuery,这有一个类似的问题 - 我可以检索一些产品字段,但是当我尝试添加该行时product.add('images'),我只是null从店面API返回.

有没有人有建立自定义/扩展查询和成功检索图像的经验?

Jam*_*ton 1

感谢 js-buy-sdk 存储库的 github 问题部分的 Rebecca Friedman 提供了这个可行的解决方案:

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!
})

Run Code Online (Sandbox Code Playgroud)

因为图像字段是它自己的对象,所以你必须添加一个回调函数来指定你需要的字段。