是的。您可以将非标量类型定义为指令中的属性,但它应该是 Input 类型:
input UrlInput {
address: String!
desription: String
}
而且您在声明时也会错过directive 保留字。应该是:
directive @custom_directive (url:UrlInput!) on FIELD_DEFINITION
在字段上注释:
type AnotherType {
field: SomeOtherType @custom_directive (url : {address: "foo" description: "bar"})
}
给定GraphQLSchema,然后您可以通过以下方式访问在给定字段上注释的指令值:
Map<String, Object> value = (Map<String, Object>) graphQLSchema
.getObjectType("AnotherType")
.getFieldDefinition("field")
.getDirective("custom_directive")
.getArgument("url").getValue();
value.get("address") //foo
value.get("description") //bar
您还可以实现SchemaDirectiveWiring 并覆盖它的onField(),这将在使用此指令标记的字段中调用
在构建GraphQLSchema 期间。在此方法中,您可以根据该指令中配置的值更改此字段的GraphQLFieldDefinition。 (例如修改其数据获取器等)
此SchemaDirectiveWiring 在构建RuntimeWiring 时由以下注册:
RuntimeWiring.newRuntimeWiring()
.directive("custom_directive", new MySchemaDirectiveWiring())
.build();