How to filter Metaobjects by its field values?

How to filter Metaobjects by its field values?

DSokolowski
Shopify Partner
9 0 3

Hi there, 

I have defined Metaobject definition with a string field named 'status' that takes values like active, inactive. Now let say I want to fetch only these metaobjects that are active. How can I achieve this. I do not see any documentation saying how to build graphql query variable to get what I want? Is this even possible?

 

I tried this and it does now work

 

metaobjects(first: 10, query: "status:active")  {
  ...
}

 

 

Replies 4 (4)

Liam
Shopify Staff
2873 312 821

Hi DSokolowski,

 

The example query you shared is correct for querying products or collections, but not for querying metafields specifically.

 

Unfortunately, the Shopify GraphQL API does not support filtering metafields by their values. You can only retrieve metafields by their namespace and key, or by their owner resource. Once you have retrieved the metafields, you will need to filter them on your end.

 

Here is an example of how you can retrieve metafields for a product:

{
  product(id: "gid://shopify/Product/1") {
    metafields(first: 10) {
      edges {
        node {
          id
          namespace
          key
          value
        }
      }
    }
  }
}

You can replace "Product/1" with the ID of your product. This will return the first 10 metafields for that product. You can then filter through these metafields in your application to find those with the key "status" and the value "active".

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

DSokolowski
Shopify Partner
9 0 3

This is bad. My goal is to display a list of metaobjects with ability to filter by status etc. If what you wrote is true I can not do it using graphql API because filtering by metafields values is not supported yet. 

You suggest to filter them on my end but in order to do it I need to fetch all metaobjects which requires to use bulk query which wont work for me also. This is how my bulk query looks like 

mutation {
  bulkOperationRunQuery(
   query: """
    {
      metaobjects(type: "SOMETYPE")  {
        edges {
          node {
            id
            fields {
              key
              reference
              type
              value
              references(first: 10) {
                edges {
                  node {
                    ... on Product {
                      id
                      handle
                      title
                      featuredImage {
                        url
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    """
  ) {
    bulkOperation {
      id
      status
    }
  }
}

 

When I run it I get error 

Queries that contain a connection field within a list field are not currently supported.

This is because list field :fields contains connection field :references. I need these references since it holds informations about products related to a metaobjects which is needed to display image etc. 

 

Do you know if there is a plan to add a support for queries that contain a connection field within a list field in near future?

noliver
Shopify Partner
3 0 3

@Liam In order to make metaobjects viable for flexible data storage, there needs to be a way to filter by value. Are you able to put in a feature request?

hardik_satasiya
Shopify Partner
1 0 3

Hmm it seems at the moment Shopify only supports 2 fields for queries in Metaobjects

 

Screenshot 2023-10-06 at 1.04.59 PM.png

  • display_name
  • updated_at

Ref: https://shopify.dev/docs/api/admin-graphql/2023-01/queries/metaobjects

 

So, it seems right now it's not possible to filter based on other field value.