mod connection_type;
mod cursor;
mod edge;
mod page_info;
use crate::{Result, SimpleObject};
pub use connection_type::Connection;
pub use cursor::CursorType;
pub use edge::Edge;
use futures::Future;
pub use page_info::PageInfo;
use std::fmt::Display;
#[derive(SimpleObject)]
#[graphql(internal)]
pub struct EmptyFields;
pub async fn query<Cursor, Node, ConnectionFields, EdgeFields, F, R>(
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
f: F,
) -> Result<Connection<Cursor, Node, ConnectionFields, EdgeFields>>
where
Cursor: CursorType + Send + Sync,
<Cursor as CursorType>::Error: Display + Send + Sync + 'static,
F: FnOnce(Option<Cursor>, Option<Cursor>, Option<usize>, Option<usize>) -> R,
R: Future<Output = Result<Connection<Cursor, Node, ConnectionFields, EdgeFields>>>,
{
if first.is_some() && last.is_some() {
return Err("The \"first\" and \"last\" parameters cannot exist at the same time".into());
}
let first = match first {
Some(first) if first < 0 => {
return Err("The \"first\" parameter must be a non-negative number".into());
}
Some(first) => Some(first as usize),
None => None,
};
let last = match last {
Some(last) if last < 0 => {
return Err("The \"last\" parameter must be a non-negative number".into());
}
Some(last) => Some(last as usize),
None => None,
};
let before = match before {
Some(before) => Some(Cursor::decode_cursor(&before)?),
None => None,
};
let after = match after {
Some(after) => Some(Cursor::decode_cursor(&after)?),
None => None,
};
f(after, before, first, last).await
}