4

I have a simple schema that I'm trying to query as follows:

{
  subQuery {
    subObjectGraph {
      Name
    }
  }
}

But "graphiql" throws the following error, without even seeming to run my query.

{
  "errors": [
    {
      "message": "Expected non-null value, resolve delegate return null for \"$Api.Schema.Queries.MySubObjectGraphType\"",
      "extensions": {
        "code": "INVALID_OPERATION"
      }
    }
  ]
}

What is wrong with my schema (below)? I am new-ing up a SubObject, so I don't understand why the error message implies the value is null.

    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);
        }
    }

The code works fine if I substitute MySubObjectGraphType with StringGraphType, so the problem must be with configuration of MySubObjectGraphType.

Please help? I'm using v2.4.

willem
  • 25,977
  • 22
  • 75
  • 115

2 Answers2

10

You need to add a service registration for the MySubObjectGraphType in your Startup.cs

So the rule could be described as "custom types deriving from ObjectGraphType must be registered via dependency injection at some point"

e.g in Startup.cs:

services.AddSingleton<MySubObjectGraphType>();

JPThorne
  • 610
  • 11
  • 17
0

You are returning a GraphType in subQuery in your RootQuery. Resolvers should only return DTOs and never GraphTypes.

If you are trying to organize your queries, then just return an empty object.

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

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

Joe McBride
  • 3,789
  • 2
  • 34
  • 38
  • Thanks Joe, and thanks for a great library! I tried changing as you suggested, but I still get the exact same issue. Looks like something is wrong with MySubObjectGraphType, but I can't figure out what. The "FetchFromRepo" method returns a Dto, so I imagine it should map to the graphType without issues? Slightly updated gist over here: https://gist.github.com/willemodendaal/77ff0feded77a33238e43142c7a00f3f – willem Dec 18 '18 at 04:43
  • I'm using v2.4 by the way. – willem Dec 18 '18 at 04:47