繁体   English   中英

Graphql java 嵌套查询解析器

[英]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;
   }

}

让我再解释一下有关上述解析器的代码:

  • 您为您的账户 DTO 创建一个新的解析器;
  • 它有一个方法与 Account 中的 GraphQL 字段具有完全相同的签名,即: accountTransactions ,返回AccountTransaction集合

至于Java DTO

  • 它指定了您的 GraphQL 类型的所有字段,但不是您希望解析器解析的字段。

SpringBoot GraphQL 将自动装配解析器,如果客户端询问有关帐户及其交易的详细信息,它将被调用。

假设您已经定义了一个名为accounts的 GraphQL 查询返回所有账户,以下 GraphQL 查询将是有效的:

{
    accounts {
        accountNumber
        accountTransactions {
            amount
            date
            annualFee
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM