Asp.net GraphQL.NET:如何将根查询分离为多个部分
我目前有一个小应用程序,它使用GraphQL与.net核心后端通信。我目前有一个单根查询,这对于GraphQL是必需的,为了组织的缘故,我正在寻找一种将其分解为多个部分的方法。我的查询如下:Asp.net GraphQL.NET:如何将根查询分离为多个部分,asp.net,.net-core,graphql,Asp.net,.net Core,Graphql,我目前有一个小应用程序,它使用GraphQL与.net核心后端通信。我目前有一个单根查询,这对于GraphQL是必需的,为了组织的缘故,我正在寻找一种将其分解为多个部分的方法。我的查询如下: public class ReactToFactsQuery : ObjectGraphType { public ReactToFactsQuery(IArticleService articleService, INewsItemService newsItemService)
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);
}
}
);
}
}
公共类ReactToFactsQuery:ObjectGraphType
{
公众反应行为(IArticleService articleService,
INewsItemService(新闻项服务)
{
然而,无论出于什么原因,我都在努力理解这里的示例,以及如何将其应用到我的代码中
非常感谢您的帮助。如文档所述,您可以将查询拆分为以下虚拟组
创建控制特定查询的子查询类型(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);
});
}
}
另一方面,如果您不想更改查询结构,并且只有一个根来保存特定的查询,那么为了清楚起见,可以将查询拆分为部分类
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);
});
}
}
在ArticlesQueryType中创建所有的文章查询是否是正确的假设。一个用于GetOne、GetAll等?我要补充的是,使用分部类的缺点是,如果使用构造函数注入,RootQuery文件仍然会因依赖项的长列表而变得臃肿。例如,如果您有100多个服务或存储库,那么hats将是一个很长的列表,使用模式优先的方法?我只看到分部方法是这里唯一的选项。。。
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);
});
}
}
type RootQuery {
articles: [Article]
....
}