我正在为汽车库存实现一个 GraphQL API,它存储注册、inDate、outDate 等信息。
我需要实现一个获取 API 来检索所有进出的汽车的信息
假设电流看起来像
scalar Date
input QueryArguments {
registration: string
inDate: unkownType
outDate: unkownType
}
type Query {
automobiles(input: QueryArguments!): [Automobile]
}
type Automobile{
registration: string
inDate: Date
outDate: Date
}
GraphQL 输入字段支持单个日期和日期范围的标准/可接受的方式是什么。
我发现了一篇 stackoverlow 文章,其中建议使用自定义类型
type DateRangeInput {
start: Date
end: Date
}
它适用于日期范围,但如果需要提供单个日期,我们应该以
start
或结束 field
发送它,或者引入除这些之外的不同字段,但在单个日期场景中看起来并不干净.
输入类型中的所有字段目前都是可选的。您可以添加:
scalar Date
input QueryArguments {
registration: string
inDate: Date
outDate: Date
start: Date
end: Date
}
type Query {
automobiles(input: QueryArguments!): [Automobile]
}
然后您可以使用任意参数组合进行查询:
query myQuery($input: QueryArguments!) {
Query(input: $input) {
… automobile fields
}
}
QueryArguments
那么可以是:
{ start: '2024-01-01', end: '2024-01-31'}
或:
{ inDate: '2024-01-09' }
或任何组合。您的解析器代码将负责确保如果提供了
start
,则 end
也必须在那里(如果这就是您想要的)。