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

Drupal GraphQL查询:如何按fieldParentSpec.targetId过滤节点?

当然可以实现!

作为GraphQL新手,你完全可以通过构造查询来根据fieldParentSpec.targetId的值过滤结果——只需要在原查询的filter条件里添加针对这个嵌套字段的过滤规则就行。

修改后的查询示例(直接指定targetId值)

nodeQuery(filter: {
  conditions: [
    { field: "type", value: "unit_descriptor", operator: EQUAL },
    { field: "fieldParentSpec.targetId", value: 1, operator: EQUAL }
  ]
}) {
  entities {
    ...on NodeUnitDescriptor {
      nid
      title
      fieldParentSpec { targetId }
    }
  }
}

更灵活的写法:使用变量

如果需要动态传入targetId的值,推荐用GraphQL变量来实现,这样查询更复用:

query GetUnitsByParentSpecId($targetId: Int!) {
  nodeQuery(filter: {
    conditions: [
      { field: "type", value: "unit_descriptor", operator: EQUAL },
      { field: "fieldParentSpec.targetId", value: $targetId, operator: EQUAL }
    ]
  }) {
    entities {
      ...on NodeUnitDescriptor {
        nid
        title
        fieldParentSpec { targetId }
      }
    }
  }
}

执行这个查询时,只需要传入变量参数{"targetId": 1},就能得到所有匹配该targetId的实体。

关键说明

  • 这里的核心是用fieldParentSpec.targetId作为字段路径,告诉GraphQL要过滤嵌套在fieldParentSpec下的targetId字段
  • 多个条件默认是**逻辑与(AND)**的关系,所以会同时满足type=unit_descriptor和targetId=指定值的条件
  • 从你返回的数据结构来看,你的GraphQL schema已经支持这种嵌套字段的过滤,直接用就没问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 18:47:54