我正在使用 hasura 和 graphql,我想创建一个查询,该查询使用多个“where _or”。遗憾的是,它没有按预期工作。我读了很多文章、stackoverflow 评论等等,但我找不到任何有帮助的内容。
不起作用的查询(对其进行修改以使我的问题和我想要做的事情更容易理解):
query getBook($Id: Int!) {
book(
where: {
_or: [
{ writerId: { _eq: $Id } }
{ coverArtistId: { _eq: $Id } }
{ grammarCheckerGuyId: { _eq: $Id } }
{ publisherGuyId: { _eq: $Id } }
]
}
) {
id
titleOfTheBook
bookContent
priceOfTheBook
someOtherSomething1
someOtherSomething2
someOtherSomething3
# a line which is commented out for some reason
someOtherSomething4
someOtherSomething5
subThingInAGivenBook(
where: {
_or: [
{ subWorkerId: { _eq: $Id } }
{ subWorkersCoworkerId: { _eq: $Id } }
]
}
) {
id
thing1
thing2
}
}
}
因此,目标是列出所有这些“书籍”,其中 writerId、OR coverArtistId、OR GrammarCheckerGuyID、ORpublisherGuyID、OR subWorkerID、OR subworkersCoworkerID 等于输入 Id。
我猜上面的语法不合适。
是否可以通过一个查询来解决这个问题?
如果没有,也许我应该执行以下操作(但这个解决方案会有点困惑):
- 为每个“where _or”事物创建两个单独的查询
- 运行这两个查询
- 以某种方式合并两个查询的结果(消除重复项,如果存在的话)
- 完成 (但我希望我可以避免这种情况,这不是一个很好的解决方案。)
预先感谢您的帮助。
最佳答案
在我同事的帮助下,我们找到了答案。可以修改查询以使其按预期工作。需要使用给定的语法将所有内容放在相同的 where-or 中。
修复查询:
query getBook($Id: Int!) {
book(
where: {
_or: [
{ writerId: { _eq: $Id } }
{ coverArtistId: { _eq: $Id } }
{ grammarCheckerGuyId: { _eq: $Id } }
{ publisherGuyId: { _eq: $Id } }
{ subThingInAGivenBook: { subWorkerId: { _eq: $Id } } }
{ subThingInAGivenBook: { subWorkersCoworkerId: { _eq: $Id } } }
]
}
) {
id
titleOfTheBook
bookContent
priceOfTheBook
someOtherSomething1
someOtherSomething2
someOtherSomething3
# a line which is commented out for some reason
someOtherSomething4
someOtherSomething5
subThingInAGivenBook{
id
thing1
thing2
}
}
}
因此,在原始代码中存在两个不同的 where-or 事物,它们在上面的查询中合并为仅一个 where-or 事物。
如果嵌套更深,它也有效。
如果您想要合并的不是两个而是三个(或更多)位置或事物,它也适用。
关于postgresql - 如何使用多个 where _or 创建 hasura graphql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70945531/