GraphQL variables not working when getting order by name

Topic summary

Issue: A Shopify GraphQL query intended to fetch an order by its name using a variable returns the first order in the store instead of the matching order.

Context: The code defines a query with a $query variable (orders(first: 1, query: $query)) and sets variables to query: “name:2456”. The expectation is to retrieve the order whose name matches the provided value.

Attempted fix: A responder advised wrapping the order name in single quotes within the search string (e.g., name:‘Test#1234’), which aligns with Shopify’s search syntax where the query argument is a string filter. The original poster tried this but reported no change—still receiving the first order, not the filtered result.

Current status: Another participant reported experiencing the same behavior and asked if a resolution was found. No confirmed solution or follow-up steps were provided.

Notes: The code snippet is central to understanding the issue. Key terms: GraphQL (API query language), Shopify GraphQL API, and the orders query’s query parameter (a text-based search filter). The discussion remains unresolved.

Summarized with AI on December 11. AI used: gpt-5.

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?

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.

Hi @PurpleMamba ! I am facing the same issue. Did you get it resolved by any chance?