0
votes

I am trying to retrieve the result from a graphQL query when the product is nil by using the productByHandle query, but it's erroring out.

My code in Ruby:

client = ShopifyAPI::GraphQL.client
 product_handle_query = client.parse <<-'GRAPHQL'
  query ($productHandle: String!) {
   productByHandle(handle: $productHandle) {
     id
     handle
     title
     tags
     productType
     vendor
   }
 }
 GRAPHQL
result = client.query(product_handle_query, variables: {productHandle: productHandle})
puts result.data.productByHandle

This should return nil, but when I try and message out, it returns with this error:

NoMethodError (undefined method `productByHandle' for #< productByHandle=nil>)

This query works fine in Insomnia, where the output looks like this:

{
"data": {
"productByHandle": null
},
"extensions": {
"cost": {
"requestedQueryCost": 1,
"actualQueryCost": 1,
"throttleStatus": {
"maximumAvailable": 2000.0,
"currentlyAvailable": 1999,
"restoreRate": 100.0
}
}
}
}

I have also tried changing from String! (which is not-null type which is what is returning) to String, but I get this error: GraphQL::Client::ValidationError (Nullability mismatch on variable $productHandle and argument handle (String / String!)) My variable for $productHandle is indeed a string, so not sure why this isn't working either.

Any help would be appreciated.

Thanks!