Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Find ids on empty product variant metafields

Find ids on empty product variant metafields

blackstonekey
Shopify Partner
1 0 0

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?

Reply 1 (1)

kjchabra
Shopify Partner
26 1 7

@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