1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::{borrow::Cow, marker::PhantomData};

use crate::{
    connection::{DefaultEdgeName, EmptyFields},
    types::connection::{CursorType, EdgeNameType},
    InputValueError, InputValueResult, ObjectType, OutputType, Scalar, ScalarType, SimpleObject,
    TypeName, Value,
};

pub(crate) struct CursorScalar<T: CursorType>(pub(crate) T);

#[Scalar(internal, name = "String")]
impl<T: CursorType + Send + Sync> ScalarType for CursorScalar<T> {
    fn parse(value: Value) -> InputValueResult<Self> {
        match value {
            Value::String(s) => T::decode_cursor(&s)
                .map(Self)
                .map_err(InputValueError::custom),
            _ => Err(InputValueError::expected_type(value)),
        }
    }

    fn is_valid(value: &Value) -> bool {
        matches!(value, Value::String(_))
    }

    fn to_value(&self) -> Value {
        Value::String(self.0.encode_cursor())
    }
}

/// An edge in a connection.
#[derive(SimpleObject)]
#[graphql(internal, name_type)]
pub struct Edge<Cursor, Node, EdgeFields, Name = DefaultEdgeName>
where
    Cursor: CursorType + Send + Sync,
    Node: OutputType,
    EdgeFields: ObjectType,
    Name: EdgeNameType,
{
    #[graphql(skip)]
    _mark: PhantomData<Name>,
    /// A cursor for use in pagination
    pub(crate) cursor: CursorScalar<Cursor>,
    /// The item at the end of the edge
    pub node: Node,
    #[graphql(flatten)]
    pub(crate) additional_fields: EdgeFields,
}

impl<Cursor, Node, EdgeFields, Name> TypeName for Edge<Cursor, Node, EdgeFields, Name>
where
    Cursor: CursorType + Send + Sync,
    Node: OutputType,
    EdgeFields: ObjectType,
    Name: EdgeNameType,
{
    #[inline]
    fn type_name() -> Cow<'static, str> {
        Name::type_name::<Node>().into()
    }
}

impl<Cursor, Node, EdgeFields, Name> Edge<Cursor, Node, EdgeFields, Name>
where
    Name: EdgeNameType,
    Cursor: CursorType + Send + Sync,
    Node: OutputType,
    EdgeFields: ObjectType,
{
    /// Create a new edge, it can have some additional fields.
    #[inline]
    pub fn with_additional_fields(
        cursor: Cursor,
        node: Node,
        additional_fields: EdgeFields,
    ) -> Self {
        Self {
            _mark: PhantomData,
            cursor: CursorScalar(cursor),
            node,
            additional_fields,
        }
    }
}

impl<Cursor, Node, Name> Edge<Cursor, Node, EmptyFields, Name>
where
    Cursor: CursorType + Send + Sync,
    Node: OutputType,
    Name: EdgeNameType,
{
    /// Create a new edge.
    #[inline]
    pub fn new(cursor: Cursor, node: Node) -> Self {
        Self {
            _mark: PhantomData,
            cursor: CursorScalar(cursor),
            node,
            additional_fields: EmptyFields,
        }
    }
}