1

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:

  • query { getResult() { resultTitle resultDecription } } <== do not execute the costly calculation
  • query { getResult() { resultTitle resultDecription costlyAdditionalProp } } <== DO execute the costly calculation

Can this be done with graphql-spqr?

Robert
  • 1,579
  • 1
  • 21
  • 36

1 Answers1

1

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);
    }
Robert
  • 1,579
  • 1
  • 21
  • 36
  • 1
    Yup, this is the correct way. SPQR suffers from next to no documentation so discovering features is harder than it needs to be. In general, the best way it to poke around the tests, as most (all?) the features are exemplified there. But sometimes the tests are too convoluted to showcase the concept properly... – kaqqao Mar 07 '21 at 20:37