52

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 query:

type POJO {
  id: Long
  name: String
}

My response is something like this

{
  "POJO" {
    "id": 123,
    "name": "abc"
  }
}

Can i rename the name field to something like userName so that my response is below

{
  "POJO" {
    "id": 123,
    "userName": "abc"
  }
}
Amit Kumar
  • 825
  • 1
  • 9
  • 19
  • The traditional way to do this would be in a (custom) resolver. So the argument value in the query would be `userName` and in your resolver you look up the `name` value. This would even work if all you could modify was an existing schema. See [Schema Delegation](https://www.apollographql.com/docs/graphql-tools/schema-delegation/) on the Apollo documentation. – Benjamin R Jun 25 '19 at 19:38

6 Answers6

112

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 gets especially interesting when using field parameters:

query {
  first: POJO(first: 1) {
    id
    name
  }

  second: POJO(first: 1, skip: 1) {
    id
    name
  }
}
marktani
  • 7,578
  • 6
  • 37
  • 60
1

The question is: how are you creating the schema in the first place? There's no intrinsic connection between Java and GraphQL types - they are completely unrelated unless you correlate them. So you can name the fields any way you want in the schema, and make a resolver (DataFetcher) that gets the value from anywhere (thus any POJO field too).

If you're using a tool to generate the schema from Java types (graphql-java-annotations, graphql-spqr etc), then use that tool's facilities to drive the mapping. Both the mentioned tools allow customizing the mapping via annotations. GraphQL-SPQR enables the same via external configuration as well.

If you clarify your question further, I'll be able to give a more precise answer.

kaqqao
  • 12,984
  • 10
  • 64
  • 118
0

Looks like GraphQLName annotation can help.

Example from documentation : "Additionally, @GraphQLName can be used to override field name. You can use @GraphQLDescription to set a description."

These can also be used for field parameters:

public String field(@GraphQLName("val") String value) {
  return value;
}
Alex.Gri
  • 1
  • 1
0

I know this question is very old but following code is used for renaming the field:

public class ProductReviewType: ObjectGraphType<ProductReview>
{
    public ProductReviewType()
    {
        Field(x => x.ProductReviewId, type: typeof(IdGraphType)).Description("some desc here");
        Field(x => x.ProductId).Description("some desc here");
        Field("reviewername", x => x.ReviewerName).Description("some desc here");            
        Field("reviewdate",x => x.ReviewDate).Description("some desc here");
        Field("emailaddress", x => x.EmailAddress).Description("some desc here");
        Field("rating", x => x.Rating).Description("some desc here");
        Field("comments",x => x.Comments).Description("some desc here");
        Field("modifieddate", x => x.ModifiedDate).Description("some desc here");
    }

}

In the above code, modifieddate would be the field name for property "ModifiedDate".

Sormita Chakraborty
  • 1,015
  • 2
  • 19
  • 36
  • Just as a note, it would appear this renaming strategy does not work for InputObjectGraphType (types used on mutations). – K0D4 Aug 06 '20 at 20:30
0

This worked for me, if you are using the Field(x=>x.Example) syntax:

Field(x => x.Example).Name("exampleNewName");

This will make your query go from this:

query{
  fake{
    example
   }
}

to this:

query{
  fake{
    exampleNewName
   }
}
DJ Burb
  • 2,346
  • 2
  • 29
  • 38
0

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 gets especially interesting when using field parameters:

query { first: POJO(first: 1) { id name }

second: POJO(first: 1, skip: 1) { id name } }