HotChocolate:如何使用 [ExtendObjectType] 将指令绑定到解析器的字段

问题描述

想象一下这个 ObjectType 的字段“bars”来自 FooResolver,并用 BazDirective 进行了注释

    public class FooResolver {
       public IEnumerable<Bar> GetBars(string name) {/*omitted*/}
    }
    
    public class FooType: ObjectType<Foo>
    {

        protected override void Configure(IObjectTypeDescriptor<Foo> descriptor) {
            descriptor.Field<FooResolver>(_ => _.GetBars(default)).Directive<BazDirective>();
        } 
    } 

如果我们改为使用扩展绑定

class FooType: ObjectType<Foo> {}

[ExtendObjectType(Name="Foo")]
class FooResolver {
   [/* how to bind BazDirective? */]
   public IEnumerable<Bar> GetBars(string name) {/* omitted */}
}
   

如何绑定 BazDirective?

解决方法

解决方案是使用自定义 DescriptorAttributes,可用于在字段上添加额外信息。

https://github.com/ChilliCream/hotchocolate-docs/blob/master/docs/descriptor-attributes.md

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...