简体   繁体   English

复杂类型的 HotChocolate (GraphQL) 模式第一种方法

[英]HotChocolate (GraphQL) schema first approach on complex type

I'm novice in HotChocolate and I'm trying to PoC some simple usage.我是 HotChocolate 的新手,我正在尝试 PoC 一些简单的用法。 I've created very simple .graphql file:我创建了非常简单的 .graphql 文件:

#camera.graphql

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

type Query {
    getCamera: Camera!
}

And a very simple .NET code for camera wrapping:还有一个非常简单的 .NET 代码用于相机包装:

    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.然而,在最后一行,我确实得到了一个异常: HotChocolate.SchemaException: '发生多个架构错误: Camera.id字段没有解析器。 - Type: Camera The field Camera.name has no resolver. - 类型:Camera 字段Camera.name没有解析器。 - 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) (我已经通过文档几次,但我在那里找不到相关帮助)

As exception clearly states - there are no revolvers bind for the particular fields ("id" and "name") of the "Camera" type/object.例外情况明确指出 - “相机”类型/对象的特定字段(“id”和“name”)没有左轮手枪绑定。

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.就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM