Search code examples
c#.netgraphqlhotchocolate

Hot Chocolate GraphQL - How to resolve field that uses ignored field with projection


I have a data model ItemData as follows :

class ItemData
{
  public string StartTime { get; set; }
  public string EndTime { get; set; }
  // other fields here
}

I would like to expose those two as a single field Duration like this. Without exposing the StartTime and EndTime

{
  "duration": {
    "start": "12:34",
    "end": "23:45"
  },
  // other fields here
}

And types ItemType and DurationType

ItemType defined a field "duration" like this

descriptor.Ignore(x=> x.EndTime);
descriptor.Ignore(x=> x.StartTime);
descriptor.Field("duration")
    .ResolveWith<TheResolver>(resolver => resolver.GetDuration(default!))
    .Type<DurationType>();
// other configurations here

The endpoint is marked with UseProjection and use EFCore data context.

When TheResolver.GetDuration() gets called it did not fetch the value for StartTime and EndTime from database. I think it's because the UseProjection tell it not to.

Is there any way to configure the Hot Chocolate to use projection on ignored fields StartTime and EndTime when the query requests duration field?

Specifying descriptor.Field(x => x.StartTime).IsProjected(true) does not work if the field specified as ignored.

UPDATE : Just found an issue on their github, looks like they're working on it (Issue #4192 - It's not possible to project an ignored property)


Solution

  • As a workaround for the issue in your particular case, you could make the selection with duration directly from queryable like:

    dbContext.Items.Select(i => new ItemData { Duration = new Duration { Start = i.StartTime, End = i.End } })
    

    And that IQueryable with the ready duration field to return further to [UseProjection] and whatever.