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

时间:2019-03-15 18:20:01

标签: 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);
                }
            }
        );
    }
}

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

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

感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

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

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