我有一个简单的模式,我试图查询如下:

{
  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。

最佳答案

您需要在您的 MySubObjectGraphType 中为 Startup.cs 添加服务注册

因此该规则可以描述为“从 ObjectGraphType 派生的自定义类型必须在某个时候通过依赖注入(inject)进行注册”

例如在 Startup.cs 中:
services.AddSingleton<MySubObjectGraphType>();

关于c# - 为什么 graphql-dotnet 会为此模式返回 "Expected non-null value"错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53773837/

10-10 04:59