How to expose graphql field with different name

后端 未结 4 1497
攒了一身酷
攒了一身酷 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
      }
    }
    
    0 讨论(0)
  • 2020-12-18 18:48

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 18:51

    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.

    0 讨论(0)
  • 2020-12-18 19:05

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

    0 讨论(0)
提交回复
热议问题