We’re currently using the Shopify Storefront API to load shop data (products, collections, metaobjects, etc.). We’ve configured additional specifications for each product in category metafields (namespace shopify, key ?, etc.).
Now we need to:
Fetch only those category metafields that are set on a given product
Display their values in our storefront
We only rely on the Storefront API in our storefront, not the Admin API, and would like to keep it that way if possible.
Does anyone have a solution for this issue?
Any help would be greatly appreciated!
Hey @sara - this is a little tricky, but you should be able to run something like this via the storefront API, as long as you know the namespace/key of each metafield you want to return for a specific product:
{
product(handle: "test") {
id
title
# Query each category metafield individually
categorySpec1: metafield(namespace: "custom", key: "test") {
value
type
}
categorySpec2: metafield(namespace: "shopify", key: "category_spec_2") {
value
type
}
categorySpec3: metafield(namespace: "shopify", key: "category_spec_3") {
value
type
}
}
}
It’s not the most elegant for sure, since you technically still have to query each specific category metafield, on a given product, but by filtering the handle of the product you can narrow down the query to a single product and then display the metafield value as needed. You could theoretically combine this with a quick post-processing script to just pull the value for each metafield as needed, but this should help with getting started.
Let me know if I can clarify anything more on my end here - hope this helps!
Our issue is that each product may have different category metafields defined, and we’re only interested in those in Shopify’s built-in shopify namespace.
Is there really no way to tell the Storefront API to return all metafields from a given namespace in one go - for example via regex or some built-in filter?
Another challenge I’m running into is rendering the actual metafield values. When I query a specific metafield, the Storefront API often returns only the metaobject IDs, forcing me to issue a follow-up request to fetch the full object values. Is there a way to simplify this - perhaps by expanding or including the metaobject data in the initial query?
Hey @sara no worries, happy to help - you’re correct here. At the moment, the Storefront API unfortunately doesn’t support filtering metafields by namespace only. I do know that this is a fairly popular feature request though, and I’m happy to pass along your feedback so that we can look into enabling this in the future for sure (I’ll do that right away!)
I did a bit of testing on my end as well, and I wasn’t able to replicate what you were seeing with the metaobject value only being shown and not the metafield in the API response. Here’s a quick example I did on my test shop using the Storefront API:
There, I did get the metafield ID, just wondering if you’d be able to share the query you’re making so that I can try to replicate on my end (if you’re open to sharing your shop ID/details as well, I can replicate things internally there on my end too - happy to set up a DM with you if needed!)
Hope to hear from you soon, happy to dig into this further for sure.