asp.net - GraphQL.NET 如何将枚举添加到 GraphQL 类型

标签 asp.net .net asp.net-core .net-core graphql

我有一个简单的枚举,我试图将其包含在我的 GraphQL 类型中,但收到以下错误:

The GraphQL type for Field: 'Category' on parent type: 'NewsItemType' could not be derived implicitly.

------------------The GraphQL type for Field: 'Category' on parent type: 'NewsItemType' could not be derived implicitly.

---------------The type: NewsType cannot be coerced effectively to a GraphQL type Parameter name: type


我的简单枚举看起来像:
public enum NewsType
{
    General = 0,

    Business = 1,

    Entertainment = 2,

    Sports = 3,

    Technology = 4
}
GraphQL ObjectGraphType它包含在:
public class NewsItemType : ObjectGraphType<NewsItemViewModel>
{
    public NewsItemType()
    {
        Field(x => x.Id).Description("Id of a news item.");
        Field(x => x.Title).Description("Title of a new item.");
        Field(x => x.Description).Description("Description of a news item.");
        Field(x => x.Author).Description("Author of a news item.");
        Field(x => x.Url).Description("URI location of the news item");
        Field(x => x.ImageUrl).Description("URI location of the image for the news item");
        Field(x => x.PublishDate).Description("Date the news item was published");
        Field(x => x.Category).Description("Category of the news item.");
    }
}
最后,graphql 类型基于的 View 模型:
public class NewsItemViewModel : ViewModelBase
{
    public string Title { get; set; }

    public string Author { get; set; }

    public string Description { get; set; }

    public string Url { get; set; }

    public string ImageUrl { get; set; }

    public DateTime PublishDate { get; set; }

    public NewsType Category { get; set; }
}
我在这里做错了什么,我该如何克服它?
编辑:
我的查询包含以下内容:
        Field<ListGraphType<NewsItemType>>(
            name: "newsItems",
            arguments: new QueryArguments(
                new QueryArgument<IntGraphType>() { Name = "count" },
                new QueryArgument<IntGraphType>() { Name = "category" }),
            resolve: context =>
            {
                var count = context.GetArgument<int?>("count");
                var category = context.GetArgument<int>("category");
                var newsType = (NewsType)category;

                if (count.HasValue)
                {
                    return newsItemService.GetMostRecent(newsType, count.Value);
                }
                else
                {
                    return newsItemService.GetMostRecent(newsType);
                }
            }
        )

最佳答案

我也很难让它发挥作用。在这方面,官方文档似乎肯定是缺乏的。我让它工作的方式是基于我在 this article 中找到的东西.

对于您的场景,您将为您的枚举创建一个“GraphType”类,如下所示:

public class NewsEnumType : EnumerationGraphType<NewsType>
{
}

然后将您的字段更新为:
Field<NewsEnumType>(nameof(NewsItemViewModel.Category)).Description("Category of the news item.");

还有一件事要提到,我遇到了我没想到的 EnumTypes。如果您使用枚举作为参数,请执行您在上面将其作为 IntGraphType 摄取的操作。然后将其转换为您的枚举类型 (NewsType)category
    Field<ListGraphType<NewsItemType>>(
        name: "newsItems",
        arguments: new QueryArguments(
            new QueryArgument<IntGraphType>() { Name = "count" },
            new QueryArgument<IntGraphType>() { Name = "category" }),
        resolve: context =>
        {
            var count = context.GetArgument<int?>("count");
            var category = context.GetArgument<int>("category");
            var newsType = (NewsType)category;

            if (count.HasValue)
            {
                return newsItemService.GetMostRecent(newsType, count.Value);
            }
            else
            {
                return newsItemService.GetMostRecent(newsType);
            }
        }
    )

我的枚举是我作为参数传递的复杂对象的一部分,更像是 new QueryArgument<NewsItemType>() { Name = "newsItem" },
如果您打算这样做,那么 category传递给服务器的对象上的属性需要是一个字符串。因此,如果您传回服务器的类别是 Business = 1 ,那么你需要通过 category: 'Business'而不是 category: 1 .

关于asp.net - GraphQL.NET 如何将枚举添加到 GraphQL 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950868/

相关文章:

docker - 事件名称应至少由斜杠分隔的 3 个部分组成。 Asp.Net Core Docker 中的参数名称 eventName

javascript - 如何在 asp.net 用户控件中调整子列表框的高度

c# - 向导中动态加载的控件

c# - 确保表单例份验证在浏览器关闭时注销

.net - 如何使子元素在 WPF 中受其父元素的大小限制?

c# - 如何在 Blazor/Razor 中获取事件数据

c# - 创建自定义 OAuth2 服务器

c# - 无法加载文件或程序集 Microsoft.ReportViewer.WebForms.XmlSerializers

c# - 如何在基类中盲目引发事件而不从派生类调用它

c# - 确定实体代理的上下文是否已被处置