首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL.NET:如何将根查询分离为多个部分

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

Stack Overflow用户
提问于 2019-03-15 18:20:01
回答 1查看 3K关注 0票数 12

我目前有一个小应用程序,它使用GraphQL与.net核心后端通信。我目前有一个根查询,这对于GraphQL来说是强制性的,并且为了组织的考虑,我正在寻找一种将其分解成多个部分的方法。我的查询如下:

代码语言:javascript
复制
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当前存在的查询名是articlearticlesnewsItems。最好是创建一个查询类来表示每种模型类型(例如,一个查询类用于与文章相关的查询,一个用于与新闻项目相关的查询,等等)。

我已经阅读了文档这里,但是,无论出于什么原因,我都很难理解这里的示例以及如何将它应用到我的代码中。

所有的帮助都是感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-15 21:17:57

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

创建控制特定查询的子查询类型(ArticlesQueryType)。

代码语言:javascript
复制
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查询类型为

代码语言:javascript
复制
type RootQuery {
  articles: ArticlesQuery
  news: NewsQuery
}

type ArticlesQuery {
   article(id: ID): Article
   articles: [Article]
}
...

另一方面,如果您不想更改查询结构,并且只有一个保存特定查询的根,则可以将查询拆分为部分类,以便清晰.

代码语言:javascript
复制
public partial class RootQuery: ObjectGraphType
{
    private IArticleService ArticleService { get; }

    public RootQuery()
    {
        Name = "RootQuery";

        InitializeArticlesQueries()
    }
}

例如,在另一个文件(RootQuery_Articles.cs)中

代码语言:javascript
复制
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查询类型是

代码语言:javascript
复制
type RootQuery {
    articles: [Article]
    ....
}
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55188636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档