skrtz
skrtz

Reputation: 1

How do I write a query using "_and" & "_or" at the same time

I'm trying to write a query that takes a start date and end date to filter out a list of events to a specific date range. At the same time, I also would like to filter for fields that include whatever the user provides (in my example code I chose fields like "title" or "location"). My application is an Event Calendar and I want this query to be used with a filter pane component where each input in the filter pane can be used to dynamically get a filtered list of data.

query MyQuery {
  events(where: {
    _and: [
      {
        _or: [
          { title: {_ilike: "%test title%"} },
          { location: {_ilike: "%test location%"} }
        ]
      },
      { start_date_time_utc: {_gte: "2024-01-08T04:05:06.605+00:00"} },
      { end_date_time_utc: {_lte: "2024-02-28T21:52:09+00:00"}}
    ]
  }
    
  ) {
    id
    title
    start_date_time_utc
  }
}

My issue is the above query works fine if I only use the "_and" operator or the "_or" operator but if I try to use them together, I'm not getting anything. I've tried it a few different ways and feel like I'm missing something syntactically. I'm new to GraphQL and Hasura so it could be something simple (and I hope it is tbh), but I haven't seen any documentation on how to use them together.

Upvotes: 0

Views: 138

Answers (2)

Geetha Pannirselvam
Geetha Pannirselvam

Reputation: 11

query MyQuery($whereSearch: events_bool_exp!) {
events(where: { _and: [$whereSearch] }) {
id
title
location
start_date_time_utc
end_date_time_utc
}
}

{
whereSearch: {
_or: [
{ title: { _ilike: "%test title%" } }
{ location: { _ilike: "%test location%" } }
]
start_date_time_utc: { _gte: "2024-01-08T04:05:06.605+00:00" }
end_date_time_utc: { _lte: "2024-02-28T21:52:09+00:00" }
}
}

So, by using variables you can filter dynamically from your table. Variable generation needs to be handled by your code.

Upvotes: 0

Abdullah Saleem
Abdullah Saleem

Reputation: 3811

I don't have access to schema but something like this should work

query MyQuery {
  events(
    where: {
      _or: [
        { title: { _ilike: "%test title%" } }
        { location: { _ilike: "%test location%" } }
      ]
      start_date_time_utc: { _gte: "2024-01-08T04:05:06.605+00:00" }
      end_date_time_utc: { _lte: "2024-02-28T21:52:09+00:00" }
    }
  ) {
    id
    title
    start_date_time_utc
  }
}

Upvotes: 0

Related Questions