简体   繁体   English

如何使用多个 where _or 创建 hasura graphql 查询?

[英]How to create hasura graphql query with multiple where _or?

I am using hasura and graphql, and I want to create a query, which is using multiple "where _or".我正在使用 hasura 和 graphql,并且我想创建一个使用多个“where _or”的查询。 Sadly, it is not working as expected.可悲的是,它没有按预期工作。 I read a lot of articles, stackoverflow comments, and many more, but I couldn't find anything what could help.我阅读了很多文章、stackoverflow 评论等等,但我找不到任何可以提供帮助的东西。

The query which is not working (it is modified to make more understandable my problem and what I want to do):不工作的查询(它被修改以使我的问题和我想要做的更容易理解):

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
        }
    }
}

So the goal is to list all of those "books", where the writerId, OR the coverArtistId, OR the grammarCheckerGuyID, OR publisherGuyID, OR subWorkerID, OR subworkersCoworkerID is equal to the input Id.因此,目标是列出所有这些“书籍”,其中 writerId 或 coverArtistId 或grammarCheckerGuyID 或 publisherGuyID 或 subWorkerID 或 subworkersCoworkerID 等于输入 ID。

I guess that the above syntax is not the appropriate one.我猜上面的语法不合适。

Is it possible to solve this in one single query?是否可以在一个查询中解决此问题?

If not, maybe I should do the following (but this solution would be a little bit messy):如果没有,也许我应该执行以下操作(但这个解决方案会有点混乱):

  • create two separate queries, for each "where _or" things为每个“where _or”事物创建两个单独的查询
  • run those two queries运行这两个查询
  • somehow merge the results of the two queries (eliminate the duplicates, if they exist)以某种方式合并两个查询的结果(消除重复项,如果它们存在)
  • done (But I hope I can avoid this, this would be not that pretty solution.)完成(但我希望我可以避免这种情况,这不是那么好的解决方案。)

Thank you in advance for your help.预先感谢您的帮助。

With the help of my coworker we found the answer.在我同事的帮助下,我们找到了答案。 It is possible to modify the query to make it work as expected.可以修改查询以使其按预期工作。 It is needed to put everything in the same where-or with a given syntax.需要将所有内容放在相同的位置或使用给定的语法中。

Fixed query:固定查询:

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
        }
    }
}

So in the original code there were two different where-or things, and they are merged in the above query into only one where-or thing.所以在原始代码中有两个不同的 where-or 事物,它们在上面的查询中被合并成一个 where-or 事物。

It also works if the nesting is much more deep.如果嵌套更深,它也可以工作。

It also works if you want to merge not two but three (or more) where-or things.如果您想要合并的不是两个而是三个(或更多)where-or 事物,它也可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM