在 Hyperledger Composer 中使用关系查询

Query using relation in Hyperledger Composer

我在 Hyperledger Composer 中进行了查询,其中查询试图搜索借款人的所有发票。借款人是发票资产的参与者:

asset Invoice identified by invoiceId {
  o String invoiceId
  o String invoiceRef optional
  o DateTime dateCreated
  o String type
  o DateTime invoiceOrPurchaseDate optional
  o Double amount
  o DateTime invoiceDueDate optional
  o String paymentStatus optional
  o Boolean overdue
  o Double outstandingBalance
  --> Participant borrower
  --> Participant lender
}

我需要一个查询 return 借款人的所有发票,我在 Hyperledger composer 中通过以下编码完成了此操作:

query QInvoiceByBorrower {
    description: "Select invoice by borrower"
    statement:
        SELECT org.n.blockchaindemo.Invoice
            WHERE (_$borrower == borrower)
}

但是当我尝试通过 REST API 调用查询时,我得到如下 [] 空结果:

http://10.10.4.244:3000/api/queries/QInvoiceByBorrower?borrower=resource:org.n.blockchaindemo.User#1381991

我可以知道如何创建将在 Hyperledger Composer 中使用外部关系进行搜索的查询吗?

在发票定义中,您需要引用特定的参与者类型,即 User 而不是系统类型参与者。因此,您的发票的最后一部分将是:

  o Double outstandingBalance
  --> User borrower
  --> User lender
 }

通过此更改,您的查询应该可以正常工作。一种有用的诊断方法是创建不带 where 子句的重复查询,然后检查返回的数据以帮助理解用于查询的参数。

(通过使用 Participant,我认为您的资产将包括首次使用资产类型时模型中的第一个参与者类型 - 这听起来可能会产生一些意外行为,因此指定实际参与者类型是有意义的.)