【问题标题】:Hot Chocolate GraphQL - How to resolve field that uses ignored field with projectionHot Chocolate GraphQL - 如何解析使用带有投影的忽略字段的字段
【发布时间】:2022-08-18 23:24:33
【问题描述】:

我有一个数据模型ItemData,如下所示:

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

我想像这样将这两个公开为单个字段Duration。不暴露StartTimeEndTime

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

并输入 ItemTypeDurationType

ItemType 定义了一个字段 \"duration\" 像这样

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

端点标有 UseProjection 并使用 EFCore 数据上下文。

TheResolver.GetDuration() 被调用时,它没有从数据库中获取StartTimeEndTime 的值。我认为这是因为UseProjection 告诉它不要这样做。

当查询请求 duration 字段时,是否有任何方法可以将 Hot Chocolate 配置为在忽略的字段 StartTimeEndTime 上使用投影?

如果指定为忽略的字段,则指定 descriptor.Field(x =&gt; x.StartTime).IsProjected(true) 不起作用。

更新:刚刚在他们的 github 上发现了一个问题,看起来他们正在处理它 (Issue #4192 - It\'s not possible to project an ignored property)

    标签: c# .net graphql hotchocolate


    【解决方案1】:

    作为特定情况下问题的解决方法,您可以直接从可查询的持续时间中进行选择,例如:

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

    并且带有准备好的持续时间字段的 IQueryable 可以进一步返回到 [UseProjection] 等等。

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2017-12-19
      • 2020-08-24
      • 1970-01-01
      • 2022-07-31
      • 1970-01-01
      • 2015-04-01
      • 2016-08-26
      相关资源
      最近更新 更多