2020-03-19 09:20:12 +00:00
|
|
|
use crate::{
|
2020-04-02 02:21:04 +00:00
|
|
|
do_resolve, registry, Context, ContextSelectionSet, ObjectType, OutputValueType, Result, Type,
|
2020-03-19 09:20:12 +00:00
|
|
|
};
|
2020-04-02 02:21:04 +00:00
|
|
|
use graphql_parser::Pos;
|
2020-03-19 09:20:12 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
pub struct Edge<'a, T, E> {
|
|
|
|
pub cursor: &'a str,
|
|
|
|
pub node: &'a T,
|
|
|
|
pub extra_type: &'a E,
|
|
|
|
}
|
|
|
|
|
2020-04-26 04:34:08 +00:00
|
|
|
impl<'a, T, E> Edge<'a, T, E>
|
|
|
|
where
|
|
|
|
T: OutputValueType + Send + Sync + 'a,
|
|
|
|
E: ObjectType + Sync + Send + 'a,
|
|
|
|
{
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
pub async fn node(&self) -> &T {
|
|
|
|
self.node
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
pub async fn cursor(&self) -> &str {
|
|
|
|
self.cursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl<'a, T, E> Type for Edge<'a, T, E>
|
|
|
|
where
|
|
|
|
T: OutputValueType + Send + Sync + 'a,
|
|
|
|
E: ObjectType + Sync + Send + 'a,
|
|
|
|
{
|
|
|
|
fn type_name() -> Cow<'static, str> {
|
|
|
|
Cow::Owned(format!("{}Edge", T::type_name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
|
|
|
registry.create_type::<Self, _>(|registry| {
|
|
|
|
E::create_type_info(registry);
|
|
|
|
let extra_fields = if let Some(registry::Type::Object { fields, .. }) =
|
2020-05-05 05:46:56 +00:00
|
|
|
registry.types.remove(E::type_name().as_ref())
|
2020-03-19 09:20:12 +00:00
|
|
|
{
|
2020-05-05 05:46:56 +00:00
|
|
|
fields
|
2020-03-19 09:20:12 +00:00
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
registry::Type::Object {
|
|
|
|
name: Self::type_name().to_string(),
|
|
|
|
description: Some("An edge in a connection."),
|
|
|
|
fields: {
|
|
|
|
let mut fields = HashMap::new();
|
|
|
|
|
|
|
|
fields.insert(
|
|
|
|
"node".to_string(),
|
|
|
|
registry::Field {
|
|
|
|
name: "node".to_string(),
|
|
|
|
description: Some("The item at the end of the edge"),
|
|
|
|
args: Default::default(),
|
|
|
|
ty: T::create_type_info(registry),
|
|
|
|
deprecation: None,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
external: false,
|
|
|
|
requires: None,
|
|
|
|
provides: None,
|
2020-03-19 09:20:12 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
fields.insert(
|
|
|
|
"cursor".to_string(),
|
|
|
|
registry::Field {
|
|
|
|
name: "cursor".to_string(),
|
|
|
|
description: Some("A cursor for use in pagination"),
|
|
|
|
args: Default::default(),
|
|
|
|
ty: String::create_type_info(registry),
|
|
|
|
deprecation: None,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
external: false,
|
|
|
|
requires: None,
|
|
|
|
provides: None,
|
2020-03-19 09:20:12 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
fields.extend(extra_fields);
|
|
|
|
fields
|
|
|
|
},
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: false,
|
|
|
|
keys: None,
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<'a, T, E> ObjectType for Edge<'a, T, E>
|
|
|
|
where
|
|
|
|
T: OutputValueType + Send + Sync + 'a,
|
|
|
|
E: ObjectType + Sync + Send + 'a,
|
|
|
|
{
|
2020-04-26 07:12:18 +00:00
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
|
|
|
|
if ctx.name.as_str() == "node" {
|
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
|
|
|
return OutputValueType::resolve(self.node().await, &ctx_obj, ctx.position).await;
|
|
|
|
} else if ctx.name.as_str() == "cursor" {
|
2020-04-26 04:34:08 +00:00
|
|
|
return Ok(self.cursor().await.into());
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 07:12:18 +00:00
|
|
|
self.extra_type.resolve_field(ctx).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<'a, T, E> OutputValueType for Edge<'a, T, E>
|
|
|
|
where
|
|
|
|
T: OutputValueType + Send + Sync + 'a,
|
|
|
|
E: ObjectType + Sync + Send + 'a,
|
|
|
|
{
|
2020-05-05 13:06:48 +00:00
|
|
|
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, _pos: Pos) -> Result<serde_json::Value> {
|
|
|
|
do_resolve(ctx, self).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|