将 .NET 枚举转换为 GraphQL EnumerationGraphType [英] Converting .NET Enum to GraphQL EnumerationGraphType

查看:22
本文介绍了将 .NET 枚举转换为 GraphQL EnumerationGraphType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I convert an enum to the EnumerationGraphType that GraphQL uses? Here is an example to illustrate what I'm talking about:

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 的隐式转换.在文档中,他们映射的模型将直接依赖在 MeetingStatusEnumType 上,但将 GraphQL 依赖引入域类型和对象之类的东西似乎并不好.我觉得我错过了一种非常简单的方法来注册这个领域,但我终生无法弄清楚.任何帮助将不胜感激!

Obviously this doesn't work because there's no implicit conversion from MeetingStatusType to MeetingStatusEnumType. In the documentation, the models that they were mapping would rely directly on MeetingStatusEnumType, but it doesn't seem good to introduce the dependency on GraphQL on something like your domain types and objects. I feel like I'm missing a painfully easy way to register this field, but I can't figure it out for the life of me. Any help would be greatly appreciated!

推荐答案

看起来我不应该尝试使用表达式重载来映射字段.将其切换为以下内容似乎已经解决了问题:

Looks like I should not have been trying to use the expression overload for mapping the fields. Switching it out to be the following instead seems to have solved the issue:

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

这篇关于将 .NET 枚举转换为 GraphQL EnumerationGraphType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆