PSA for Devs: JS String interpolation for GraphQL queries

Highlighted
Tourist
10 0 5

Hopefully this post helps other app devs avoid a world of pain (would have saved me a bunch of time).

 

If you use string interpolation to form your GraphQL queries, here is a truncated example of the WRONG way to do it (where Constants.namespace is a regular string):

 

const gqlquery = gql`metafields (first: 1, reverse: true, namespace: ${Constants.namespace}) {
          edges {
            node {
              namespace
              description
              key
              value
            }
          }
        }
      }`

 

Here's the RIGHT way (you need to make sure that the variable referenced has double quotes before and after for proper GraphQL syntax):

 

const gqlquery = gql`metafields (first: 1, reverse: true, namespace: "${Constants.namespace}") {

 

Adam Tzagournis
Sound Onyx Inc
https://apps.shopify.com/partners/sound-onyx-inc
1 Like
Shopify Partner
116 4 43

Oh snap. That's a rough one....

 

Thanks for the heads up!

Freshly-baked Shopify Tutorials, Tips & Tricks. I also build Shopify Apps!
My Tutorials:
* Get Notified When New Comments Are Added To Your Shopify Blog (Free!)
1 Like