How to query with variables Shopify API using shopify_api gem

Viewed 275

I have been struggling with this for a while, any help would be appreciated. So I am trying to query inventory items at a shopify store at a certain location. I am using the shopify_api gem. If I take out the variables and just hardcode a locationId at inventoryLevel, the code works just fine so I don't know where I am going wrong in the syntax. Below are some code snippets

def query_using_location_ids
      client.parse <<-'GRAPHQL'
      {
        query($locationId: ID!){
          inventoryItems(first:10){
            pageInfo{
              hasNextPage
              hasPreviousPage
            }
            edges{
              node{
                id
                legacyResourceId
                inventoryLevel(locationId: $locationId){
                  id
                  available
                  incoming
                  location{ id }
                  item{
                    id
                    legacyResourceId
                    sku
                    variant{
                      id
                      legacyResourceId
                      price
                      product{
                        description
                      }
                    }
                    unitCost{
                      amount
                      currencyCode
                    }
                  }
                }
              }
            }
          }
        }
      }
      GRAPHQL
end
 def client
   @client ||= ShopifyAPI::GraphQL.client
 end
 def fetch_by_location(id)
   client.query(query_using_location_ids, variables: {locationId: ID(id)}).original_hash["data"]["inventoryItems"]
 end

Error Produced:

GraphQL::ParseError: Parse error on "$" (VAR_SIGN) at [2, 15] from parser.y:527:in `on_error'

0 Answers
Related