为什么 graphql-dotnet 会为此模式返回 "Expected non-null value" 错误?
Why is graphql-dotnet returning "Expected non-null value" error for this schema?
我有一个简单的模式,我正尝试按如下方式查询:
{
subQuery {
subObjectGraph {
Name
}
}
}
但是 "graphiql" 抛出以下错误,甚至 运行 我的查询似乎都没有。
{
"errors": [
{
"message": "Expected non-null value, resolve delegate return null for \"$Api.Schema.Queries.MySubObjectGraphType\"",
"extensions": {
"code": "INVALID_OPERATION"
}
}
]
}
我的模式(如下)有什么问题?我正在新建一个子对象,所以我不明白为什么错误消息暗示该值为空。
public class Schema: GraphQL.Types.Schema
{
public Schema(IDependencyResolver resolver): base(resolver)
{
Query = resolver.Resolve<RootQuery>();
Mutation = null;
}
}
public class RootQuery: ObjectGraphType
{
public RootQuery(IDependencyResolver resolver)
{
Name = "Query";
Field<MySubQuery>(
name: "subQuery",
resolve: ctx => resolver.Resolve<MySubQuery>());
}
}
public class MySubQuery: ObjectGraphType
{
public MySubQuery()
{
Name = "TempSubQuery";
Field<StringGraphType>("SubQueryName", resolve: ctx => "Some string value");
Field<MySubObjectGraphType>(
name: "subObjectGraph",
resolve: ctx => FetchFromRepo());
}
//Repo access would go here, but just new-ing the object for now.
private SubObject FetchFromRepo()
{
return new SubObject() { Name = "some sub object" };
}
}
public class SubObject
{
public string Name { get; set; }
}
public class MySubObjectGraphType: ObjectGraphType<SubObject>
{
public MySubObjectGraphType()
{
Name = "MySubObject";
Description = "An object with leaf nodes";
Field(l => l.Name);
}
}
如果我用 StringGraphType 替换 MySubObjectGraphType,代码工作正常,所以问题一定出在 MySubObjectGraphType 的配置上。
请帮忙?我正在使用 v2.4。
您正在 return在您的 RootQuery 的子查询中创建 GraphType。解析器应该只 return DTO 而不是 GraphTypes。
如果您尝试组织查询,那么只需 return 一个空对象。
Field<MySubQuery>(
name: "subQuery",
resolve: ctx => new {});
https://graphql-dotnet.github.io/docs/getting-started/query-organization
您需要在 Startup.cs
中为 MySubObjectGraphType
添加服务注册
所以规则可以描述为"custom types deriving from ObjectGraphType
must be registered via dependency injection at some point"
例如 Startup.cs
:
services.AddSingleton<MySubObjectGraphType>();
我有一个简单的模式,我正尝试按如下方式查询:
{
subQuery {
subObjectGraph {
Name
}
}
}
但是 "graphiql" 抛出以下错误,甚至 运行 我的查询似乎都没有。
{
"errors": [
{
"message": "Expected non-null value, resolve delegate return null for \"$Api.Schema.Queries.MySubObjectGraphType\"",
"extensions": {
"code": "INVALID_OPERATION"
}
}
]
}
我的模式(如下)有什么问题?我正在新建一个子对象,所以我不明白为什么错误消息暗示该值为空。
public class Schema: GraphQL.Types.Schema
{
public Schema(IDependencyResolver resolver): base(resolver)
{
Query = resolver.Resolve<RootQuery>();
Mutation = null;
}
}
public class RootQuery: ObjectGraphType
{
public RootQuery(IDependencyResolver resolver)
{
Name = "Query";
Field<MySubQuery>(
name: "subQuery",
resolve: ctx => resolver.Resolve<MySubQuery>());
}
}
public class MySubQuery: ObjectGraphType
{
public MySubQuery()
{
Name = "TempSubQuery";
Field<StringGraphType>("SubQueryName", resolve: ctx => "Some string value");
Field<MySubObjectGraphType>(
name: "subObjectGraph",
resolve: ctx => FetchFromRepo());
}
//Repo access would go here, but just new-ing the object for now.
private SubObject FetchFromRepo()
{
return new SubObject() { Name = "some sub object" };
}
}
public class SubObject
{
public string Name { get; set; }
}
public class MySubObjectGraphType: ObjectGraphType<SubObject>
{
public MySubObjectGraphType()
{
Name = "MySubObject";
Description = "An object with leaf nodes";
Field(l => l.Name);
}
}
如果我用 StringGraphType 替换 MySubObjectGraphType,代码工作正常,所以问题一定出在 MySubObjectGraphType 的配置上。
请帮忙?我正在使用 v2.4。
您正在 return在您的 RootQuery 的子查询中创建 GraphType。解析器应该只 return DTO 而不是 GraphTypes。
如果您尝试组织查询,那么只需 return 一个空对象。
Field<MySubQuery>(
name: "subQuery",
resolve: ctx => new {});
https://graphql-dotnet.github.io/docs/getting-started/query-organization
您需要在 Startup.cs
MySubObjectGraphType
添加服务注册
所以规则可以描述为"custom types deriving from ObjectGraphType
must be registered via dependency injection at some point"
例如 Startup.cs
:
services.AddSingleton<MySubObjectGraphType>();