Graphql Shopify |查询带有其元字段的文章

问题描述

有人对具有元字段的文章进行查询吗? 我可以查询标题,其他字段等的文章, 但认情况下,Shopify不会在此调用中返回文章元字段。这些文章的这些元字段是由一个应用程序创建的。我的第一个目标是尝试调用所有文章并查看其元字段。

当前...

# My Graphql
{
    articles(first:2){
        edges{
            node{
                title
                excerpt
            }
        }
    }
}
# Json Response
{
  "data": {
    "articles": {
      "edges": [
        {
          "node": {
            "title": "First Post","excerpt": ""
          }
        },{
          "node": {
            "title": "Second Post","excerpt": ""
          }
        }
      ]
    }
  }
}

我希望......

My Graphql:
{
    articles(first:2){
        edges{
            node{
                title
                excerpt
                Metafields
            }
        }
    }
}
# Response
{
  "data": {
    "articles": {
      "edges": [
        {
          "node": {
            "title": "First Post","excerpt": "","Metafields":[{
                "namespace":"mynamespacehere","key":"mykey","value":"Some Value"
                }

            ]
          }
        }
        ##...all other posts
      ]
    }
  }
}

解决方法

工作原理与此处所述完全相同:

https://shopify.dev/docs/admin-api/graphql/reference/metafields/metafield

如果您要查询某个元字段 其

fieldName: metafield(namespace: "YOUR_NAMESPACE",key: "YOUR_KEY"){
     value
}

从2020年11月开始,似乎元字段查询只能在ADMIN GraphQL API上运行。当我尝试使用Storefront API进行操作时,它对我查询的所有内容均返回null。

PS:似乎您必须启用要通过Storefront访问的每个元字段: https://shopify.dev/tutorials/retrieve-metafields-with-storefront-api