async-graphql/src/types/connection/connection_type.rs

142 lines
4.0 KiB
Rust
Raw Normal View History

2022-04-19 04:25:11 +00:00
use std::{borrow::Cow, marker::PhantomData};
2020-10-15 06:38:10 +00:00
2020-03-19 09:20:12 +00:00
use crate::{
2022-05-02 08:20:45 +00:00
connection::{
edge::Edge, ConnectionNameType, DefaultConnectionName, DefaultEdgeName, EdgeNameType,
PageInfo,
},
2022-04-19 04:25:11 +00:00
types::connection::{CursorType, EmptyFields},
Object, ObjectType, OutputType, TypeName,
2020-03-19 09:20:12 +00:00
};
2020-03-20 03:56:08 +00:00
/// Connection type
2020-03-19 09:20:12 +00:00
///
/// Connection is the result of a query for `connection::query`.
2022-04-19 04:25:11 +00:00
pub struct Connection<
Cursor,
Node,
ConnectionFields = EmptyFields,
EdgeFields = EmptyFields,
2022-05-02 08:20:45 +00:00
Name = DefaultConnectionName,
EdgeName = DefaultEdgeName,
2022-04-19 04:25:11 +00:00
> where
Cursor: CursorType + Send + Sync,
Node: OutputType,
ConnectionFields: ObjectType,
EdgeFields: ObjectType,
2022-05-02 08:20:45 +00:00
Name: ConnectionNameType,
EdgeName: EdgeNameType,
2022-04-19 04:25:11 +00:00
{
_mark1: PhantomData<Name>,
_mark2: PhantomData<EdgeName>,
/// All edges of the current page.
2022-05-02 08:20:45 +00:00
pub edges: Vec<Edge<Cursor, Node, EdgeFields, EdgeName>>,
2022-04-19 04:25:11 +00:00
/// Additional fields for connection object.
pub additional_fields: ConnectionFields,
/// If `true` means has previous page.
pub has_previous_page: bool,
/// If `false` means has next page.
pub has_next_page: bool,
}
2022-05-02 08:20:45 +00:00
impl<Cursor, Node, EdgeFields, Name, EdgeName>
Connection<Cursor, Node, EmptyFields, EdgeFields, Name, EdgeName>
2022-04-19 04:25:11 +00:00
where
Cursor: CursorType + Send + Sync,
Node: OutputType,
EdgeFields: ObjectType,
2022-05-02 08:20:45 +00:00
Name: ConnectionNameType,
EdgeName: EdgeNameType,
2022-04-19 04:25:11 +00:00
{
/// Create a new connection.
2022-04-19 04:25:11 +00:00
#[inline]
pub fn new(has_previous_page: bool, has_next_page: bool) -> Self {
Connection {
2022-04-19 04:25:11 +00:00
_mark1: PhantomData,
_mark2: PhantomData,
additional_fields: EmptyFields,
has_previous_page,
has_next_page,
edges: Vec::new(),
}
}
2020-03-19 09:20:12 +00:00
}
2022-05-02 08:20:45 +00:00
impl<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName>
Connection<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName>
2022-04-19 04:25:11 +00:00
where
Cursor: CursorType + Send + Sync,
Node: OutputType,
ConnectionFields: ObjectType,
EdgeFields: ObjectType,
2022-05-02 08:20:45 +00:00
Name: ConnectionNameType,
EdgeName: EdgeNameType,
2022-04-19 04:25:11 +00:00
{
/// Create a new connection, it can have some additional fields.
2022-04-19 04:25:11 +00:00
#[inline]
pub fn with_additional_fields(
2020-03-19 09:20:12 +00:00
has_previous_page: bool,
has_next_page: bool,
2022-04-19 04:25:11 +00:00
additional_fields: ConnectionFields,
2020-03-19 09:20:12 +00:00
) -> Self {
Connection {
2022-04-19 04:25:11 +00:00
_mark1: PhantomData,
_mark2: PhantomData,
additional_fields,
has_previous_page,
has_next_page,
edges: Vec::new(),
2020-03-19 09:20:12 +00:00
}
}
}
2020-03-19 09:20:12 +00:00
2022-04-19 04:25:11 +00:00
#[Object(internal, name_type)]
2022-05-02 08:20:45 +00:00
impl<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName>
Connection<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName>
2022-04-19 04:25:11 +00:00
where
Cursor: CursorType + Send + Sync,
Node: OutputType,
ConnectionFields: ObjectType,
EdgeFields: ObjectType,
2022-05-02 08:20:45 +00:00
Name: ConnectionNameType,
EdgeName: EdgeNameType,
2022-04-19 04:25:11 +00:00
{
/// Information to aid in pagination.
async fn page_info(&self) -> PageInfo {
PageInfo {
has_previous_page: self.has_previous_page,
has_next_page: self.has_next_page,
2022-04-19 04:25:11 +00:00
start_cursor: self.edges.first().map(|edge| edge.cursor.0.encode_cursor()),
end_cursor: self.edges.last().map(|edge| edge.cursor.0.encode_cursor()),
}
}
2022-04-19 04:25:11 +00:00
/// A list of edges.
#[inline]
2022-05-02 08:20:45 +00:00
async fn edges(&self) -> &[Edge<Cursor, Node, EdgeFields, EdgeName>] {
2022-04-19 04:25:11 +00:00
&self.edges
2020-04-26 04:34:08 +00:00
}
2020-03-19 09:20:12 +00:00
2022-04-19 04:25:11 +00:00
#[graphql(flatten)]
#[inline]
async fn additional_fields(&self) -> &ConnectionFields {
&self.additional_fields
}
}
2022-05-02 08:20:45 +00:00
impl<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName> TypeName
for Connection<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName>
where
2022-04-19 04:25:11 +00:00
Cursor: CursorType + Send + Sync,
Node: OutputType,
ConnectionFields: ObjectType,
EdgeFields: ObjectType,
2022-05-02 08:20:45 +00:00
Name: ConnectionNameType,
EdgeName: EdgeNameType,
{
2022-04-19 04:25:11 +00:00
#[inline]
2020-03-19 09:20:12 +00:00
fn type_name() -> Cow<'static, str> {
2022-04-19 04:25:11 +00:00
Name::type_name::<Node>().into()
2020-03-19 09:20:12 +00:00
}
}