Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
I have a GraphQL query that returns a variant's metafields.
{
productVariant(id: "gid://shopify/ProductVariant/123456789") {
title
metafields(namespace: "custom", first: 10) {
nodes {
id
key
type
value
}
}
}
}
This works fine, but it will only return metafields that contain values. I am new to GraphQL but I understand this may be because the fields are nullable.
I need to find the ids of all the metafields on a given variant - How can I achieve this?
@blackstonekey You can get all Metafields associated to Product Variants data by running the following query:
query MyQuery {
metafieldDefinitions(ownerType: PRODUCTVARIANT, first: 100) {
nodes {
id
key
name
visibleToStorefrontApi
namespace
type {
name
category
}
}
}
}
This will output the first 100 metafield definitons associated to product variants. You can then use your variant metafields query to get all possible values associated with the variant. Once you get both queryies, you can then filter for definitions that don't have values for the selected variant.
Metafield Defintion Doc