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
相关产品推荐
相关产品推荐

