How to query metaobject references within graphql metafield queries

Topic summary

The discussion centers on querying metaobject references within Shopify GraphQL metafield queries, with users sharing solutions and troubleshooting common issues.

Initial Solution:
Celso_White provides a working GraphQL example using references(first: 10) to retrieve metaobject data from metafield lists.

Common Problems & Solutions:

  • Null references: Multiple users report getting null values when querying references. FutureAndAHope identifies that custom metafields must be exposed via metafieldStorefrontVisibilityCreate mutation in the Admin API before they can be accessed.
  • Shopify Functions limitation: In Shopify Functions, the references field appears unavailable, with metaobjects only accessible as stringified GID arrays in the value field.
  • Query syntax issues: st_consult suggests removing the key parameter from certain queries to resolve errors.

Critical Distinction:
ScottP99 highlights an important configuration detail: use reference (singular) for single-entry metafields versus references (plural) for list-type metafields. This metafield definition setting determines the correct query structure.

The thread remains active with users confirming solutions, particularly around the visibility permissions requirement.

Summarized with AI on October 25. AI used: claude-sonnet-4-5-20250929.

Here is an example showing how to use graphql to get metaobject reference data when stored in a metafield list.

features: metafields(identifiers: [ 
 {namespace: "custom", key: "features"}
]){
    namespace
    key
    references(first: 10) {
      edges {
        node {
          ... on Metaobject {
            fields {
              key
              type
              value
            }
          }
        }
      }
    }
  }

Hi, I try to use GraphQL with metaobjects and see your solution. But it doesn’t work for me, I always get ‘null’ in references, what I did wrong?

Hey Celso,

I am trying to find a way to query a product by it’s ID, and then return the values for a metaobject reference in one of its metafields. I have the following structure, but it doesn’t seem to be working.

const productIds = `["gid://shopify/Product/7573912289465"]`;

const productQuery = () => `
  query {
    node(id: "${productId}") {
      ... on Product {
        id
        title
        handle
        tags
        metafields(first: 10, namespace: "custom", key: "disc_model") {
          edges {
            node {
              namespace
              key
              type
              value
              references(first: 10) {
                edges {
                  node {
                    ... on Metaobject {
                      id
                      handle
                      type
                      updatedAt
                      fields {
                        key
                        type
                        value
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
`;
1 Like

We are attempting to do the same thing in a Shopify function, but there only seems to be a value field (which is a string) i.e.

 "value": "[\"gid://shopify/Metaobject/3110174995\",\"gid://shopify/Metaobject/3110207763\",\"gid://shopify/Metaobject/3110240531\"]"

The following is not working:

query Input {
  cart {
    lines {
      quantity

      discount: attribute(key: "dis"){
         key
         value
      }

      merchandise {
        __typename
        ...on ProductVariant {
            id
            product {
                binding: metafield(namespace: "test_data", key: "binding_mount") {
                    value

                }
                length: metafield(namespace: "test_data", key: "snowboard_length") {
                    value

                }

                quantity_price_breaks: metafield(namespace: "custom", key: "quantity_price_breaks"){

                    
                    references(first: 5) {
                        edges {
                            node {
                                ... on Metaobject {
                                    fields {
                                        key
                                        type
                                        value
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }

      }
    }
  }
}

Giving the error: Error 1: Cannot query field “references” on type “Metafield”.

1 Like

It may be that you have not exposed the custom field. You have to give permission to access the field.

mutation {
  metafieldStorefrontVisibilityCreate(
    input: {
      namespace: "custom"
      key: "features"
      ownerType: PRODUCT
    }
  ) {
    metafieldStorefrontVisibility {
      id
    }
    userErrors {
      field
      message
    }
  }
}

That must be ran in the Admin API. Once you have run it it will expose the custom field.

Have you been able to solve this? We are having the same issues.

Worked for me when leaving out the part

, key: "disc_model"

in your query above.

One thing to be careful with that caught me out. When you setup the metafield definition it’s important if it’s set as one entry or list of entries as it determines whether you use the field reference or references.

If one entry then this works…

features: metafields(identifiers: [ 
 {namespace: "custom", key: "features"}
]){
    namespace
    key
    reference {
       ... on Metaobject {
          fields {
            key
            type
            value
          }
       }
    }
  }

God thank you so muchm I spent 3 hours trying to figure out why I was getting null for references in a product metafield I needed access to :sob::sob::sob: