1

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.

1 Answer 1

1

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:

  • You create a new resolver for your Account DTO;
  • It has one method with the exact same signature as your GraphQL field in Account, that is: accountTransactions, returning a collection of AccountTransaction.

As for the Java DTO:

  • It specify all the fields of your GraphQL type but NOT the field you want your resolver to resolve.

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if it goes one level deeper? what if accountTransactions has another entity that needs to be retrieved?
And now if I have another structure inside the accountTransactions, say destinations ,which is a complex structure and for resolving it I need the field accountNumber, how can I resolve this deeply nested query? I have a case for which I am searching for an effective solution. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.