gpt4 book ai didi

asp.net - GraphQL.NET : How to separate the root query into multiple parts

转载 作者:行者123 更新时间:2023-12-04 18:00:46 24 4
gpt4 key购买 nike

我目前有一个使用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);
}
}
);
}
}

当前,查询非常小且易于管理,但是随着应用程序的增长,我可以很容易地看到在此类中定义了很多查询。当前存在的查询名称是 articlearticlesnewsItems。最好是,我想创建一个查询类来表示每种模型类型(即,一个用于文章相关查询的查询类,一个用于新闻项目相关查询的查询类,等等)。

我已经阅读了 here文档,但是出于任何原因,我都在努力理解此处的示例以及如何将其应用于我的代码。

感谢所有帮助。

最佳答案

如文档所述,您可以像这样将查询分为多个虚拟组...

创建控制特定查询的子查询类型(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]
....
}

关于asp.net - GraphQL.NET : How to separate the root query into multiple parts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188636/

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