0
votes

Has anyone done a query for Articles that have metafields? I can query the articles with its title, otherfields etc, but by default Shopify won't return the articles metafields in this call. These metafields for these articles have been created by an app. My first goal would be trying to call all articles and view its metafields also.

Currently...

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

What I hope for ...

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
      ]
    }
  }
}
1

1 Answers

0
votes

Works exactly like described here:

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

if you want to query a certain meta field its

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

As of Nov 2020 it seems like metafield queries only work against the ADMIN GraphQL API. When I try them with the Storefront API it returns null for everything I query.

PS: Seems like you have to enable each metafield you want to access via Storefront: https://shopify.dev/tutorials/retrieve-metafields-with-storefront-api