Search code examples
jsonapinext.jsgraphqlshopify

Shopify GraphQL Storefront API Query Error


Hello I am a beginner with GrapQL/Shopify and have been following the documentation here. After trying to run a basic Storefront API call below to get information about a Collection

query getProductById($id: "gid://shopify/Product/xxxxxxxxx") {
  product(id: $id) {
    title
  }
}

I get the error below

{
  "errors": [
    {
      "message": "Parse error on \"gid://shopify/Product/xxxxxxxxx\" (STRING) at [1, 27]",
      "locations": [
        {
          "line": 1,
          "column": 27
        }
      ]
    }
  ]
}

I'm sure it's a syntax issue but the documentation isn't very clear on what the correct way of entering the query argument information is.

Thanks in advance


Solution

  • You should be passing the data type of the product ID where you're currently passing the product ID. In this case, the data type of the product ID is ID!

    It should look like this:

    query getProductById($id: ID!) {
      product(id: $id) {
        title
      }
    }
    

    The product ID is sent in the variables object which is separate to the query

    Shopify have a really useful GraphQL tool to help get a better understanding of how to query data from your store https://shopify-graphiql-app.shopifycloud.com/login