我目前有一个简单的查询(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.");
}
}
为什么我的查询返回一个空字符串而不是我的文章列表,我该如何解决这个问题?
经过更多的游戏,看起来我的查询格式不正确。应该是:
query GetArticleData{
articles {
id
description
}
}
代替:
query GetArticleData(){
articles {
id
description
}
}
因为只有在指定查询变量时才需要括号,否则需要排除。