gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:36:21 28 4
gpt4 key购买 nike

我有一个简单的枚举,我试图将其包含在我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com