PSA for Devs: JS String interpolation for GraphQL queries

Tourist
9 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
105 4 32

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

 

Thanks for the heads up!

Building custom, public-facing Shopify Apps for various niches. Ping me with your app ideas!

My Apps:
* Countries We Ship To Button (Free!) - Show your customers where you ship to with a crisp button + pop-up!


My Tutorials:
* Get Notified When New Comments Are Added To Your Shopify Blog (Free!) - A quick & easy tutorial to set up a free web-service that e-mails you when you get a new comment on your Shopify blog!
1 Like