A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I'm trying to look up an order by name, this is my code
# Get an order by order name field
query = ShopifyAPI::GraphQL.client.parse <<-'GRAPHQL'
query($query: String) {
orders(first: 1, query: $query) {
edges {
node {
id
name
createdAt
}
}
}
}
GRAPHQL
variables = {
query: "name:2456"
}
result = ShopifyAPI::GraphQL.client.query(query)
It should return the order passed in the variables, but instead returns the very first order in the database.
What am I doing wrong here?
Solved! Go to the solution
This is an accepted solution.
I got it working. It wasn't the single-quotes. That actually had no effect which told me something else was wrong.
I had to pass in the variables in the result line, like this:
# Get an order by order name field
query = ShopifyAPI::GraphQL.client.parse <<-'GRAPHQL'
query($query: String) {
orders(first: 1, query: $query) {
edges {
node {
id
name
createdAt
}
}
}
}
GRAPHQL
variables = {
query: "name:2456"
}
result = ShopifyAPI::GraphQL.client.query(query, variables: variables)
And now it works! Thanks for taking the time to try and help me out with this!
You need to wrap your name in single quotes to get that to work.
{
"query": "name:'Test#1234'"
}
Works for me perfect.
Hi @HunkyBill thanks for answering!
I tried that, and it didn't change the result unfortunately. Still not getting back the queried order.
Dude! I hate to break it to you, but that is it. That is the answer. If that is not working for you, to retrieve a named order, then you are doing something else wrong. I know it seems improbable.
- you either messed up your quotes
- you are messing something else up.
I battle-tested this with orders, including some just now. It works perfectly and cherry-picks the exact correct order. I am so sorry for your loss! I learned this many moons ago, dealing with orders, and dates, and comparisons, and how to get unfulfilled orders between two dates. Surround your data in single quotes.
This is an accepted solution.
I got it working. It wasn't the single-quotes. That actually had no effect which told me something else was wrong.
I had to pass in the variables in the result line, like this:
# Get an order by order name field
query = ShopifyAPI::GraphQL.client.parse <<-'GRAPHQL'
query($query: String) {
orders(first: 1, query: $query) {
edges {
node {
id
name
createdAt
}
}
}
}
GRAPHQL
variables = {
query: "name:2456"
}
result = ShopifyAPI::GraphQL.client.query(query, variables: variables)
And now it works! Thanks for taking the time to try and help me out with this!
Yes.. I just wrote you that too... trust me, you'll not want to forget these single-quotes at times, because while they may not always help sometimes they are needed, and their presence does not hurt.
A quick perusal of your Ruby and I can see how this does not work for you. I would not setup my query like you. I pass on variables as a second parameter.
I keep my GQL in a nice OO file, where I can instantiate each one as needed, and then pass in my variables...
query = Queries::GetOrderByName.query
payload = {
query: "name:'test#5522'"
}
result = ShopifyAPI::GraphQL.client.query(query, variables: payload)
This works time and time again, perfect...
That's a much cleaner way of doing it for sure. I'll have to refactor for a similar set up.
Thanks again!