0

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.

Joundill
  • 6,828
  • 12
  • 36
  • 50
Nikita
  • 19
  • 1
  • 6

1 Answers1

2

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
      }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
beats
  • 93
  • 4
  • I understand this but assume the client does not use the where on customerId, then I need to change, in your case, the GetAll method logic. – Nikita Apr 24 '21 at 17:07
  • You can just replace customerId in the query to a column you do want to filter by. So in my example I could change customerId to contactName, change the eq to a valid contact Name in the customer table and it will still work without changing the Get all method. – beats Apr 25 '21 at 18:32
  • Hi, yes but my use case is I need it to be automatic in that if they don't filter on a specific field, say customerId, then the query must change to be filtered where some other field say customerName is null. The client should not have to manually filter where customerName is null. It should automatically do this if the client applies no filter on the customerId. – Nikita Apr 25 '21 at 20:25