2020-05-26 10:34:43 +00:00
|
|
|
use crate::{InputValueError, InputValueResult, ScalarType, Value, ID};
|
2020-05-05 05:21:45 +00:00
|
|
|
use async_graphql_derive::Scalar;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
/// Cursor scalar
|
|
|
|
///
|
|
|
|
/// A custom scalar that serializes as a string.
|
|
|
|
/// https://relay.dev/graphql/connections.htm#sec-Cursor
|
|
|
|
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
|
|
|
|
pub struct Cursor(String);
|
|
|
|
|
|
|
|
impl Deref for Cursor {
|
|
|
|
type Target = String;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Cursor {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:41:04 +00:00
|
|
|
impl<T> From<T> for Cursor
|
2020-05-10 10:38:20 +00:00
|
|
|
where
|
2020-05-10 13:49:29 +00:00
|
|
|
T: std::fmt::Display,
|
2020-05-10 10:38:20 +00:00
|
|
|
{
|
2020-05-10 12:41:04 +00:00
|
|
|
fn from(value: T) -> Self {
|
|
|
|
Cursor(value.to_string())
|
2020-05-10 10:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 14:25:16 +00:00
|
|
|
impl From<ID> for Cursor {
|
|
|
|
fn from(id: ID) -> Self {
|
|
|
|
Cursor(id.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 05:21:45 +00:00
|
|
|
#[Scalar(internal)]
|
|
|
|
impl ScalarType for Cursor {
|
2020-05-11 13:47:24 +00:00
|
|
|
fn parse(value: Value) -> InputValueResult<Self> {
|
2020-05-05 05:21:45 +00:00
|
|
|
match value {
|
2020-05-16 13:14:26 +00:00
|
|
|
Value::String(s) => Ok(Cursor(s)),
|
2020-05-11 13:47:24 +00:00
|
|
|
_ => Err(InputValueError::ExpectedType(value)),
|
2020-05-10 10:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid(value: &Value) -> bool {
|
|
|
|
match value {
|
|
|
|
Value::String(_) => true,
|
|
|
|
_ => false,
|
2020-05-05 05:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:43 +00:00
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
Value::String(self.0.to_string())
|
2020-05-05 05:21:45 +00:00
|
|
|
}
|
|
|
|
}
|