【问题标题】:Graphql java nested query resolverGraphql java嵌套查询解析器
【发布时间】:2019-10-14 02:27:37
【问题描述】:

我有使用 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。

【问题讨论】:

    标签: java spring-boot graphql


    【解决方案1】:

    您是否考虑过按照 link 中对 BookResolver 的说明实现 GraphQLResolver?

    如果你阅读了上面的链接,你应该能够写出这样的东西:

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

    【讨论】:

    • 如果更深一层怎么办?如果 accountTransactions 有另一个实体需要检索呢?
    猜你喜欢
    • 2018-08-25
    • 2019-09-12
    • 2021-01-29
    • 2020-12-02
    • 2022-06-12
    • 2015-02-08
    • 2021-07-28
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多