![](/img/trans.png)
[英]Java GraphQL Resolver for iterable object: Hibernate Exception
[英]Graphql java nested query resolver
我有使用 java 解析器的需求图嵌套查询。
getAccounts(type: "01",transactionMonths: 12){
accountNumber,
openDate,
productType,
accountTransactions(annualFee: True){
amount,
date
}
}
我们如何在 graphql 中编写查询以及如何为嵌套查询编写 java 解析器。 如何获取嵌套的查询参数以传递给我的 jparepository。 我有账户类型和交易类型如下
type Account{
accountNumber: String
openDate: String
type: String
transactionMonths: String
productType: String
accountTransactions:[AccountTransaction]
}
type AccountTransaction{
amount: String
date:String
annualFee:Boolean
}
如何使用 Java 解析器使用嵌套查询检索帐户中的 accountTransactions。
您是否考虑过实现 GraphQLResolver,如此链接中对 BookResolver 的解释?
如果你阅读了上面的链接,你应该能够写出这样的东西:
public class AccountResolver implements GraphQLResolver<Account> {
public Collection<AccountTransaction> accountTransactions(Account account,Boolean annualFee) {
// put your business logic here that will call your jparepository
// for a given account
}
}
至于你的java DTO,你应该有这样的东西:
public class Account {
private String accountNumber;
private String openDate;
private String type;
private String transactionMonths;
private String productType;
// Don't specify a field for your list of transactions here, it should
// resolved by our AccountResolver
public Account(String accountNumber, String openDate, String type, String transactionMonths, String productType) {
this.accountNumber = accountNumber;
this.openDate = openDate;
this.type = type;
this.transactionMonths = transactionMonths;
this.productType = productType;
}
public String getAccountNumber() {
return accountNumber;
}
public String getOpenDate() {
return openDate;
}
public String getType() {
return type;
}
public String getTransactionMonths() {
return transactionMonths;
}
public String getProductType() {
return productType;
}
}
让我再解释一下有关上述解析器的代码:
至于Java DTO :
SpringBoot GraphQL 将自动装配解析器,如果客户端询问有关帐户及其交易的详细信息,它将被调用。
假设您已经定义了一个名为accounts的 GraphQL 查询返回所有账户,以下 GraphQL 查询将是有效的:
{
accounts {
accountNumber
accountTransactions {
amount
date
annualFee
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.