如何公开具有不同名称的graphql字段 [英] How to expose graphql field with different name

查看:21
本文介绍了如何公开具有不同名称的graphql字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索 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 操作中多次使用相同的查询或变异字段.这个 get 在使用字段参数时特别有趣:

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字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆