How to expose graphql field with different name

后端 未结 4 1500
攒了一身酷
攒了一身酷 2020-12-18 18:33

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 {
  Lo         


        
4条回答
  •  臣服心动
    2020-12-18 18:44

    You can use GraphQL Aliases to modify individual keys in the JSON response.

    If this is your original query

    query {
      POJO {
        id
        name
      }
    }
    

    you can introduce a GraphQL alias userName for the field name like so:

    query {
      POJO {
        id
        userName: name
      }
    }
    

    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
      }
    }
    

提交回复
热议问题