4

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

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

4

2 回答 2

9

您需要为MySubObjectGraphType您的Startup.cs

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

例如在Startup.cs

services.AddSingleton<MySubObjectGraphType>();

于 2018-12-20T06:38:40.823 回答
0

您在 RootQuery 的子查询中返回 GraphType。解析器应该只返回 DTO 而从不返回 GraphTypes。

如果您尝试组织查询,则只需返回一个空对象。

Field<MySubQuery>(
  name: "subQuery",
  resolve: ctx => new {});

https://graphql-dotnet.github.io/docs/getting-started/query-organization

于 2018-12-14T16:41:00.487 回答