Search code examples
c#graphqlhotchocolate

HotChocolate AND filtering in DateTime Enumerable


In my c# HotChocolate graphql app (HotChocolate.AspNetCore version 13.5.1) I have the following class containing an Enumerable with DateTimes.

public class MyData {

    ...
    public IEnumerable<DateTime> MyDates { get; set; } = new List<DateTime>();
}

I want to get all entries where the myDates contains an entry that is between 2 specific dates. For that I am trying the following query in my Playground.

query {
    getMyData
        (first: 10, 
         where: { myDates: { some: {gt: "2016-02-23", lt: "2016-02-25"}}}) 
    { ... }
}

Examples:

MyDates: [ "2016-02-22", "2016-02-24", "2016-02-26"] should be found.

MyDates: [ "2016-02-22", "2016-02-26"] should not be found.

The problem is that both entries are found, because somehow the "gt" and "lt" are being executed as OR and not as AND.

My query is configured like this:

[ExtendObjectType("Query")]
public class MyDataQuery
{
    [UsePaging(IncludeTotalCount = true)]
    [UseFiltering]
    [UseSorting]
    public IQueryable<MyData> GetMyData([Service] ISomeService service)
    {
        if (service == null) throw new ArgumentNullException(nameof(service));
        return service.GetMyData());
    }
}

In my Program.cs:

builder.Services.AddGraphQLServer()
    .AddQueryType(d => d.Name("Query"))
    .AddTypeExtension<MyDataQuery>()
    .AddProjections()
    .AddFiltering()
    .AddSorting()
    .ModifyRequestOptions(opt => opt.IncludeExceptionDetails = 
true);

Solution

  • Case closed.

    The filtering worked fine. I was missing a property inside my "HasKey()" definition of my entity. Therefore the HasMany relationship always had only one entry inside the myDates enumerable.

    It looked like the filtering did not work, but somehow HotChocolate found the right entries, but was missing some myDates entries.