graphql - 日期之间的 Hasura graphql 查询
问题描述
在我的 hasura.io 数据库中,我有一个booking
表,我在其中存储具有以下属性的预订:
- id(自增)
- 从(时间戳)
- 到(时间戳)
- 说明(文字)
现在,从 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
}
}
以上是我尝试过但没有成功的方法。任何人都可以帮助找出正确的查询来检查范围内或用户新预订中是否有任何预订?
解决方案
我们想分别考虑两者from
,to
并将范围应用于两者。因此,您可以执行以下操作:
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
}
}
推荐阅读
- node.js - 得到 UnhandledPromiseRejectionWarning: TimeoutError
- dart - 如何用不可为空的一个飞镖覆盖可空字段
- google-cloud-platform - 作为 GCP 数据融合的一部分,无法使用 6.4.1 中的 CDAP 微服务创建计算配置文件
- wolfram-mathematica - Wolfram Mathematica 中的 PadRight 函数有什么作用?
- html - 移动响应式网站的居中背景和元素
- java - 为什么 ActivityCompat.requestPermissions 不提示权限请求?
- python - django项目中apps.py、appconfig有什么用。如何以及为什么要使用它?有人可以简单地解释一下
- robotframework - 在表达式中使用变量时出现“未定义名称”错误
- c++ - 模板化整个班级的替代方案
- html - django 社交分享按钮图标