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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::types::connection::cursor::Cursor;
use crate::types::connection::edge::Edge;
use crate::types::connection::page_info::PageInfo;
use crate::{
    do_resolve, registry, Context, ContextSelectionSet, EmptyEdgeFields, Error, ObjectType,
    OutputValueType, Pos, QueryError, Result, Type,
};
use inflector::Inflector;
use itertools::Itertools;
use std::borrow::Cow;
use std::collections::HashMap;

/// Connection type
///
/// Connection is the result of a query for `DataSource`,
/// If the `T` type is `OutputValueType`, you can return the value as a field function directly,
/// otherwise you can use the `Connection::map` function to convert to a type that implements `OutputValueType`.
/// `E` is an extension object type that extends the edge fields.
pub struct Connection<T, E: ObjectType + Sync + Send = EmptyEdgeFields> {
    /// The total number of records.
    pub total_count: Option<usize>,

    /// Information about pagination in a connection.
    pub page_info: PageInfo,

    /// All records of the current page.
    pub nodes: Vec<(Cursor, E, T)>,
}

impl<T, E: ObjectType + Sync + Send> Connection<T, E> {
    /// Create a connection object.
    pub fn new(
        total_count: Option<usize>,
        has_previous_page: bool,
        has_next_page: bool,
        nodes: Vec<(Cursor, E, T)>,
    ) -> Self {
        Connection {
            total_count,
            page_info: PageInfo {
                has_previous_page,
                has_next_page,
                start_cursor: nodes.first().map(|(cursor, _, _)| cursor.clone()),
                end_cursor: nodes.last().map(|(cursor, _, _)| cursor.clone()),
            },
            nodes,
        }
    }

    /// Convert node type.
    pub fn map<O, F>(self, mut f: F) -> Connection<O, E>
    where
        F: FnMut(T) -> O,
    {
        Connection {
            total_count: self.total_count,
            page_info: self.page_info,
            nodes: self
                .nodes
                .into_iter()
                .map(|(cursor, edge_type, node)| (cursor, edge_type, f(node)))
                .collect(),
        }
    }

    #[doc(hidden)]
    #[inline]
    pub async fn page_info(&self) -> &PageInfo {
        &self.page_info
    }

    #[doc(hidden)]
    #[inline]
    pub async fn edges(&self) -> Option<Vec<Option<Edge<'_, T, E>>>> {
        Some(
            self.nodes
                .iter()
                .map(|(cursor, extra_type, node)| {
                    Some(Edge {
                        cursor,
                        extra_type,
                        node,
                    })
                })
                .collect_vec(),
        )
    }

    #[doc(hidden)]
    #[inline]
    pub async fn total_count(&self) -> Option<i32> {
        self.total_count.map(|n| n as i32)
    }
}

impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> Type for Connection<T, E> {
    fn type_name() -> Cow<'static, str> {
        Cow::Owned(format!("{}Connection", T::type_name()))
    }

    fn create_type_info(registry: &mut registry::Registry) -> String {
        registry.create_type::<Self, _>(|registry| registry::Type::Object {
            name: Self::type_name().to_string(),
            description: None,
            fields: {
                let mut fields = HashMap::new();

                fields.insert(
                    "pageInfo".to_string(),
                    registry::Field {
                        name: "pageInfo".to_string(),
                        description: Some("Information to aid in pagination."),
                        args: Default::default(),
                        ty: PageInfo::create_type_info(registry),
                        deprecation: None,
                        cache_control: Default::default(),
                        external: false,
                        requires: None,
                        provides: None
                    },
                );

                fields.insert(
                    "edges".to_string(),
                    registry::Field {
                        name: "edges".to_string(),
                        description: Some("A list of edges."),
                        args: Default::default(),
                        ty: <Option::<Vec<Option<Edge<T,E>>>> as Type>::create_type_info(registry),
                        deprecation: None,
                        cache_control: Default::default(),
                        external: false,
                        requires: None,
                        provides: None
                    },
                );

                fields.insert(
                    "totalCount".to_string(),
                    registry::Field {
                        name: "totalCount".to_string(),
                        description: Some(r#"A count of the total number of objects in this connection, ignoring pagination. This allows a client to fetch the first five objects by passing "5" as the argument to "first", then fetch the total count so it could display "5 of 83", for example."#),
                        args: Default::default(),
                        ty: Option::<i32>::create_type_info(registry),
                        deprecation: None,
                        cache_control: Default::default(),
                        external: false,
                        requires: None,
                        provides: None
                    },
                );

                let elements_name = T::type_name().to_plural().to_camel_case();
                fields.insert(elements_name.clone(),registry::Field{
                    name: elements_name,
                    description: Some(r#"A list of all of the objects returned in the connection. This is a convenience field provided for quickly exploring the API; rather than querying for "{ edges { node } }" when no edge data is needed, this field can be be used instead. Note that when clients like Relay need to fetch the "cursor" field on the edge to enable efficient pagination, this shortcut cannot be used, and the full "{ edges { node } }" version should be used instead."#),
                    args: Default::default(),
                    ty: Vec::<T>::type_name().to_string(),
                    deprecation: None,
                    cache_control: Default::default(),
                    external: false,
                    requires: None,
                    provides: None
                });

                fields
            },
            cache_control: Default::default(),
            extends: false,
            keys: None
        })
    }
}

#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> ObjectType
    for Connection<T, E>
{
    async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
        if ctx.name.as_str() == "pageInfo" {
            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            return OutputValueType::resolve(self.page_info().await, &ctx_obj, ctx.position())
                .await;
        } else if ctx.name.as_str() == "edges" {
            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            return OutputValueType::resolve(&self.edges().await, &ctx_obj, ctx.position()).await;
        } else if ctx.name.as_str() == "totalCount" {
            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            return OutputValueType::resolve(&self.total_count().await, &ctx_obj, ctx.position())
                .await;
        } else if ctx.name.as_str() == T::type_name().to_plural().to_camel_case() {
            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            let items = self.nodes.iter().map(|(_, _, item)| item).collect_vec();
            return OutputValueType::resolve(&items, &ctx_obj, ctx.position()).await;
        }

        Err(Error::Query {
            pos: ctx.position(),
            path: None,
            err: QueryError::FieldNotFound {
                field_name: ctx.name.clone_inner(),
                object: Connection::<T, E>::type_name().to_string(),
            },
        })
    }
}

#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> OutputValueType
    for Connection<T, E>
{
    async fn resolve(&self, ctx: &ContextSelectionSet<'_>, _pos: Pos) -> Result<serde_json::Value> {
        do_resolve(ctx, self).await
    }
}