GraphQL.NET:如何将根查询分成多个部分

Gre*_*egH 10 asp.net .net-core graphql

我目前有一个使用 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);
                }
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

感谢所有帮助。

jgo*_*day 17

正如文档所说,您可以将查询拆分为这样的虚拟组......

创建控制特定查询的子查询类型 (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);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

GraphQL 查询类型将是

type RootQuery {
  articles: ArticlesQuery
  news: NewsQuery
}

type ArticlesQuery {
   article(id: ID): Article
   articles: [Article]
}
...
Run Code Online (Sandbox Code Playgroud)

另一方面,如果您不想更改查询结构并且只有一个包含特定查询的根,为了清楚起见,您可以将查询拆分为部分类......

public partial class RootQuery: ObjectGraphType
{
    private IArticleService ArticleService { get; }

    public RootQuery()
    {
        Name = "RootQuery";

        InitializeArticlesQueries()
    }
}
Run Code Online (Sandbox Code Playgroud)

例如在另一个文件 (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);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,GraphQL 查询类型是

type RootQuery {
    articles: [Article]
    ....
}
Run Code Online (Sandbox Code Playgroud)

  • 我想补充一点,使用部分类的缺点是,如果您使用构造函数注入,RootQuery 文件仍然会因一长串依赖项而变得臃肿。例如,如果您有 100 多个服务或存储库,那么这将是一个很长的列表 (2认同)