Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hi, i am trying to query all my products with all corresponding metafields. When i try to create new bulk query operation I get response with error "Selections can't be made directly on unions (see selections on MetafieldReference)". How can i get my products and metafields linked together?
I am using GraphQL Admin API.
Thanks for any help.
Here asi my query:
mutation { bulkOperationRunQuery( query: """ { products { edges { node { title id metafields { edges { node { id key value references { edges { node { id ... on Metaobject { type fields { key value } } } } } } } } } } } } """ ) { bulkOperation { id status } userErrors { field message } } }
Solved! Go to the solution
This is an accepted solution.
What you posted is still querying metafields.references.edges[N].node which is still union (of Metaobject | ProductVariant | Product | OrWhatever). Also this is normal query, and I need bulk query
UPDATE:
Oh i didnt get it when i wrote this reply.. 😄
You just cant use or query any field outside the specific case of the union. You have to always specify the case you need and then query the fields you need. Even if it means some repetition.
so my query should look like this:
mutation {
bulkOperationRunQuery(
query: """
{
products {
edges {
node {
title
id
metafields {
edges {
node {
id
namespace
key
value
references {
edges {
node {
... on Metaobject {
id
type
fields {
key
value
}
}
}
}
}
}
}
}
}
}
}
}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
In my case i just moved "id" field to "... on Metaobject" union case.
Thanks again @SomeUsernameHe for your help
You cannot use selections directly on unions in GraphQL. Instead of directly querying the references field on Metafield, you might need to structure your query to handle the specific types of references you expect to return using GraphQL fragments or a similar approach.
For example:
query {
products(first: 5) {
edges {
node {
id
title
metafields(first: 5) {
edges {
node {
key
value
type
# Include fragment for handling Metafield references
references(first: 5) {
edges {
node {
... on ProductVariant {
id
title
}
... on Product {
id
title
}
# Add more fragments as needed for other types of references
}
}
}
}
}
}
}
}
}
}
This is an accepted solution.
What you posted is still querying metafields.references.edges[N].node which is still union (of Metaobject | ProductVariant | Product | OrWhatever). Also this is normal query, and I need bulk query
UPDATE:
Oh i didnt get it when i wrote this reply.. 😄
You just cant use or query any field outside the specific case of the union. You have to always specify the case you need and then query the fields you need. Even if it means some repetition.
so my query should look like this:
mutation {
bulkOperationRunQuery(
query: """
{
products {
edges {
node {
title
id
metafields {
edges {
node {
id
namespace
key
value
references {
edges {
node {
... on Metaobject {
id
type
fields {
key
value
}
}
}
}
}
}
}
}
}
}
}
}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
In my case i just moved "id" field to "... on Metaobject" union case.
Thanks again @SomeUsernameHe for your help