graphqlgraphql-spqr

graphql-spqr: How to add a GraphQL property to an entity


I have a basic Java entity class.

public class Result {
  String resultTitle;
  String resultDecription;
}

This class is returned in a GraphQL query

@GraphQLQuery(name = "getResult")
public Result getResult() {
  Result result = //get result, e.g. from DB
  return result;
}

This works fine so far. Now I want to return one additional attribute in the GraphQL query. But that additonal attribute is very costly to calculate. So I only want to return it, when the GraphQL client actually requests it in his query like so:

Can this be done with graphql-spqr?


Solution

  • Oh, that was actually quite simple. Just needed to dig up the right example from the graphql-spqr-examples.

        @GraphQLQuery(name = "costlyAdditionalProp ")
        public Long getCostlyAdditionalProp (@GraphQLContext Result result) {
            return calculationService.doCalculation(result);
        }