The GraphQL Admin API productVariants search is returning inconsistent results when combining an SKU filter with product_status.
I’ve also tested this across all currently supported versions of the Admin GraphQL API and the behaviour is the same.
Basic reproduction
If I search for an SKU by itself:
query GetProductVariant {
productVariants(
first: 12
query: "sku:EXAMPLE001"
) {
nodes {
id
sku
displayName
product {
status
}
}
}
}
Only variants with the exact SKU EXAMPLE001 are returned.
EXAMPLE001 - ARCHIVED
EXAMPLE001 - ARCHIVED
EXAMPLE001 - ACTIVE
Variants that have partial matching skus like these are not returned (even if we do first:12)
EXAMPLE001-PINK
EXAMPLE001-BLACK
EXAMPLE001-PINK-SALE
So far, everything looks correct.
Product Status changes search behaviour
Now if I run essentially the same query, but add:
product_status:active
So the query becomes:
query GetProductVariant {
productVariants(
first: 12
query: "sku:EXAMPLE001 product_status:active"
) {
nodes {
id
sku
displayName
product {
status
}
}
}
}
I start getting results like:
EXAMPLE001-PINK
EXAMPLE001-BLACK
EXAMPLE001
EXAMPLE001-PINK-SALE
EXAMPLE001-BLACK-SALE
This seems wrong to me.
The SKU portion of the search is exactly the same:
sku:EXAMPLE001
But simply adding product_status:active changes how that SKU filter behaves.
The search debug output also shows Shopify parsing the query as an AND:
{
"and": [
{
"field": "sku",
"match_all": "EXAMPLE001"
},
{
"field": "product_status",
"match_all": "active"
}
]
}
So I would expect the results to effectively be:
sku === EXAMPLE001
AND
product_status === active
Instead, the SKU search now appears to behave more like a partial/prefix match.
The bigger problem is result ordering
The exact SKU isn’t necessarily returned first either.
For example, the results can look like:
1. EXAMPLE001-PINK
2. EXAMPLE001-BLACK
3. EXAMPLE001
This becomes a much bigger issue when using:
productVariants(
first: 1
query: "sku:EXAMPLE001 product_status:active"
)
Our app was using this pattern to resolve the active variant for a known SKU.
Why I think this is a bug
These two queries contain the exact same SKU predicate:
sku:EXAMPLE001
and:
sku:EXAMPLE001 product_status:active
The first only returns exact matches.
The second suddenly returns partial/prefix matches.
If I wanted a prefix search, I would expect to explicitly use something like:
sku:EXAMPLE001*
Adding a product_status filter shouldn’t change the matching behaviour of the SKU filter itself.
Again, I’ve reproduced the same behaviour across all currently supported Admin GraphQL API versions.
Has anyone else run into this, or can someone from Shopify confirm whether this is expected behaviour?