0

I am trying to make a query where I can add filters with multiple options. I currently have this query

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
      }
    }
  }

I am trying to query by status_id. I am trying to implement a filter on front-end which can fetch all the checked options based on a modal with a checkbox.

For example I want to fetch records with status_id 1 and 2, I am looking for something like this

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

But I am not able to execute them. I need help in modifying this query such way.

Usman Tahir
  • 2,513
  • 4
  • 24
  • 38
  • it's not only frontend ... api has to support all your needs ... `eq` ususally means ONE value – xadm Apr 28 '20 at 19:33
  • I am beginner, I don't really know how to add it to API – Usman Tahir Apr 28 '20 at 20:17
  • I mean GQL doesn't have support for this? I can modify my query if I have to. I just need to filter based on statuses. – Usman Tahir Apr 28 '20 at 20:18
  • GQL standard doesn't define that (filtering at all) - it's implementation related ... read docs.... `_in` for set ? nested `_or` (id eq 1 or id eq 2) – xadm Apr 28 '20 at 20:58

1 Answers1

0

In your query you need to specify the correct type, auto generated by Hasura. In the Hasura console, GraphiQL tab, there is a documentation explorer tab. There you can see the types generated by Hasura for each query.

HasuraQueryExplorer

You will need to check in your Hasura console, but your query should be something like:

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
      }
    }
  }

and you can build the where as needed.

Leonardo Alves
  • 1,876
  • 1
  • 16
  • 19
  • Yes, so far I am following the documentation. But I am not really able to find if I have to add _or or _and for a single field. I can change the query if I have to – Usman Tahir Apr 28 '20 at 20:19