Graphiql product queries with variables

Solved
ACrego
Excursionist
17 2 6

I'm having some success with queries FINALLY, after coming close to giving up several times over the last couple of weeks. 

 

I have learned how to build queries with variables, aliases, fragments, yay! 

 

However, I am still hitting a brick wall with one issue and I cannot find a solution, and I have searched the community here and I do not see where anybody has had this and shared the solution. 

 

When there is a schema like 

Shop.products

and its attributes are not available at the surface level; the only things available are things like 'first', 'after', 'last', things like that, and then there's a whole list of attributes - the ones I *really* want in my app search, nested in this 'query: String' attribute that must be dealt with. The schema from the Docs: 

 

query: String
Supported filter parameters:

barcode
created_at
delivery_profile_id
error_feedback
gift_card
inventory_total
out_of_stock_somewhere
product_type
published_status
sku
tag
title
updated_at
vendor

(Which brings up another very confusing thing, not as urgent and somewhat unrelated, but still very perplexing: WHY are THOSE supported parameters SO COMPLETELY DIFFERENT than the ones that appear in graphql when you press ctrl+spacebar? Hmmm?? I don't have most of those, and the ones I do have, are unsupported? I'll worry about that another day.)

 

So what I can't figure out is, how do you query MORE THAN ONE ATTRIBUTE of the shop's products in this query inside another query. 

 

HOW. How, in the name of all the planets, do you accomplish this? 

 

Graphql will NOT allow two 'query' parameters within the same ( ) after the main 'products' query. 

 

The following example works:

query($vendor: String){
   products(first:10, query:$vendor) {
    edges {
      node {
        id
        totalInventory
        handle
        vendor
        variants(first:3) {
          edges {
            node {
              id
              displayName
            }
          }
        }
      }
    }
  }
}

Query Variables
{
  "vendor": ""
}

However, what if I want to also query the 'handle' or any of the many other options available to 'products' not shown here because I have not yet added them to the query at this point? 

 

I already tried adding more attributes to 'query($vendor: String, $handle: String)' and 'products(first:10, query:$vendor, $handle)' and many other variations on that theme, but nothing is  workiiiiiiing! 😫😭

ACrego
Excursionist
17 2 6

This is an accepted solution.

OK, I solved it. 

 

I had to name the queries, and also name the queries (give them an alias) inside the queries! 

 

Amazing what a good night's sleep can do. 

Nathanhahaha
Shopify Partner
18 0 2

hey man, How did you solved it? can you share a code?

0 Likes
carch
Tourist
6 0 4

@ACrego yes please share the code to the solution! 

0 Likes
makasanas
Shopify Partner
5 0 0
body: JSON.stringify({
    query,
    variables: { dice, sides },
  })
 
0 Likes
Jroyce1180
Pathfinder
85 4 14

Let's see it! Save us the headache too  

0 Likes
originmaster
Tourist
4 0 3

For posterity here's how I solved it:

query productByTag($query: String!) {
    product: products(first: 50, query: $query) {
      edges {
        node {
          handle
        }
      }
    }
  }

 

and I passed in the query variable like this:

const someTag = 'some-product-tag'

const variables = {
   query: `tag:${someTag}`
}

 

Hope that helps

0 Likes