首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL.NET如何向GraphQL类型添加枚举

GraphQL.NET如何向GraphQL类型添加枚举
EN

Stack Overflow用户
提问于 2019-03-01 19:09:30
回答 2查看 3.4K关注 0票数 4

我尝试在我的GraphQL类型中包含一个简单的枚举,并接收以下错误:

字段的GraphQL类型:父类型上的“类别”:“NewsItemType”不能隐式派生。 -字段的GraphQL类型:父类型上的“类别”:“NewsItemType”不能隐式派生。

我的简单枚举看起来像:

代码语言:javascript
复制
public enum NewsType
{
    General = 0,

    Business = 1,

    Entertainment = 2,

    Sports = 3,

    Technology = 4
}

它包含在其中的GraphQL ObjectGraphType

代码语言:javascript
复制
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类型基于以下视图模型:

代码语言:javascript
复制
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; }
}

我在这里做错了什么,我如何克服它?

编辑:我的查询包含以下内容:

代码语言:javascript
复制
        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);
                }
            }
        )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-05 20:29:07

我也很难让这件事成功。在这方面似乎显然缺乏正式文件。我让它工作的方式是基于我在这篇文章中发现的东西。

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

代码语言:javascript
复制
public class NewsEnumType : EnumerationGraphType<NewsType>
{
}

然后将您的字段更新为:

代码语言:javascript
复制
Field<NewsEnumType>(nameof(NewsItemViewModel.Category)).Description("Category of the news item.");

还有一件事需要提一下,那就是我偶然遇到了EnumTypes,这是我没想到的。如果您使用枚举作为参数,请执行上面所做的操作,将其作为IntGraphType摄入,然后将其转换为枚举类型的(NewsType)category

代码语言:javascript
复制
    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

票数 7
EN

Stack Overflow用户

发布于 2019-07-26 17:11:53

为了你的案子,这里有个快速的方法。基本上,您不使用默认的lambda表达式Field。相反,实际上自己写一个解析器,并将枚举转换成适当的类型。这有助于GraphQL正确地将类型转换为要返回的类型。

代码语言:javascript
复制
Field<IntGraphType>("category", resolve: context => (int)context.Source.Category);

如果您的枚举是一个字符串,您也可以这样做。

代码语言:javascript
复制
Field<StringGraphType>("category", resolve: context => context.Source.Category.ToString());

还有另一种更详细的方法,在这个答案中,您首先继承EnumerationGraphType<T>,然后执行与上面相同的自定义解析器。

https://stackoverflow.com/a/56051133/11842628

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54950868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档