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
}
}
}
}
}
}
}
}
}
}
}
`;
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”.
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
}
}
}
}