GraphQL search for products based on description

Solved
find-replace
Tourist
5 0 1

Is there anyway to use graphQL to search for products with a description that contains a string. I tried this, but it does not work.

 

{
  products(query:"description:*Black*" first:5 ) {
    edges {
      node {
        id
        title
        description
      }
    }
  } 
}
Zameer
Shopify Staff
Shopify Staff
295 32 76

This is an accepted solution.

Hey there,

 

The GraphQL docs show what filter parameters you can use when querying products.

 

Unfortunately, `description` isn't an option, but you can use the product `title` or `tag` amongst a list of other properties.

ACrego
Excursionist
17 2 6

I was so happy when I searched the community and saw that someone had the exact same question that I am struggling with right now, and there was an answer. 

 

But when I went to the link provided, :( I feel more sad than ever, because I do not understand how to implement the solution AT. ALL. 

 

I already know that the "products" object has within it, the properties, for example, of "title", "vendor" that I want to be able to use as variables in queries. 

 

But the query I have is written like this: 

 products(first: 20, query: here?) {
    edges {
      node {
        title
        description
      }
    }
  }

I don't understand where the query goes inside the query??? How do you write it? There's a list of supported filter parameters, but what about the other product attributes? Just one example would have been nice... 

 

Sorry, just frustrated... :(  I tried lots of different ways but nothing works. 

 

This page and this page are just mocking me at this point. I know *what*, I just don't know *how*. And they shouldn't title that one page "Syntax" when nowhere anywhere on the whole page does it show any examples of how to actually use it in a functional use case. There is no larger context whatsoever.

 

Zameer
Shopify Staff
Shopify Staff
295 32 76

 

Hey @ACrego ,

 

When using the `query` parameter in GraphQL there are a few things to keep in mind.

 

First, the statement needs to be a string. Second, you use one of the supported `filter parameters` shown in the GraphQL object documentation. Third, you use one of the `comparators` shown in the Search Syntax documentation. Lastly, you follow it with whatever value you're interested in looking for.

 

As an example, if you wanted to find the ids and titles of all the products in your store that included the word `Shirt` in the title, you would have:

{
  products(first: 20, query: "title:Shirt") {
    edges {
      node {
        id
        title
      }
    }
  }
}

If instead, you wanted to find the ids and descriptions of all the products in your store that exactly matched the `Plaid Shirt` title, you would have:

{
  products(first: 20, query: "title:'Plaid Shirt'") {
    edges {
      node {
        id
        description
      }
    }
  }
}

This can then be combined with additional logical terms such as AND, or OR to make more complex queries. You can also use prefix queries with `*` that allows you to search for products that begin with a certain string. 

 

Hopefully, this helps you configure the queries you're looking for!

Raj3
Shopify Partner
3 0 0

Hey @Zameer, is there any documentation for search query parameters? For example I do not know what's the difference between `publishable_status` and `published_status` what are valid options for them in products search query.

0 Likes
ACrego
Excursionist
17 2 6

Hey, belated thanks for that explanation, I just was searching for query-related things just now and this came up lol.

 

I have a new question directly related to this: I'm trying to make a wildcard search/filter in graphql, it looks like this:

query MyQuery {
  products(first: 10, query: "description:'you*'") {
    edges {
      node {
        title
        handle
        description
      }
    }
  }

 

What I expected to see was a list of just the product(s) with descriptions beginning with, or containing the term 'you'. Instead, I just got ALL 10 of the products. The word 'you' is not in any of the other descriptions. 

 

So the query works exactly the same with or without the asterisk after 'you'. It even works if I put 'foo' instead of 'you', and 'foo' is not in any of the descriptions. 

 

So, what is wrong with this syntax? 

 

Also, is it possible to use this same method to query deeply nested attributes? How would that look? 

 

query MyQuery {
  products(first: 10, query: "description:'foo'") {
    edges {
      node {
        title
        handle
        description
        variants(first: 10) { 
          edges {
            node {
              id
              title // Can I query the variant title up in the main products query?
            }
          }
        }
      }
    }
  }
}

Thanks in advance :)

...

0 Likes
btrain-004
Tourist
3 0 0

@Raj3 Did you eve figure out publishable_status` and `published_status`? I can't seem to find it anywhere? 

0 Likes
Nathanhahaha
Shopify Partner
18 0 2

what if my query value is a variable?

0 Likes
Jroyce1180
Pathfinder
85 4 14

@ACrego The reason was because your query was not actually using your search term and just returning all results. This is because description is not one of the currently available filter options. 

Look at the ProductVariant query options in the docs: https://shopify.dev/docs/admin-api/graphql/reference/queryroot

If you pass an invalid parameter, GraphQL returns the entire query and disregards the parameter.

Jroyce1180_0-1597547439451.png

 

 

0 Likes
Claire222
Shopify Partner
70 2 5

Anyone @Raj3 , @btrain-004 , did you find out about `publishable_status` and `published_status`?

0 Likes