[英]graphql-spqr: How to add a GraphQL property to an entity
I have a basic Java entity class.我有一个基本的 Java 实体 class。
public class Result {
String resultTitle;
String resultDecription;
}
This class is returned in a GraphQL query此 class 在 GraphQL 查询中返回
@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.
现在我想在 GraphQL 查询中返回一个附加属性。 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:
所以我只想返回它,当 GraphQL 客户端在他的查询中实际请求它时,如下所示:
query { getResult() { resultTitle resultDecription } }
<== do not execute the costly calculation query { getResult() { resultTitle resultDecription } }
<== 不执行代价高昂的计算query { getResult() { resultTitle resultDecription costlyAdditionalProp } }
<== DO execute the costly calculation query { getResult() { resultTitle resultDecription costlyAdditionalProp } }
<== DO 执行代价高昂的计算Can this be done with graphql-spqr?这可以用graphql-spqr完成吗?
Oh, that was actually quite simple.哦,那其实很简单。 Just needed to dig up the right example from the graphql-spqr-examples .
只需要从graphql-spqr-examples中挖掘出正确的示例。
@GraphQLQuery(name = "costlyAdditionalProp ")
public Long getCostlyAdditionalProp (@GraphQLContext Result result) {
return calculationService.doCalculation(result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.