GraphQL query by SKU returning multiple products

GraphQL query by SKU returning multiple products

wmcelwee
Shopify Partner
2 0 6

We use a GraphQL call to retrieve products by their SKU. All of our product SKUs are unique. Some calls however will return multiple SKUs that are near matches. A Hyphen in the SKU seems to be the culprit on why this happens. Is there any way to check for exact matches only?

This is the GraphQL request structure I'm testing with via the GraphiQL App on our account.

{
      products(first: 10, query: "sku:SWS-OSCS-1-8") {
        edges {
          node {
            id
            variants(first: 1) {
              edges {
                node {
                  id
                  weight
                  sku
                }
              }
            }
          }
        }
      }
    }

 

So given we have these 3 SKUs

5012FH-Y
5012FH-Y-2PK
5012FH-Y-4PK

If I query with "sku:5012FH-Y", I get all 3 SKUs in the results. If I use 5012FH-Y-2PK, I get only the one matching product.

Now given the following query "sku:SWS-OSCS-1-8" is when I start to see some more unexpected behavior. Although with inspection, I can see a system to it. With this query I will get the following results:

SWS-OSCS-1-14-8
SWS-OSCS-1-38-8
SWS-OSCS-1-8 (the actual match)
SWS-OSCS-1-18-8
...

What I finally discovered is that the - almost acts like a range or wildcard. There can be characters in the "middle" of the SKU where a hyphen exists and it will return as a result. You can see for the given results they all have the same pattern of my qctual query with an additional [ -XX ] pattern. Several categories of our products have a similar SKU structure and are affected by this query. Is there a parameter in the request that I can add that will say I only want exact matches on the SKU? Or possibly is there a way to get the - to not be treated as a wildcard, or range type character?

The alternative is to read if we're getting multiple results from the query, loop through each of them until we verify that we found the correct product and proceed. Not all that difficult but certainly less efficient than just getting the expected result from the query in the first place if that is possible.

 

Replies 5 (5)

triple
Excursionist
12 0 10

Have you found a solution? I am having the exact same issue.

frogstack
Visitor
1 0 0

Also having the same problem. Is there really no solution for this? 

wmcelwee
Shopify Partner
2 0 6

I haven't, I just resorted to looping through the results and checking for an exact match. Feels like an oversight, would be nice to get an official answer.

Schmidtc63
Shopify Partner
100 14 27

What happens if you enclose the sku in single quotes?

 

"sku:'SWS-OSCS-1-8'"

Schmidtc63
Shopify Partner
100 14 27

So, I think the issue is that sku is not part of the product (the parent product, if you will), at least for graphql. You can't return the sku of a product. 

It looks like what graphql is doing is wilcard searching variant skus, getting the parent of a matching sku and returning what it finds.

 

If you're looking for an exact sku match, search the product variants:

query {productVariants(first: 1, query: "sku:aa-bb-cc-xxl") {
    edges { 
        node {
            id 
            weight  
            sku  
            availableForSale  
            compareAtPrice  
            displayName  
            harmonizedSystemCode   
            price  
            title 
            weight  
            weightUnit  
            contextualPricing(context: {country: CA}) { 
                price {
                    amount 
                    currencyCode
                } 
                compareAtPrice {
                    amount 
                    currencyCode
                }
             }
        }
    }
}}