首页 > 解决方案 > 是否可以在 hasura-graphql 中将运算符作为变量/参数传递

问题描述

我有一个要求,我需要基于输入的两个运算符(_and 和 _or)。

那么是否可以在 hasura graphql 中将运算符作为变量/参数传递?

我正在传递“匹配”变量,要求是我应该能够根据一些点击传递“_or”或“_and”,如果可能的话,请写下操作员的“类型”。

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

标签: graphqlhasura

解决方案


您可以根据需要构建整个 where 对象;您可以执行以下操作:

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%"
        }
      }
    ]
  }
}

推荐阅读