作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的枚举,我试图将其包含在我的 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.");
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/
我是一名优秀的程序员,十分优秀!