首页 > 解决方案 > HotChocolate:如何使用 [ExtendObjectType] 将指令绑定到解析器中的字段

问题描述

想象一下这个 ObjectType 带有来自 FooResolver 的字段“bars”,并用 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?

标签: .nethotchocolate

解决方案


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

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


推荐阅读