async_graphql/types/connection/
edge.rs

1use std::{borrow::Cow, marker::PhantomData};
2
3use crate::{
4    connection::{DefaultEdgeName, EmptyFields},
5    types::connection::{CursorType, EdgeNameType},
6    ComplexObject, ObjectType, OutputType, SimpleObject, TypeName,
7};
8
9/// An edge in a connection.
10#[derive(SimpleObject)]
11#[graphql(internal, name_type, shareable, complex)]
12pub struct Edge<Cursor, Node, EdgeFields, Name = DefaultEdgeName>
13where
14    Cursor: CursorType + Send + Sync,
15    Node: OutputType,
16    EdgeFields: ObjectType,
17    Name: EdgeNameType,
18{
19    #[graphql(skip)]
20    _mark: PhantomData<Name>,
21    /// A cursor for use in pagination
22    #[graphql(skip)]
23    pub cursor: Cursor,
24    /// The item at the end of the edge
25    pub node: Node,
26    #[graphql(flatten)]
27    pub(crate) additional_fields: EdgeFields,
28}
29
30#[ComplexObject(internal)]
31impl<Cursor, Node, EdgeFields, Name> Edge<Cursor, Node, EdgeFields, Name>
32where
33    Cursor: CursorType + Send + Sync,
34    Node: OutputType,
35    EdgeFields: ObjectType,
36    Name: EdgeNameType,
37{
38    /// A cursor for use in pagination
39    async fn cursor(&self) -> String {
40        self.cursor.encode_cursor()
41    }
42}
43
44impl<Cursor, Node, EdgeFields, Name> TypeName for Edge<Cursor, Node, EdgeFields, Name>
45where
46    Cursor: CursorType + Send + Sync,
47    Node: OutputType,
48    EdgeFields: ObjectType,
49    Name: EdgeNameType,
50{
51    #[inline]
52    fn type_name() -> Cow<'static, str> {
53        Name::type_name::<Node>().into()
54    }
55}
56
57impl<Cursor, Node, EdgeFields, Name> Edge<Cursor, Node, EdgeFields, Name>
58where
59    Name: EdgeNameType,
60    Cursor: CursorType + Send + Sync,
61    Node: OutputType,
62    EdgeFields: ObjectType,
63{
64    /// Create a new edge, it can have some additional fields.
65    #[inline]
66    pub fn with_additional_fields(
67        cursor: Cursor,
68        node: Node,
69        additional_fields: EdgeFields,
70    ) -> Self {
71        Self {
72            _mark: PhantomData,
73            cursor,
74            node,
75            additional_fields,
76        }
77    }
78}
79
80impl<Cursor, Node, Name> Edge<Cursor, Node, EmptyFields, Name>
81where
82    Cursor: CursorType + Send + Sync,
83    Node: OutputType,
84    Name: EdgeNameType,
85{
86    /// Create a new edge.
87    #[inline]
88    pub fn new(cursor: Cursor, node: Node) -> Self {
89        Self {
90            _mark: PhantomData,
91            cursor,
92            node,
93            additional_fields: EmptyFields,
94        }
95    }
96}