Hasura-query for tags-null数组应该返回所有结果,但只返回带有标签的项



我有一个查询:

query SearchProductData($tagId: [Int!]) {
product(where: { productTags: {_or: {tagId: {_in: $tagId}}}}) {
id
name
productTags {
tag {
id
name
}
}
}
}

然后我传入的一个变量

{"tagId": null}

我想取回所有的产品,不管它们是否应用了标签。然而,它只检索应用了标记的项目,而不包括没有标记的项目。

DB模式是

|- product
|
|-- productTags (one to many linking table of productIDs and tagIDs)
|
|--- tags

有什么想法可以为这个用例编写查询吗?

这是意料之中的事,因为where子句是如何转换为联接的(您可以通过单击GraphiQL控制台中的分析来查看生成的SQL和执行计划查询分析。(

我认为如果不注入bool表达式是不可能的。见下文:

例如

query Test($expression: product_bool_exp) {
product(where: $expression) {
id
name
product_tags {
tag {
id
name
}
}
}
}

带自变量

{
"expression": {"product_tags": {"tag_id": {"_in": [2]}}}
}

返回:

{
"data": {
"product": [
{
"id": 1,
"name": "product_A",
"product_tags": [
{
"tag": {
"id": 1,
"name": "tag_1"
}
},
{
"tag": {
"id": 2,
"name": "tag_2"
}
}
]
}
]
}
}

对于使用相同查询的另一种情况(其中没有传入标签,因此所有产品都可以使用此变量值:

{
"expression": null
}

我们回来了。。。

{
"data": {
"product": [
{
"id": 1,
"name": "product_A",
"product_tags": [
{
"tag": {
"id": 1,
"name": "tag_1"
}
},
{
"tag": {
"id": 2,
"name": "tag_2"
}
}
]
},
{
"id": 4,
"name": "product_D",
"product_tags": []
}
]
}
}

因此,您可以动态地构造where表达式,并将其作为参数传递给查询。

最新更新