0

I have a requirement where I need two operators(_and and _or) based on the inputs.

So is it possible to pass operator as variable/parameter in hasura graphql?

I am passing "match" variable, the requirement is I should be able to pass "_or" or "_and" based on the some click and also if this is possible then please write down the "Type" of the operator.

query Search($match: String) {
  restaurants(where: {_or: [{cuisine: {_ilike: $match}}, {name: {_ilike: $match}}]}) {
    cuisine
    id
    name
    reviews {
      body
    }
  }
}
#variable
{
  "match":"%woodland%"
}
Amrutha VS
  • 144
  • 1
  • 1
  • 10

1 Answers1

4

you can construct the whole where object depending on your needs; you can do something like:

query($match: restaurants_bool_exp!) {
  restaurants(where: $match) {
    id
    name
    cuisine
    reviews {
      body
    }
  }
}

#variables_1

{
  "match": {
    "_or": [
      {
        "name": {
          "_ilike": "%user entered value%"
        }
      },
      {
        "cuisine": {
          "_ilike": "%user entered value%"
        }
      }
    ]
  }
}

#variables_2

{
  "match": {
    "_and": [
      {
        "name": {
          "_ilike": "%user entered value%"
        }
      },
      {
        "cuisine": {
          "_ilike": "%user entered value%"
        }
      }
    ]
  }
}
  • Can you please be more clear, please let me know what you mean by "restaurants_bool_exp" (there is not "type" with this name) and can you format the variables as I am getting "Variable expected to be "string". – Amrutha VS Nov 05 '20 at 07:36
  • 1
    In graphql, the where argument can accept a "Boolean expression". And "restaurants_bool_exp" is a type used to filter rows from the table `restaurants`. It's mostly "table_name_bool_exp" unless you renamed your table. paste the above query in hasura console and hover on the error so it will show you the right type. – Muluken Getachew Nov 05 '20 at 10:27