swapna
swapna

Reputation: 45

Graphql java nested query resolver

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

Answers (1)

D. Lawrence
D. Lawrence

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:

  • 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
        }
    }
}

Upvotes: 1

Related Questions