GraphQL string interpolation in query

Highlighted
New Member
1 0 0

Hello - 

 

I am trying to implement a very simple GraphQL operation using a string variable to query on products with a title containing said string. Here's what I have in GraphiQL:

query searchProductTitle($x: String) {
  products(first:10, query:"title:$x*") {
    edges {
      node {
        id,
        title,
        productType
      }
    }
  }
}

{
"x": "shirt"
}

 

What is the proper way to interpolate my variable within the query string argument? 

 

Thank you

0 Likes
Highlighted
Shopify Staff
Shopify Staff
438 90 66

Hi @mmkk20 

 

With GraphQL queries, you will provide most of the dynamic arguments for your query by utilizing variables. Rather than writing the query in a way that needs to be interpolated, consider the proper use of variables instead. Here is an example: 

 

query ($input: String!){
customers(first: 100, query: $input) {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
node {
id
displayName
createdAt
state
}
cursor
}
}
}
{
"input": "state:enabled"
}
If needed, you can update the JSON variable as you wish with your content rather than in your query. 

Vix | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 


0 Likes