首页 > 解决方案 > 日期之间的 Hasura graphql 查询

问题描述

在我的 hasura.io 数据库中,我有一个booking表,我在其中存储具有以下属性的预订:

现在,从 UI 中,当用户进行预订时,我想检查之前是否有任何涉及这些日期范围的预订。

例如。用户希望使用以下日期进行预订:

{
  "from": "2020-03-31T14:00:00+00:00",
  "to": "2020-03-31T17:00:00+00:00"
}

但在同一天有一个预订:

{
  "from": "2020-03-31T15:00:00+00:00",
  "to": "2020-04-01T17:00:00+00:00"
}

请注意,此预订不在用户尝试预订的日期范围内。因此,以下查询将不返回任何内容。

query CheckBooking($from: timestamptz!, $to: timestamptz!) {
  booking(where: {_and: [{from: {_lte: $from}}, {to: {_gte: $to}}]}) {
    id
    from
    to
  }
}

以上是我尝试过但没有成功的方法。任何人都可以帮助找出正确的查询来检查范围内或用户新预订中是否有任何预订?

标签: graphqlreact-apollogqlhasura

解决方案


我们想分别考虑两者fromto并将范围应用于两者。因此,您可以执行以下操作:

query CheckBooking($from: timestamptz!, $to: timestamptz!) {
  booking(where: {_or: [
    {_and: [{from: {_gte: $from}}, {from: {_lt: $to}}]},
    {_and: [{to: {_gt: $from}}, {to: {_lte: $to}}]},
  ]}) {
    id
    from
    to
  }
}

推荐阅读