问题描述
我正在研究GraphQL,想知道是否有任何重命名响应字段的方法,例如我有一个带有这些字段的POJO
I am exploring GraphQL and would like to know if there is any way of renaming the response field for example i have a POJO with these field
class POJO {
Long id;
String name;
}
GraphQL查询:
GraphQL query:
type POJO {
id: Long
name: String
}
我的回答是这样的
{
"POJO" {
"id": 123,
"name": "abc"
}
}
我可以将名称字段重命名为类似UserName,这样我的回答就在下面吗
Can i rename the name field to something like userName so that my response is below
{
"POJO" {
"id": 123,
"userName": "abc"
}
}
推荐答案
您可以使用 GraphQL别名修改JSON响应中的各个键.
You can use GraphQL Aliases to modify individual keys in the JSON response.
如果这是您的原始查询
query {
POJO {
id
name
}
}
您可以为字段 name
引入GraphQL别名 userName
,如下所示:
you can introduce a GraphQL alias userName
for the field name
like so:
query {
POJO {
id
userName: name
}
}
您还可以使用GraphQL别名来使用在同一GraphQL操作中多次访问同一查询或突变字段.使用字段参数时,这一点特别有趣:
You can also use GraphQL aliases to use the same query or mutation field multiple times in the same GraphQL operation. This get's especially interesting when using field parameters:
query {
first: POJO(first: 1) {
id
name
}
second: POJO(first: 1, skip: 1) {
id
name
}
}
这篇关于如何使用不同的名称公开graphql字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!