问题描述
想象一下这个 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