Solved

Impossible to search with a " symbol in SKU using GraphQL Admin API

angpro
Shopify Partner
10 1 1

Hi community,

 

Shopify allows " symbols in SKU:

CleanShot 2024-05-06 at 16.14.29@2x.png

 

But when I try to search for that SKU using GraphQL Admin API, it looks like that it does not allow " symbol in search condition:

CleanShot 2024-05-06 at 16.21.10@2x.png

CleanShot 2024-05-06 at 16.22.50@2x.png

 

 

 

When it works without " symbol:

CleanShot 2024-05-06 at 16.20.07@2x.png

 

 

Is there an elegant way to avoid this problem in general and using Remix template? 

 

Very much appreciate any help.

 

 

 

Accepted Solution (1)

PackLabelShip
Shopify Partner
36 1 0

This is an accepted solution.

You don't need to wrap the string after the sku in double quotes so this query should work:

 

 

query myQuery {
  productVariants(first: 1, query: "sku:AMS\"FG1WHCM") {
    edges {
      node {
        id
        title
      }
    }
  }
}

 

 

 

And for what it's worth you can also use unicode where " translates to \u0022 resulting in: 

 

query myQuery {
  productVariants(first: 1, query: "sku:AMS\u0022FG1WHCM") {
    edges {
      node {
        id
        title
      }
    }
  }
}

 

You could replace any " in the original string with \" or \u0022 explicitly, or find a more fully fledged solution in whatever programming language or tool you're using to make this request!

 

And finally if you really prefer to have quotes around the query you can just use single quotes to avoid the escaping nightmare:

query myQuery {
  productVariants(first: 1, query: "sku:'AMS\"FG1WHCM'") {
    edges {
      node {
        id
        title
      }
    }
  }
}

 

All three of the above gave me the same results when testing on my shop!

Putting Custom Packaging and Order Inserts back in the Merchant's hands!

View solution in original post

Replies 2 (2)

PackLabelShip
Shopify Partner
36 1 0

This is an accepted solution.

You don't need to wrap the string after the sku in double quotes so this query should work:

 

 

query myQuery {
  productVariants(first: 1, query: "sku:AMS\"FG1WHCM") {
    edges {
      node {
        id
        title
      }
    }
  }
}

 

 

 

And for what it's worth you can also use unicode where " translates to \u0022 resulting in: 

 

query myQuery {
  productVariants(first: 1, query: "sku:AMS\u0022FG1WHCM") {
    edges {
      node {
        id
        title
      }
    }
  }
}

 

You could replace any " in the original string with \" or \u0022 explicitly, or find a more fully fledged solution in whatever programming language or tool you're using to make this request!

 

And finally if you really prefer to have quotes around the query you can just use single quotes to avoid the escaping nightmare:

query myQuery {
  productVariants(first: 1, query: "sku:'AMS\"FG1WHCM'") {
    edges {
      node {
        id
        title
      }
    }
  }
}

 

All three of the above gave me the same results when testing on my shop!

Putting Custom Packaging and Order Inserts back in the Merchant's hands!
angpro
Shopify Partner
10 1 1

This looks great. Thank you!

 

I have to surround a SKU with " because a SKU may have a space besides " symbol: 

CleanShot 2024-05-07 at 19.44.30@2x.png

 

So looks like the third option works for me.

 

But there is an issue with the third option - it does not allow ' symbol 😅

 

Because I am construction a search condition with provided SKUs, I do not know exactly what is inside:

 

 

 

   const queryCondition = batch
        .map(
          (id: any) =>
            `${filterCheckedValue === "sku" ? "sku" : "barcode"}:\\"${id}\\"`
        )
        .join(" OR ");

 

 

 

 

 

So I am not sure whether there is a universal solution for any allowed for SKUs symbols.