我目前有一个小应用程序,它使用GraphQL与.net核心后端通信。我目前有一个根查询,这对于GraphQL来说是强制性的,并且为了组织的考虑,我正在寻找一种将其分解成多个部分的方法。我的查询如下:
public class ReactToFactsQuery : ObjectGraphType
{
public ReactToFactsQuery(IArticleService articleService,
INewsItemService newsItemService)
{
Field<ArticleType>(
name: "article",
arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
resolve: context =>
{
var id = context.GetArgument<int>("id");
return articleService.Get(id);
}
);
Field<ListGraphType<ArticleType>>(
name: "articles",
arguments: new QueryArguments(new QueryArgument<IntGraphType>() { Name = "count" }),
resolve: context =>
{
var count = context.GetArgument<int?>("count");
if (count.HasValue)
{
return articleService.GetAll(count.Value);
}
else
{
return articleService.GetAll();
}
}
);
Field<ListGraphType<NewsItemType>>(
name: "newsItems",
arguments: new QueryArguments(
new QueryArgument<IntGraphType>() { Name = "count" },
new QueryArgument<IntGraphType>() { Name = "newsType" }),
resolve: context =>
{
var count = context.GetArgument<int?>("count");
var category = context.GetArgument<int>("newsType");
var newsType = (NewsType)category;
if (count.HasValue)
{
return newsItemService.GetMostRecent(newsType, count.Value);
}
else
{
return newsItemService.GetMostRecent(newsType);
}
}
);
}
}目前,查询非常小且易于管理,但是随着应用程序的增长,我可以很容易地看到该类中定义的大量查询。THe当前存在的查询名是article、articles和newsItems。最好是创建一个查询类来表示每种模型类型(例如,一个查询类用于与文章相关的查询,一个用于与新闻项目相关的查询,等等)。
我已经阅读了文档这里,但是,无论出于什么原因,我都很难理解这里的示例以及如何将它应用到我的代码中。
所有的帮助都是感激的。
发布于 2019-03-15 21:17:57
如文档所述,您可以将查询拆分成这样的虚拟组.
创建控制特定查询的子查询类型(ArticlesQueryType)。
public class RootQuery : ObjectGraphType
{
public RootQuery()
{
Name = "RootQuery";
// defines the articles sub query and returns an empty anonymous type object
// whose only purpose is to allow making queries on the subtype (ArticlesQueryType)
Field<ArticlesQueryType>("articles", resolve: context => new {});
}
}
// defines the articles specific queries
public class ArticlesQueryType: ObjectGraphType
{
public ArticlesQueryType(IArticleService articleService)
{
Name = "ArticlesQuery";
Field<ArticleType>(
name: "article",
arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
resolve: context =>
{
var id = context.GetArgument<int>("id");
return articleService.Get(id);
});
}
}GraphQL查询类型为
type RootQuery {
articles: ArticlesQuery
news: NewsQuery
}
type ArticlesQuery {
article(id: ID): Article
articles: [Article]
}
...另一方面,如果您不想更改查询结构,并且只有一个保存特定查询的根,则可以将查询拆分为部分类,以便清晰.
public partial class RootQuery: ObjectGraphType
{
private IArticleService ArticleService { get; }
public RootQuery()
{
Name = "RootQuery";
InitializeArticlesQueries()
}
}例如,在另一个文件(RootQuery_Articles.cs)中
public partial class RootQuery
{
protected InitializeArticlesQuery()
{
Field<ArticleType>(
name: "article",
arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "id" }),
resolve: context =>
{
var id = context.GetArgument<int>("id");
return articleService.Get(id);
});
}
}这样,GraphQL查询类型是
type RootQuery {
articles: [Article]
....
}https://stackoverflow.com/questions/55188636
复制相似问题