Is there a way to get products where a metafield (type list.metaobject_reference) type is empty/null?
Hi,
I want to query products that have null values in a metafield (type list.metaobject_reference). I can query the products where the metafields contains a specific gid without problem. However, if I try
query: "-metafields.mynamespace.producttype:*"
OR
query: "metafields.mynamespace.producttype:*"
OR
query: "-metafields.mynamespace.producttype:\"\""
OR
query: "metafields.mynamespace.producttype:\"\""
All of those queries will return null and non-null
_
here is a full example : the query
query FilterProductsByEmptyMetafield {
products(first: 100, query: "-metafields.mynamespace.producttype:*") {
edges {
node {
id
title
metafield(namespace: "mynamespace", key: "producttype") {
key
value
}
}
}
}
}
it returns this (one where metafield is null, and the other that the metafield is not null)
{
"data": {
"products": {
"edges": [
{
"node": {
"id": "gid://shopify/Product/7882997334131",
"title": "test1",
"metafield": null
}
},
{
"node": {
"id": "gid://shopify/Product/7891930349683",
"title": "test2",
"metafield": {
"key": "producttype",
"value": "[\"gid://shopify/Metaobject/122493960307\"]"
}
}
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 29,
"actualQueryCost": 14,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1986,
"restoreRate": 100
}
},
"search": [
{
"path": [
"products"
],
"query": "-metafields.mynamespace.producttype:*",
"parsed": {
"field": "metafields.mynamespace.producttype",
"exists": true
}
}
]
}
}
Thank you very much
1 Like
Hey @MisterBoy,
Can you run that query again and add the query debugging headers. This will return the details on how your query was parsed. In most cases this can be used to identify issues where your query isn’t being applied as expected.
To receive the search extension for all queries with and without warnings, you can send a Shopify-Search-Query-Debug=1
header to enable it manually.
Hi,
thank you for your help.
in graphiQL I added this in the headers section
{
"Shopify-Search-Query-Debug": 1
}
I reran all of those query, and they all return the same result. Here is the result for the last query. I don’t see anything different with and without the header. I also tried to change the Shopify-Search-Query-Debug value to 0, 1,2 and I see nothing different
{
"data": {
"products": {
"edges": [
{
"node": {
"id": "gid://shopify/Product/7882997334131",
"title": "test1",
"metafield": null
}
},
{
"node": {
"id": "gid://shopify/Product/7891930349683",
"title": "test2",
"metafield": {
"key": "producttype",
"value": "[\"gid://shopify/Metaobject/122493960307\"]"
}
}
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 20,
"actualQueryCost": 12,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1988,
"restoreRate": 100
}
},
"search": [
{
"path": [
"products"
],
"query": "metafields.xpav.producttype:\"\"",
"parsed": {
"field": "metafields.xpav.producttype",
"match_phrase": ""
}
}
]
}
}
I feel like it behave similar to when I try to query an unfiltrable metafield like a decimal metafield. It would be easier to understand if it would return no results instead of all products.
I believe this is simply not well supported for type list.metaobject_reference.
I decided to go a different path
I was using this query in shopify flow, I changed my query to get the most recent 100 product and used a run code to count product that didn’t have a metaobject in a specific field, then a condition if that count is bigger than 0, send an internal email where I added liquid code to only show products where that field is empty.
{% for getProductData_item in getProductData %}
{% assign productContainProductType = true %}
{% for value_item in getProductData_item.producttype.value %}
{% assign productContainProductType = true %}
{% endfor %}
{% if productContainProductType %}
{% for value_item in getProductData_item.relatedproducts.value %}
[ {{value_item.producttypename}} ]
{% endfor %}
{% endif %}
{% endfor %}
one other way if you are using the query in a coded software, would be pretty easy to simply filter the result of the query, that is not very efficient, but it would doable.
I think the type list.metaobject_reference is not well supported all around.