Atik
Atik

Reputation: 29

How to do change query in hot chocolate based on filter applied?

I am new to hot chocolate and I have a filtering use case which I am unable to figure out.

I have this function in my product repository returning product data.

public IQueryable<Product> GetProducts()
{
    return _context.Products.Where(e=>e.indx == null);
}

If a user filters on field brand in the product class I want to change the query so it does not select Where(e=>e.indx==null). I need to do this when user does it on field brand or product type or both.

Upvotes: 1

Views: 5154

Answers (1)

beats
beats

Reputation: 113

Try something like this:

[UseProjection]
[UseFiltering]
public IQueryable<Customer> GetAll([Service] NorthWindDBContext dBContext)
{
    return dBContext.Customers.AsQueryable();
}

This allows me to write queries such as:

query {
      customers(where: {
        customerId: {
          eq: "ALFKI"
      }
    }) {
        customerId
        contactName
        companyName
        address
      }
    }

Upvotes: 2

Related Questions