You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hot Chocolate V13过滤功能不符合预期问题咨询

Hot Chocolate V13 过滤不符合预期问题

我正在使用Hot Chocolate V13的过滤功能,但未得到预期结果。

现有JSON数据:

{"id": "b3d7768e-7a22-4109-9511-cdd17585267b","createdDateTime": "2023-06-01T21:02:34.4800722+00:00","earliestPaymentDate": "2023-05-31T07:00:00+00:00","itemNumber": "abc123456","lineItems": [{"dueDate": "2023-07-01T06:59:59+00:00","eventId": null,"itemState": "Mismatched","itemUpdateDateTime": "2023-06-01T21:07:26.284906+00:00","type": "Part"},{"dueDate": "2023-07-01T06:59:59+00:00","eventId": null,"itemState": "Mismatched","itemUpdateDateTime": "2023-06-01T21:07:26.2849904+00:00","type": null}]}

返回对象定义:

type EventDto {
  id: String
  itemNumber: String
  matchedEntries: [ItemDto]
  mismatchedEntries: [ItemDto]
}

其中matchedEntries和mismatchedEntries是基于itemState属性过滤得到的衍生字段。

执行以下查询时,由于and条件中的两个筛选条件都不匹配,我预期返回null或空结果,但实际仍返回2条记录:

query {
  itemDetailsByItemNumber(itemNumber: ["abc123456"],where: { 
    and: [
      { matchedEntries: { all: { itemState: { contains: "Mismatched" } } } },
      { matchedEntries: { all: { type: { contains: "Cars" } } } }
    ]
  }) {
    id
  }
}

请问:

  1. 我的查询是否存在错误?
  2. 过滤逻辑是否与我的预期不同?

问题解答

1. 查询存在明确错误

你的查询犯了两个核心错误:

  • 字段使用逻辑矛盾:matchedEntries是已经按itemState过滤出「匹配状态」的衍生字段,你却要求它的itemState包含"Mismatched",逻辑完全冲突。
  • 过滤对象选错:Hot Chocolate的过滤是针对数据源的原始实体字段,而非返回DTO中的衍生字段。你应该用原始的lineItems字段做过滤条件,而非二次处理后的matchedEntries。

2. 过滤逻辑确实和你的预期不同

Hot Chocolate的all操作符有个关键特性:当作用在空集合上时,会默认返回true(逻辑上的"虚真",空集合的所有元素天然满足任意条件)。

你的场景中,原始lineItems的itemState都是"Mismatched",所以matchedEntries是空集合。此时两个all条件都会返回true,and组合后结果还是true,再加上itemNumber匹配,自然会返回所有符合编号的记录,和你的预期完全相反。

正确的查询写法

直接针对原始的lineItems字段做过滤:

query {
  itemDetailsByItemNumber(itemNumber: ["abc123456"],where: { 
    and: [
      { lineItems: { all: { itemState: { contains: "Mismatched" } } } },
      { lineItems: { all: { type: { contains: "Cars" } } } }
    ]
  }) {
    id
    matchedEntries
    mismatchedEntries
  }
}

这样就能过滤出所有lineItems同时满足itemState包含"Mismatched"且type包含"Cars"的EventDto,因为没有符合条件的数据,会返回空结果,符合你的预期。


内容的提问来源于stack exchange,提问作者GThree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:07:45