ArtDev
ArtDev

Reputation: 543

Can't Get Product Metafields using StoreFront GraphQL API

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!

Upvotes: 2

Views: 5368

Answers (2)

Yaroslav Draha
Yaroslav Draha

Reputation: 795

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:

Make metafields visible to the Storefront API

Upvotes: 2

David Lazar
David Lazar

Reputation: 11427

There are a couple of problems here.

  1. when you created your metafields, you had to assign them to a namespace. When you try to retrieve them with Storefront API, you have to provide this namespace, and you failed to do that.
  2. evem if you created metafields in the admin using some App, you will not see them using Storefront API unless you turn them ON using a special flag.

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.

Upvotes: 3

Related Questions