Add ToGraphQLCursor and ToGraphQLID traits. #54

This commit is contained in:
sunli 2020-05-10 18:38:20 +08:00
parent 728989209f
commit ff6f97d7df
6 changed files with 37 additions and 5 deletions

View File

@ -122,7 +122,7 @@ pub use error::{
pub use parser::{Pos, Positioned, Value};
pub use query::{IntoQueryBuilder, IntoQueryBuilderOpts, QueryBuilder, QueryResponse};
pub use registry::CacheControl;
pub use scalars::{Any, Json, ID};
pub use scalars::{Any, Json, ToGraphQLID, ID};
pub use schema::Schema;
pub use subscription::{
SimpleBroker, SubscriptionStream, SubscriptionStreams, SubscriptionTransport,
@ -130,7 +130,7 @@ pub use subscription::{
};
pub use types::{
Connection, Cursor, DataSource, EmptyEdgeFields, EmptyMutation, EmptySubscription, PageInfo,
QueryOperation, Upload,
QueryOperation, ToGraphQLCursor, Upload,
};
pub use validation::ValidationMode;

View File

@ -98,6 +98,21 @@ impl PartialEq<&str> for ID {
}
}
/// Convert any type that implements Display to the ID type
pub trait ToGraphQLID {
#[allow(missing_docs)]
fn to_graphql_id(&self) -> ID;
}
impl<T> ToGraphQLID for T
where
T: std::fmt::Display,
{
fn to_graphql_id(&self) -> ID {
ID(self.to_string())
}
}
#[Scalar(internal)]
impl ScalarType for ID {
fn type_name() -> &'static str {

View File

@ -15,7 +15,7 @@ mod bson;
mod uuid;
pub use any::Any;
pub use id::ID;
pub use id::{ToGraphQLID, ID};
pub use json::Json;
#[cfg(test)]

View File

@ -47,6 +47,21 @@ impl From<usize> for Cursor {
}
}
/// Convert any type that implements Display to the Cursor type
pub trait ToGraphQLCursor {
#[allow(missing_docs)]
fn to_graphql_cursor(&self) -> Cursor;
}
impl<T> ToGraphQLCursor for T
where
T: std::fmt::Display,
{
fn to_graphql_cursor(&self) -> Cursor {
Cursor(self.to_string())
}
}
#[Scalar(internal)]
impl ScalarType for Cursor {
fn type_name() -> &'static str {

View File

@ -7,7 +7,7 @@ mod slice;
use crate::{Context, FieldResult, ObjectType};
pub use connection_type::Connection;
pub use cursor::Cursor;
pub use cursor::{Cursor, ToGraphQLCursor};
pub use page_info::PageInfo;
/// Connection query operation

View File

@ -7,7 +7,9 @@ mod optional;
mod query_root;
mod upload;
pub use connection::{Connection, Cursor, DataSource, EmptyEdgeFields, PageInfo, QueryOperation};
pub use connection::{
Connection, Cursor, DataSource, EmptyEdgeFields, PageInfo, QueryOperation, ToGraphQLCursor,
};
pub use empty_mutation::EmptyMutation;
pub use empty_subscription::EmptySubscription;
pub use query_root::QueryRoot;