【问题标题】:Adding multiple options in against single column in GraphQL在 GraphQL 中针对单个列添加多个选项
【发布时间】:2020-08-12 16:59:56
【问题描述】:

我正在尝试进行查询,我可以在其中添加具有多个选项的过滤器。我目前有这个查询

query FacilitiesQuery(
    $limit: Int
    $offset: Int
    $query: String
    $sort: [facilities_order_by!]
  ) {
    facilities(
      limit: $limit
      offset: $offset
      where: {
        _or: [
          { name: { _ilike: $query } },
          { location: { _ilike: $query } },
        ]
        _and: [
          { deleted_at: { _is_null: true } },
          { status_id: { _eq: 1 } }
        ]
      }
      order_by: $sort
    ) {
      id
      location
      name
      status_id
      updated_at
      created_at
      area
      floor
      handover_condition
      handover_meter
      handover_office
      lcd
      level
      move_in_date
      rcd
      company_id
      deleted_at
      expiry_date
      area_type
      building_name
      enum_area_type {
        id
        name
      }
    }
    enum_facility_statuses {
      id
      value
    }
    facilities_aggregate(where: { deleted_at: { _is_null: true } }) {
      aggregate {
        count
      }
    }
  }

我正在尝试通过status_id 进行查询。我正在尝试在前端实现一个过滤器,它可以根据带有复选框的模式获取所有选中的选项。

例如我想获取 status_id 为 1 和 2 的记录,我正在寻找这样的东西

_and: [
  { deleted_at: { _is_null: true } },
  { status_id: { _eq: [1,2] } }
]

但我无法执行它们。我需要帮助以这种方式修改此查询。

【问题讨论】:

  • 它不仅仅是前端...... api 必须支持你的所有需求...... eq 通常意味着一个值
  • 我是初学者,我真的不知道如何将它添加到API中
  • 我的意思是 GQL 不支持这个?如果需要,我可以修改我的查询。我只需要根据状态进行过滤。
  • GQL 标准没有定义(过滤)——它与实现相关......阅读文档......_in 设置?嵌套_or(id eq 1 或 id eq 2)

标签: reactjs graphql react-apollo hasura


【解决方案1】:

在您的查询中,您需要指定正确的类型,由 Hasura 自动生成。在 Hasura 控制台的 GraphiQL 选项卡中,有一个文档资源管理器选项卡。在那里你可以看到 Hasura 为每个查询生成的类型。

您需要检查 Hasura 控制台,但您的查询应该类似于:

query FacilitiesQuery(
    $limit: Int
    $offset: Int
    $where: facilities_bool_exp!
    $sort: [facilities_order_by!]
  ) {
    facilities(
      limit: $limit
      offset: $offset
      where: $where
      }
      order_by: $sort
    ) {
      id
      location
      name
      status_id
      updated_at
      created_at
      area
      floor
      handover_condition
      handover_meter
      handover_office
      lcd
      level
      move_in_date
      rcd
      company_id
      deleted_at
      expiry_date
      area_type
      building_name
      enum_area_type {
        id
        name
      }
    }
    enum_facility_statuses {
      id
      value
    }
    facilities_aggregate(where: { deleted_at: { _is_null: true } }) {
      aggregate {
        count
      }
    }
  }

您可以根据需要在哪里构建。

【讨论】:

  • 是的,到目前为止,我正在关注文档。但是我真的无法找到是否必须为单个字段添加 _or 或 _and 。如果需要,我可以更改查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 2016-06-11
相关资源
最近更新 更多