graphql - 自定义指令的非原始类型属性
问题描述
我有一个关于 graphql 模式定义的问题。
可以将非原始类型定义为指令的属性吗?如果是,那么在字段上使用指令时的语法是什么?
例如,有一个 Url 类型定义如下:
type Url {
address: String!
desription: String
}
和一个指令
@custom_directive {
url: Url!
} on FIELD_DEFINITION
然后如何在字段上使用该指令?
type AnotherType {
field: SomeOtherType @custom_directive(url: ???)
}
谢谢
解决方案
是的。您可以将非标量类型定义为指令中的属性,但它应该是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"})
}
给定 a 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
您还可以实现 aSchemaDirectiveWiring
并覆盖它onField()
,在构建期间将调用此指令标记的字段GraphQLSchema
。GraphQLFieldDefinition
在此方法中,您可以根据该指令中配置的值更改此字段。(例如修改其数据获取器等)
这SchemaDirectiveWiring
在构建时由以下内容注册RuntimeWiring
:
RuntimeWiring.newRuntimeWiring()
.directive("custom_directive", new MySchemaDirectiveWiring())
.build();
推荐阅读
- java - 坚持从控制台检测下一行
- java - 当类名在同一个类中使用两次时,java传递一个变量
- actions-on-google - 如何在 Google 上为基本卡片添加边框
- ruby-on-rails - 如何验证参数值(字符串)?
- c# - ASP.NET MVC - 如何找到返回响应的代码
- php - Laravel 没有获取访问器试图获取非对象的属性
- git - 无法将项目从本地推送到 AWS 中设置的 git 存储库:权限被拒绝(公钥)
- c++ - C++ Vector不显示包含对象的数据
- python - 如何更新熊猫数据框的日期时间索引值?
- sql - jpa hibernate 在 DB2 中呈现 SQL 错误?