Hasura complex search of products

Viewed 33

How can i solve this problem with complex searching in products?

This is what i have now as a standard get products and search on them.

query getProducts($limit: Int, $offset: Int, $searchQuery: String) {
  products_products(limit: $limit, offset: $offset, where: {_or: [{title: {_ilike: $searchQuery}}, {shortDescription: {_ilike: $searchQuery}}], active: {_eq: true}}) {
    __typename
    id
    shortDescription
    title
    likes {
      userId
    }
    images(where: {main: {_eq: true}}) {
      image
    }
    categories {
      category {
        title
      }
    }
  }
}

i have products table, categories table, join table(productId, categoryId), later on there would be reviews table, wishlist table

  1. how to show products only in any category (record in join table)?
  2. how to show products with reviews 3.5+ ?

You know how it is with complex filtration.

Can someone show me how it could be done? Lastly .. im using flutter with optimistic results, so thats why i chose this structure of query. it makes sense when "replicating" the state of wishlist.

1 Answers
  1. how to show products only in any category (record in join table)?

You can filter on related data as well

query {
  product(where: { category: { name: { _eq: "Kitchen" } } })
}
  1. how to show products with reviews 3.5+ ?

I assume you mean an average review of 3.5 stars or greater?

There is a brand new feature in Hasura called Aggregation Predicates that lets you do this

See last month's community call demo for how to use it:

By the way, if you are building an e-commerce type application, there is a sample application which already implements much of this functionality so you can see how it is done:

Related