【问题标题】:Converting .NET Enum to GraphQL EnumerationGraphType将 .NET 枚举转换为 GraphQL EnumerationGraphType
【发布时间】:2019-09-25 17:02:54
【问题描述】:

如何将枚举转换为 GraphQL 使用的 EnumerationGraphType? 这是一个例子来说明我在说什么:

public enum MeetingStatusType
{
    Tentative,
    Unconfirmed,
    Confirmed,
}
public class MeetingDto
{
    public string Id { get; set; }
    public string Name { get; set; }
    public MeetingStatusType Status { get; set; }
}
public class MeetingStatusEnumType : EnumerationGraphType<MeetingStatusType>
{
    public MeetingStatusEnumType()
    {
        Name = "MeetingStatusType";
    }
}
public class MeetingType : ObjectGraphType<MeetingDto>
{
    public MeetingType()
    {
        Field(m => m.Id);
        Field(m => m.Name, nullable: true);
        Field<MeetingStatusEnumType>(m => m.Status); // Fails here
     }
}

显然这不起作用,因为没有从MeetingStatusTypeMeetingStatusEnumType 的隐式转换。 In the documentation,他们正在映射的模型将直接依赖于 MeetingStatusEnumType,但是在诸如域类型和对象之类的东西上引入对 GraphQL 的依赖似乎并不好。 我觉得我错过了一种非常简单的方法来注册这个领域,但我一辈子都想不通。任何帮助将不胜感激!

【问题讨论】:

  • 您能否显示Field&lt;T&gt; 的定义或确切的错误消息?我只能找到第一个参数是字符串的。
  • 出现两个错误:Cannot implicitly convert type 'MeetingStatusType' to 'MeetingStatusEnumType' Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type 这是我正在使用的字段重载的定义:public FieldType Field&lt;TGraphType&gt;(string name, string description = null, QueryArguments arguments = null, Func&lt;ResolveFieldContext&lt;TSourceType&gt;, object&gt; resolve = null, string deprecationReason = null) where TGraphType : IGraphType;

标签: c# .net-core graphql graphql-dotnet


【解决方案1】:

另一种方式;

Field(m => m.Status, type: typeof(EnumerationGraphType<MeetingStatusType>));

【讨论】:

    【解决方案2】:

    您需要告诉 graphql dotnet 如何将 Enum 类型映射到 EnumerationGraphType

    GraphTypeTypeRegistry.Register(typeof(MeetingStatusType), typeof(EnumerationGraphType<MeetingStatusType>));
    

    【讨论】:

    • 这是错字吗?我没有看到类似GraphTypeTypeRegistry
    • 在这里,不是错字...GraphTypeTypeRegistry
    【解决方案3】:

    看起来我不应该尝试使用表达式重载来映射字段。将其改为以下似乎已经解决了这个问题:

    Field(e => e.Id);
    Field(e => e.Name, nullable: true);
    Field<MeetingStatusEnumType>("meetingStatus", resolve: e => e.Source.Status);
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多