Search code examples
graphqlshopifye-commerceshopify-apishopify-storefront-api

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!


Solution

  • 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.