use super::{Field, FieldFuture, FieldValue, Object, TypeRef};
use crate::Value;
#[derive(Debug, Clone)]
pub struct DynamicPageInfo {
pub has_previous_page: bool,
pub has_next_page: bool,
pub start_cursor: Option<String>,
pub end_cursor: Option<String>,
}
impl DynamicPageInfo {
pub fn object_type() -> Object {
Self::object_type_named("PageInfo")
}
fn object_type_named(name: &str) -> Object {
Object::new(name)
.field(Field::new(
"hasPreviousPage",
TypeRef::named_nn(TypeRef::BOOLEAN),
|ctx| {
FieldFuture::new(async move {
let info = ctx.parent_value.try_downcast_ref::<DynamicPageInfo>()?;
Ok(Some(Value::from(info.has_previous_page)))
})
},
))
.field(Field::new(
"hasNextPage",
TypeRef::named_nn(TypeRef::BOOLEAN),
|ctx| {
FieldFuture::new(async move {
let info = ctx.parent_value.try_downcast_ref::<DynamicPageInfo>()?;
Ok(Some(Value::from(info.has_next_page)))
})
},
))
.field(Field::new(
"startCursor",
TypeRef::named(TypeRef::STRING),
|ctx| {
FieldFuture::new(async move {
let info = ctx.parent_value.try_downcast_ref::<DynamicPageInfo>()?;
Ok(info.start_cursor.as_ref().map(|c| Value::from(c.clone())))
})
},
))
.field(Field::new(
"endCursor",
TypeRef::named(TypeRef::STRING),
|ctx| {
FieldFuture::new(async move {
let info = ctx.parent_value.try_downcast_ref::<DynamicPageInfo>()?;
Ok(info.end_cursor.as_ref().map(|c| Value::from(c.clone())))
})
},
))
}
}
#[derive(Debug, Clone)]
pub struct DynamicEdge {
pub cursor: String,
pub node: Value,
pub extra_fields: Vec<(String, Value)>,
}
impl DynamicEdge {
pub fn new(cursor: impl Into<String>, node: impl Into<Value>) -> Self {
Self {
cursor: cursor.into(),
node: node.into(),
extra_fields: Vec::new(),
}
}
pub fn extra_field(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
self.extra_fields.push((name.into(), value.into()));
self
}
}
#[derive(Debug, Clone)]
pub struct DynamicConnection {
pub edges: Vec<DynamicEdge>,
pub page_info: DynamicPageInfo,
pub extra_fields: Vec<(String, Value)>,
}
impl DynamicConnection {
pub fn new(has_previous_page: bool, has_next_page: bool) -> Self {
Self {
edges: Vec::new(),
page_info: DynamicPageInfo {
has_previous_page,
has_next_page,
start_cursor: None,
end_cursor: None,
},
extra_fields: Vec::new(),
}
}
pub fn edge(mut self, edge: DynamicEdge) -> Self {
self.edges.push(edge);
self
}
pub fn extra_field(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
self.extra_fields.push((name.into(), value.into()));
self
}
pub fn computed_page_info(&self) -> DynamicPageInfo {
DynamicPageInfo {
has_previous_page: self.page_info.has_previous_page,
has_next_page: self.page_info.has_next_page,
start_cursor: self
.page_info
.start_cursor
.clone()
.or_else(|| self.edges.first().map(|e| e.cursor.clone())),
end_cursor: self
.page_info
.end_cursor
.clone()
.or_else(|| self.edges.last().map(|e| e.cursor.clone())),
}
}
pub fn builder(type_name: impl Into<String>) -> DynamicConnectionBuilder {
DynamicConnectionBuilder::new(type_name)
}
}
pub struct DynamicConnectionBuilder {
connection_name: String,
edge_name: String,
page_info_name: String,
node_type: TypeRef,
edge_fields: Vec<(String, TypeRef)>,
connection_fields: Vec<(String, TypeRef)>,
}
impl DynamicConnectionBuilder {
pub fn new(type_name: impl Into<String>) -> Self {
let type_name = type_name.into();
Self {
connection_name: format!("{}Connection", type_name),
edge_name: format!("{}Edge", type_name),
page_info_name: "PageInfo".to_string(),
node_type: TypeRef::named_nn(type_name),
edge_fields: Vec::new(),
connection_fields: Vec::new(),
}
}
pub fn connection_name(mut self, name: impl Into<String>) -> Self {
self.connection_name = name.into();
self
}
pub fn edge_name(mut self, name: impl Into<String>) -> Self {
self.edge_name = name.into();
self
}
pub fn page_info_name(mut self, name: impl Into<String>) -> Self {
self.page_info_name = name.into();
self
}
pub fn node_type_name(mut self, name: impl Into<String>) -> Self {
self.node_type = TypeRef::named_nn(name);
self
}
pub fn node_type(mut self, ty: impl Into<TypeRef>) -> Self {
self.node_type = ty.into();
self
}
pub fn edge_field(mut self, name: impl Into<String>, ty: impl Into<TypeRef>) -> Self {
self.edge_fields.push((name.into(), ty.into()));
self
}
pub fn connection_field(mut self, name: impl Into<String>, ty: impl Into<TypeRef>) -> Self {
self.connection_fields.push((name.into(), ty.into()));
self
}
pub fn connection_type_name(&self) -> &str {
&self.connection_name
}
pub fn edge_type_name(&self) -> &str {
&self.edge_name
}
pub fn page_info_type_name(&self) -> &str {
&self.page_info_name
}
pub fn objects(&self) -> Vec<Object> {
vec![
self.page_info_object(),
self.edge_object(),
self.connection_object(),
]
}
pub fn page_info_object(&self) -> Object {
DynamicPageInfo::object_type_named(&self.page_info_name)
}
pub fn edge_object(&self) -> Object {
let node_type = self.node_type.clone();
let mut object = Object::new(&self.edge_name)
.field(Field::new(
"cursor",
TypeRef::named_nn(TypeRef::STRING),
|ctx| {
FieldFuture::new(async move {
let edge = ctx.parent_value.try_downcast_ref::<DynamicEdge>()?;
Ok(Some(Value::from(edge.cursor.clone())))
})
},
))
.field(Field::new("node", node_type, |ctx| {
FieldFuture::new(async move {
let edge = ctx.parent_value.try_downcast_ref::<DynamicEdge>()?;
Ok(Some(FieldValue::value(edge.node.clone())))
})
}));
for (name, ty) in &self.edge_fields {
let field_name = name.clone();
object = object.field(Field::new(name.clone(), ty.clone(), move |ctx| {
let field_name = field_name.clone();
FieldFuture::new(async move {
let edge = ctx.parent_value.try_downcast_ref::<DynamicEdge>()?;
Ok(extra_field_value(&edge.extra_fields, &field_name))
})
}));
}
object
}
pub fn connection_object(&self) -> Object {
let page_info_name = self.page_info_name.clone();
let edge_type_name = self.edge_name.clone();
let edge_type_ref = TypeRef::named_nn_list_nn(&edge_type_name);
let nodes_type_ref = non_null_list(self.node_type.clone());
let mut object = Object::new(&self.connection_name)
.field(Field::new(
"pageInfo",
TypeRef::named_nn(page_info_name),
|ctx| {
FieldFuture::new(async move {
let conn = ctx.parent_value.try_downcast_ref::<DynamicConnection>()?;
Ok(Some(FieldValue::owned_any(conn.computed_page_info())))
})
},
))
.field(Field::new("edges", edge_type_ref, |ctx| {
FieldFuture::new(async move {
let conn = ctx.parent_value.try_downcast_ref::<DynamicConnection>()?;
let edges = conn.edges.iter().cloned().map(FieldValue::owned_any);
Ok(Some(FieldValue::list(edges)))
})
}))
.field(Field::new("nodes", nodes_type_ref, |ctx| {
FieldFuture::new(async move {
let conn = ctx.parent_value.try_downcast_ref::<DynamicConnection>()?;
let nodes = conn
.edges
.iter()
.map(|edge| FieldValue::value(edge.node.clone()));
Ok(Some(FieldValue::list(nodes)))
})
}));
for (name, ty) in &self.connection_fields {
let field_name = name.clone();
object = object.field(Field::new(name.clone(), ty.clone(), move |ctx| {
let field_name = field_name.clone();
FieldFuture::new(async move {
let conn = ctx.parent_value.try_downcast_ref::<DynamicConnection>()?;
Ok(extra_field_value(&conn.extra_fields, &field_name))
})
}));
}
object
}
}
fn extra_field_value<'a>(fields: &[(String, Value)], field_name: &str) -> Option<FieldValue<'a>> {
fields
.iter()
.rev()
.find(|(name, _)| name == field_name)
.map(|(_, value)| FieldValue::value(value.clone()))
}
fn non_null_list(item_type: TypeRef) -> TypeRef {
TypeRef::NonNull(Box::new(TypeRef::List(Box::new(item_type))))
}