62

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"
  }
}
1
  • 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 on the Apollo documentation. Commented Jun 25, 2019 at 19:38

5 Answers 5

133

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
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I dont want to change my query, i was thinking if i can map that field to different name before returning the result from backend, is that possible?
Why don't you just rename the field directly in the schema then?
I think this is controlled by client side. But what if expose the fields need transform by server side?
This clears my issue for having custom field name on client side. Thanks !
So glad this exists for those that like to follow a singular naming convention.
|
1

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".

1 Comment

Just as a note, it would appear this renaming strategy does not work for InputObjectGraphType (types used on mutations).
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;
}

Comments

0

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.