I am using storefront API for my Shopify store.
And need to retrieve product metafields for my product.
I already added metafields using 3rd party app(Metafields Guru).
I need to get all those metafields for product, and had been following this article.
https://shopify.dev/tutorials/retrieve-metafields-with-storefront-api
{
productByHandle(handle: "xxx-handle") {
id
title
handle
productType
metafields(first: 5) {
edges {
node {
key
value
}
}
}
}
}
But I get an empty array for metafields.
Not sure what I am missing here.
Any help would be appreciated
2
Answers
There are a couple of problems here.
So that is likely a missed step too. Read up on this and pay attention to the MetafieldStorefrontVisibility. You set the namespace and key to be visible. Meaning you likely will not be creating metafields willy nilly without careful consideration about turning each one on or off to Storefront API access.
I would extend a bit David`s answer as I cannot add comments to other answers yet.
Indeed, you have to make the metafields explicitly visible to Storefront API by running mutation MetafieldStorefrontVisibility. Here are some useful links:
Also, I found a nice app Accentuate Custom Fields that provides UI options for making metafields visible to the Storefront API while configuring the metafields: