gf000
gf000

Reputation: 93

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". 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.

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.

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):

Thank you in advance for your help.

Upvotes: 3

Views: 4823

Answers (1)

gf000
gf000

Reputation: 93

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.

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.

Upvotes: 4

Related Questions