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.

GRAPHQL Admin API: Query products metafield references via bulk queries

Solved

GRAPHQL Admin API: Query products metafield references via bulk queries

Rawn
Shopify Partner
2 1 0

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
    }
  }
}

 

Accepted Solution (1)
Rawn
Shopify Partner
2 1 0

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

View solution in original post

Replies 2 (2)

SomeUsernameHe
Shopify Partner
519 58 111

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
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
Rawn
Shopify Partner
2 1 0

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