use async_graphql::{
InputObjectType, InputType, InputValueError, InputValueResult, Object, OutputType, Scalar,
ScalarType, TypeName, Value,
registry::{MetaInputValue, MetaType, MetaTypeId, Registry},
};
use std::borrow::Cow;
use crate::{NullableFace, ScalarFilter, Sent, Timestamp, ToGraphqlOutput, sent};
use super::{
Contained, ContainedOperand, TimeRange, TimeRangeFilter, TimeRangeOperand, TimeRanged,
TimeRangedFilter,
storage::{END, START},
};
#[cfg(test)]
mod tests;
const NO_RANGE_SHORTHAND: &str = "a time range filter takes a NAMED interval word — `overlaps`, `contains` or `within` — or a \
`start`/`end` component filter: a bare range does not say which of the three was meant";
const NO_RANGED_SHORTHAND: &str = "a timed filter takes `range`, `data`, or both: a bare value does not say which half of the pair \
it is asking about";
#[Object]
impl TimeRange {
#[doc(hidden)]
#[graphql(name = "start")]
async fn __async_graphql_start__(&self) -> Timestamp {
self.start()
}
#[doc(hidden)]
#[graphql(name = "end")]
async fn __async_graphql_end__(&self) -> Timestamp {
self.end()
}
}
impl InputType for TimeRangeOperand {
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("TimeRangeOperand")
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::InputObject, |registry| {
MetaType::InputObject {
name: String::from("TimeRangeOperand"),
description: None,
input_fields: crate::__ingraph_filter_input_fields!(
registry,
"start" : Timestamp,
"end" : Timestamp,
),
visible: None,
inaccessible: false,
tags: Vec::new(),
rust_typename: Some(std::any::type_name::<Self>()),
oneof: false,
directive_invocations: Vec::new(),
}
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let object = match sent(value) {
Sent::Longhand(object) => object,
Sent::Bare(bare) => return Err(InputValueError::expected_type(bare)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
let start = <Timestamp as InputType>::parse(object.get(START.as_str()).cloned())
.map_err(InputValueError::propagate)?;
let end = <Timestamp as InputType>::parse(object.get(END.as_str()).cloned())
.map_err(InputValueError::propagate)?;
TimeRange::new(start, end)
.map(Self::new)
.map_err(InputValueError::custom)
}
fn to_value(&self) -> Value {
let range = self.get();
let mut object = async_graphql::indexmap::IndexMap::new();
object.insert(
async_graphql::Name::new(START.as_str()),
InputType::to_value(&range.start()),
);
object.insert(
async_graphql::Name::new(END.as_str()),
InputType::to_value(&range.end()),
);
Value::Object(object)
}
#[inline]
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}
impl InputObjectType for TimeRangeOperand {}
#[Scalar(name = "ContainedOperand")]
impl ScalarType for ContainedOperand {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(text) => contained(text)
.map(Self::new)
.map_err(InputValueError::custom),
_ => Err(InputValueError::expected_type(value.clone())),
}
}
#[inline]
fn is_valid(value: &Value) -> bool {
matches!(value, Value::String(text) if contained(text).is_ok())
}
fn to_value(&self) -> Value {
Value::String(match self.get() {
Contained::Instant(at) => at.to_string(),
Contained::Span(span) => span.to_string(),
})
}
}
fn contained(text: &str) -> Result<Contained, String> {
let text = text.trim();
if text.starts_with('[') {
return text
.parse::<TimeRange>()
.map(Contained::Span)
.map_err(|refused| refused.to_string());
}
text
.parse::<Timestamp>()
.map(Contained::Instant)
.map_err(|refused| refused.to_string())
}
fn range_fields(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue> {
crate::__ingraph_filter_input_fields!(
registry,
"overlaps" : Option<TimeRangeOperand>,
"contains" : Option<ContainedOperand>,
"within" : Option<TimeRangeOperand>,
"start" : Option<ScalarFilter<Timestamp>>,
"end" : Option<ScalarFilter<Timestamp>>,
)
}
fn range_values(
filter: &TimeRangeFilter,
) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value> {
crate::__ingraph_filter_value_map!(
filter,
"overlaps" : overlaps,
"contains" : contains,
"within" : within,
"start" : start,
"end" : end,
)
}
impl InputType for TimeRangeFilter {
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("TimeRangeFilter")
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::InputObject, |registry| {
MetaType::InputObject {
name: String::from("TimeRangeFilter"),
description: None,
input_fields: <Self as NullableFace>::bare_fields(registry),
visible: None,
inaccessible: false,
tags: Vec::new(),
rust_typename: Some(std::any::type_name::<Self>()),
oneof: false,
directive_invocations: Vec::new(),
}
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let object = match sent(value) {
Sent::Longhand(object) => object,
Sent::Bare(_) => return Err(InputValueError::custom(NO_RANGE_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
overlaps: InputType::parse(object.get("overlaps").cloned())
.map_err(InputValueError::propagate)?,
contains: InputType::parse(object.get("contains").cloned())
.map_err(InputValueError::propagate)?,
within: InputType::parse(object.get("within").cloned())
.map_err(InputValueError::propagate)?,
start: InputType::parse(object.get("start").cloned()).map_err(InputValueError::propagate)?,
end: InputType::parse(object.get("end").cloned()).map_err(InputValueError::propagate)?,
})
}
fn to_value(&self) -> Value {
Value::Object(<Self as NullableFace>::bare_values(self))
}
#[inline]
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}
impl InputObjectType for TimeRangeFilter {}
impl NullableFace for TimeRangeFilter {
fn bare_fields(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue> {
range_fields(registry)
}
fn bare_values(&self) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value> {
range_values(self)
}
}
impl<T> TypeName for TimeRanged<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"TimeRanged{}",
<T::Output<'static> as OutputType>::type_name()
))
}
}
#[Object(name_type)]
impl<T> TimeRanged<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "range")]
async fn __async_graphql_range__(&self) -> &TimeRange {
self.range()
}
#[doc(hidden)]
#[graphql(name = "data")]
async fn __async_graphql_data__(&self) -> T::Output<'_> {
T::to_graphql_output(self.data())
}
}
impl<T> crate::ToGraphqlOutput for TimeRanged<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
fn ranged_fields<TF>(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue>
where
TF: InputType,
{
crate::__ingraph_filter_input_fields!(
registry,
"range" : Option<TimeRangeFilter>,
"data" : Option<TF>,
)
}
fn ranged_values<TF>(
filter: &TimeRangedFilter<TF>,
) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value>
where
TF: InputType,
{
crate::__ingraph_filter_value_map!(filter, "range" : range, "data" : data)
}
impl<TF> InputType for TimeRangedFilter<TF>
where
TF: InputType,
{
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("TimeRanged{}", <TF as InputType>::type_name()))
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::InputObject, |registry| {
MetaType::InputObject {
name: String::from(<Self as InputType>::type_name()),
description: None,
input_fields: <Self as NullableFace>::bare_fields(registry),
visible: None,
inaccessible: false,
tags: Vec::new(),
rust_typename: Some(std::any::type_name::<Self>()),
oneof: false,
directive_invocations: Vec::new(),
}
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let object = match sent(value) {
Sent::Longhand(object) => object,
Sent::Bare(_) => return Err(InputValueError::custom(NO_RANGED_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
range: InputType::parse(object.get("range").cloned()).map_err(InputValueError::propagate)?,
data: InputType::parse(object.get("data").cloned()).map_err(InputValueError::propagate)?,
})
}
fn to_value(&self) -> Value {
Value::Object(<Self as NullableFace>::bare_values(self))
}
#[inline]
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}
impl<TF: InputType> InputObjectType for TimeRangedFilter<TF> {}
impl<TF: InputType> NullableFace for TimeRangedFilter<TF> {
fn bare_fields(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue> {
ranged_fields::<TF>(registry)
}
fn bare_values(&self) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value> {
ranged_values(self)
}
}