[英]graphql-spqr: How to add a GraphQL property to an entity
我有一個基本的 Java 實體 class。
public class Result {
String resultTitle;
String resultDecription;
}
此 class 在 GraphQL 查詢中返回
@GraphQLQuery(name = "getResult")
public Result getResult() {
Result result = //get result, e.g. from DB
return result;
}
到目前為止,這工作正常。 現在我想在 GraphQL 查詢中返回一個附加屬性。 但是該附加屬性的計算成本非常高。 所以我只想返回它,當 GraphQL 客戶端在他的查詢中實際請求它時,如下所示:
query { getResult() { resultTitle resultDecription } }
<== 不執行代價高昂的計算query { getResult() { resultTitle resultDecription costlyAdditionalProp } }
<== DO 執行代價高昂的計算這可以用graphql-spqr完成嗎?
哦,那其實很簡單。 只需要從graphql-spqr-examples中挖掘出正確的示例。
@GraphQLQuery(name = "costlyAdditionalProp ")
public Long getCostlyAdditionalProp (@GraphQLContext Result result) {
return calculationService.doCalculation(result);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.