1

I'm novice in HotChocolate and I'm trying to PoC some simple usage. I've created very simple .graphql file:

#camera.graphql

type Camera {
    id: ID!
    name: String!
}

type Query {
    getCamera: Camera!
}

And a very simple .NET code for camera wrapping:

    public class QlCamera
    {
        public static QlCamera New()
        {
            return new QlCamera
            {
                Id = Guid.NewGuid().ToString(),
                Name = Guid.NewGuid().ToString()
            };
        }

        public string Id { get; set; }
        public string Name { get; set; }
    }

as well as such for schema creation:

   public void CreateSchema()
   {
        string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        var smBuilder = SchemaBuilder.New();
        smBuilder.AddDocumentFromFile(path + "/GraphQL/camera.graphql");

        smBuilder.AddResolver("Query", "getCamera", () => QlCamera.New());

        var schema = smBuilder.Create();
   }

On the last line however I do get an exception : HotChocolate.SchemaException: 'Multiple schema errors occured: The field Camera.id has no resolver. - Type: Camera The field Camera.name has no resolver. - Type: Camera '

I've tried to create :

    public class QlCameraType : ObjectType<QlCamera>
    {
        protected override void Configure(IObjectTypeDescriptor<QlCamera> descriptor)
        {
            descriptor.Name("Camera");

            descriptor.Field(t => t.Id).Type<NonNullType<StringType>>();
            descriptor.Field(t => t.Name).Type<StringType>();
        }
    }

and to replace

smBuilder.AddResolver("Query", "getCamera", () => QlCamera.New());

with

smBuilder.AddResolver("Query", "getCamera", () => new QlCameraType());

But I continue to get the same exception.

Obviously I miss something here, But I cannot understand what exactly. Could someone explain me what I do miss ?

(I've passed few times trough the documentation, but I cannot find relevant help there)

pvv
  • 147
  • 10

1 Answers1

0

As exception clearly states - there are no revolvers bind for the particular fields ("id" and "name") of the "Camera" type/object.

So they just have to be added with :

smBuilder.AddResolver("Camera", "id", rc => rc.Parent<QlCamera>().Id);
smBuilder.AddResolver("Camera", "name", rc => rc.Parent<QlCamera>().Name);

And that is it.

pvv
  • 147
  • 10
  • So do these AddResolver statements REPLACE his AddResolver statement? Or are they additional statements? You don't specify. With the state of docs as it is it would be helpful if there was clarity. Where do these statements go in the above code? – Andrew H Jul 14 '20 at 04:41
  • They are additional statements - they do not replace the initial one: smBuilder.AddResolver("Query", "getCamera", () => QlCamera.New()); which describes the getCamera query. They just specify how different fields of the getCamera will be resolved. If you use type first approach - this is not needed - as the schema is generated from the type. In the schema first approach however you have to specify how all the particular fields are resolved. Even if their names match (response fields and type fields). – pvv Jul 15 '20 at 08:07