Reputation: 45
I have requirements graph nested query with java resolver.
getAccounts(type: "01",transactionMonths: 12){
accountNumber,
openDate,
productType,
accountTransactions(annualFee: True){
amount,
date
}
}
How can we write query in graphql and how to write java resolver for nested query. How to fetch the nested query arguments to to pass to my jparepository. i have Account type and Transactions type as below
type Account{
accountNumber: String
openDate: String
type: String
transactionMonths: String
productType: String
accountTransactions:[AccountTransaction]
}
type AccountTransaction{
amount: String
date:String
annualFee:Boolean
}
How can i retrive the accountTransactions in accounts using nested query using java resolver.
Upvotes: 1
Views: 4465
Reputation: 949
Did you look into implementing a GraphQLResolver as explained for the BookResolver in this link ?
If you read the above link, you should be able to write something like this:
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
}
}
As for your java DTO, you should have something like this:
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;
}
}
Let me explain a bit more the code concerning the resolver above:
As for the Java DTO:
SpringBoot GraphQL will autowire the resolver, and it will be called if the client asked for details on the account and its transactions.
Assuming that you have defined a GraphQL query called accounts that returns all accounts, the following GraphQL query would be valid:
{
accounts {
accountNumber
accountTransactions {
amount
date
annualFee
}
}
}
Upvotes: 1