【问题标题】:GraphQL query in .NET Core returning empty result.NET Core 中的 GraphQL 查询返回空结果
【发布时间】:2019-07-07 16:36:54
【问题描述】:

我目前设置了一个简单的查询 (ArticleQuery),其中包括两个字段。第一个字段接受一个 id 并返回适当的数据——这个字段的功能与我期望的一样,并且有效。第二个字段(名为articles)应该返回表中的所有对象,但是当使用GraphiQL 接口发出以下查询时,我将返回一个空字符串。

查询:

query GetArticleData(){
  articles {
    id
    description
  }
}

ArticleQuery 如下所示:

    public class ArticleQuery : ObjectGraphType
    {
        public ArticleQuery(IArticleService articleService)
        {
            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",
                resolve: context =>
                {
                    return articleService.GetAll();
                }
            );
        }
    }

请注意,在 articleService.GetAll() 方法中设置的断点永远不会被命中。

最后,ArticleType 类:

    public class ArticleType : ObjectGraphType<ArticleViewModel>
    {
        public ArticleType()
        {
            Field(x => x.Id).Description("Id of an article.");
            Field(x => x.Description).Description("Description of an article.");
        }
    }

为什么我的查询返回一个空字符串而不是我的文章列表,我该如何解决这个问题?

【问题讨论】:

    标签: asp.net asp.net-core .net-core graphql


    【解决方案1】:

    经过更多尝试,我的查询格式似乎不正确。应该是:

    query GetArticleData{
      articles {
        id
        description
      }
    }
    

    代替:

    query GetArticleData(){
      articles {
        id
        description
      }
    }
    

    因为括号仅在指定查询变量时需要,否则需要排除。

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 2020-10-23
      • 1970-01-01
      • 2020-01-11
      • 2020-11-02
      • 2021-03-18
      • 2021-11-20
      • 2019-10-13
      • 1970-01-01
      相关资源
      最近更新 更多