Why is my GraphQL query failing?

For some reason, I cannot seem to query for variants on a collection.
Here is my query:

query COLLECTION_QUERY($after: String, $before: String, $filters: [ProductFilter!], $first: Int, $handle: String, $last: Int, $reverse: Boolean, $sortKey: ProductCollectionSortKeys) {
  collection(handle: $handle) {
    id
    image {
      id
      altText
      height
      url
      width
    }
    products(
      after: $after
      before: $before
      filters: $filters
      first: $first
      last: $last
      reverse: $reverse
      sortKey: $sortKey
    ) {
      pageInfo {
        endCursor
        hasNextPage
        hasPreviousPage
      }
      edges {
        node {
          id
          variants {
            nodes {
              id
              compareAtPrice {
                amount
                currencyCode
              }
            }
          }
        }
      }
    }
    title
  }
}

You can test it out here:
https://shopify.dev/graphiql/storefront-graphiql

In my testing I was entering this into the “query variables” section, but it doesn’t seem to matter too much, it was consistently failing for me:

{
  "first": 20,
  "handle": "latest-stuff"
}

Hi @lukebennett

The first thing I would say is you can’t use the “collection” query to search for a collection by handle.

For that, you’ll need the collectionByHandle query instead.

Then in the products query, you’re using a lot of variables that you’re not passing in:

 products(
   after: $after
   before: $before
   filters: $filters
   first: $first
   last: $last
   reverse: $reverse
   sortKey: $sortKey
)

I’d look into the documentation for the products query as well and look at what you’d actually like to filter by or which variables you’d need to use.

The first thing I would say is you can’t use the “collection” query to search for a collection by handle.

Thank you for trying to help, but this isn’t actually true. You might be familiar with an older version of the API. If you click on this link to the storefront GraphQL explorer, you will see that using the collection query I posted above, does in fact let you query for a collection by it’s handle. In fact, if I try to use collectionByHandle, you’ll a see message in the explorer that that API has been deprecated.

Here is a stripped back query showing you that the query works:

query COLLECTION_QUERY {
  collection(handle: "latest-stuff") {
    id
    products(first: 20) {
      edges {
        node {
          id
          # everything _except_ variants seems to work here
        }
      }
    }
    title
  }
}

Then in the products query, you’re using a lot of variables that you’re not passing in:

That is true, but I need those variables in the real query. This was just a simplified version.

Hi @lukebennett

You’re quite right - the up-to-date API does include the ability to query collections by handle - good spot!

Variants does seem to work in there for me, but I had to include a first argument to get it to work, e.g:

query COLLECTION_QUERY {
  collection(handle: "latest-stuff") {
    id
    products(first: 20) {
      edges {
        node {
          id
          variants(first:20) {
            edges {
              node {
                id
              }
            }
          }
          # everything _except_ variants seems to work here
        }
      }
    }
    title
  }
}

That gave me a list of products and subvariants.

Without the “first” argument in the variants tag it just gave me an error but not any useful feedback on how to correct it.

1 Like

I wasn’t setting the first param on variants ?‍ :male_sign:
Thanks so much, that was it! For some reason now I’m actually getting a useful error when I leave first out (previously it said something along the lines of there was a problem on the backend).