2020-03-19 09:20:12 +00:00
|
|
|
use crate::registry::Registry;
|
2020-03-20 03:56:08 +00:00
|
|
|
use crate::{registry, Context, ContextSelectionSet, QueryError, Result, ID};
|
2020-03-06 15:58:43 +00:00
|
|
|
use graphql_parser::query::{Field, Value};
|
2020-03-02 00:24:49 +00:00
|
|
|
use std::borrow::Cow;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Represents a GraphQL type
|
2020-03-20 03:56:08 +00:00
|
|
|
///
|
|
|
|
/// All GraphQL types implement this trait, such as `Scalar`, `Object`, `Union` ...
|
2020-03-19 09:20:12 +00:00
|
|
|
pub trait Type {
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Type the name.
|
2020-03-03 03:48:00 +00:00
|
|
|
fn type_name() -> Cow<'static, str>;
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Qualified typename.
|
2020-03-03 11:15:18 +00:00
|
|
|
fn qualified_type_name() -> String {
|
|
|
|
format!("{}!", Self::type_name())
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Create type information in the registry and return qualified typename.
|
2020-03-03 11:15:18 +00:00
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String;
|
2020-03-20 03:56:08 +00:00
|
|
|
|
|
|
|
/// Returns a `GlobalID` that is unique among all types.
|
|
|
|
fn global_id(id: ID) -> ID {
|
|
|
|
base64::encode(format!("{}:{}", Self::type_name(), id)).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse `GlobalID`.
|
|
|
|
fn from_global_id(id: ID) -> Result<ID> {
|
2020-03-21 01:32:13 +00:00
|
|
|
let v: Vec<&str> = id.splitn(2, ':').collect();
|
2020-03-20 03:56:08 +00:00
|
|
|
if v.len() != 2 {
|
|
|
|
return Err(QueryError::InvalidGlobalID.into());
|
|
|
|
}
|
|
|
|
if v[0] != Self::type_name() {
|
|
|
|
return Err(QueryError::InvalidGlobalIDType {
|
|
|
|
expect: Self::type_name().to_string(),
|
|
|
|
actual: v[0].to_string(),
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
Ok(v[1].to_string().into())
|
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Represents a GraphQL input value
|
2020-03-19 09:20:12 +00:00
|
|
|
pub trait InputValueType: Type + Sized {
|
2020-03-20 03:56:08 +00:00
|
|
|
/// Parse from `Value`
|
2020-03-04 02:38:07 +00:00
|
|
|
fn parse(value: &Value) -> Option<Self>;
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Represents a GraphQL output value
|
2020-03-03 03:48:00 +00:00
|
|
|
#[async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
pub trait OutputValueType: Type {
|
2020-03-20 03:56:08 +00:00
|
|
|
/// Resolve an output value to `serde_json::Value`.
|
2020-03-06 15:58:43 +00:00
|
|
|
async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value>;
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Represents a GraphQL object
|
2020-03-06 15:58:43 +00:00
|
|
|
#[async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
pub trait ObjectType: OutputValueType {
|
|
|
|
/// This function returns true of type `EmptyMutation` only
|
2020-03-09 10:05:52 +00:00
|
|
|
#[doc(hidden)]
|
2020-03-05 09:06:14 +00:00
|
|
|
fn is_empty() -> bool {
|
2020-03-19 09:20:12 +00:00
|
|
|
false
|
2020-03-05 09:06:14 +00:00
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Resolves a field value and outputs it as a json value `serde_json::Value`.
|
2020-03-06 15:58:43 +00:00
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value>;
|
2020-03-07 02:39:55 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Resolve an inline fragment with the `name`.
|
2020-03-07 02:39:55 +00:00
|
|
|
async fn resolve_inline_fragment(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
ctx: &ContextSelectionSet<'_>,
|
|
|
|
result: &mut serde_json::Map<String, serde_json::Value>,
|
|
|
|
) -> Result<()>;
|
2020-03-05 09:06:14 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Represents a GraphQL input object
|
2020-03-19 09:20:12 +00:00
|
|
|
pub trait InputObjectType: InputValueType {}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Represents a GraphQL scalar
|
|
|
|
///
|
|
|
|
/// You can implement the trait to create a custom scalar.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use async_graphql::*;
|
|
|
|
///
|
|
|
|
/// struct MyInt(i32);
|
|
|
|
///
|
2020-03-19 09:20:12 +00:00
|
|
|
/// impl Scalar for MyInt {
|
2020-03-09 10:05:52 +00:00
|
|
|
/// fn type_name() -> &'static str {
|
|
|
|
/// "MyInt"
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn parse(value: &Value) -> Option<Self> {
|
|
|
|
/// if let Value::Int(n) = value {
|
|
|
|
/// Some(MyInt(n.as_i64().unwrap() as i32))
|
|
|
|
/// } else {
|
|
|
|
/// None
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn to_json(&self) -> Result<serde_json::Value> {
|
|
|
|
/// Ok(self.0.into())
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl_scalar!(MyInt); // // Don't forget this one
|
|
|
|
/// ```
|
2020-03-19 09:20:12 +00:00
|
|
|
pub trait Scalar: Sized + Send {
|
2020-03-09 10:05:52 +00:00
|
|
|
/// The type name of a scalar.
|
2020-03-01 10:54:34 +00:00
|
|
|
fn type_name() -> &'static str;
|
2020-03-08 12:35:36 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// The description of a scalar.
|
2020-03-03 11:15:18 +00:00
|
|
|
fn description() -> Option<&'static str> {
|
|
|
|
None
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Parse a scalar value, return `Some(Self)` if successful, otherwise return `None`.
|
2020-03-04 02:38:07 +00:00
|
|
|
fn parse(value: &Value) -> Option<Self>;
|
2020-03-08 12:35:36 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Checks for a valid scalar value.
|
|
|
|
///
|
|
|
|
/// The default implementation is to try to parse it, and in some cases you can implement this on your own to improve performance.
|
2020-03-08 12:35:36 +00:00
|
|
|
fn is_valid(value: &Value) -> bool {
|
|
|
|
Self::parse(value).is_some()
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Convert the scalar value to json value.
|
2020-03-02 00:24:49 +00:00
|
|
|
fn to_json(&self) -> Result<serde_json::Value>;
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
#[macro_export]
|
2020-03-09 10:05:52 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! impl_scalar_internal {
|
2020-03-06 15:58:43 +00:00
|
|
|
($ty:ty) => {
|
2020-03-19 09:20:12 +00:00
|
|
|
impl crate::Type for $ty {
|
2020-03-06 15:58:43 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
2020-03-19 09:20:12 +00:00
|
|
|
std::borrow::Cow::Borrowed(<$ty as crate::Scalar>::type_name())
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
fn create_type_info(registry: &mut crate::registry::Registry) -> String {
|
|
|
|
registry.create_type::<$ty, _>(|_| crate::registry::Type::Scalar {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: <$ty as crate::Scalar>::type_name().to_string(),
|
2020-03-06 15:58:43 +00:00
|
|
|
description: <$ty>::description(),
|
2020-03-19 09:20:12 +00:00
|
|
|
is_valid: |value| <$ty as crate::Scalar>::is_valid(value),
|
2020-03-06 15:58:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl crate::InputValueType for $ty {
|
2020-03-06 15:58:43 +00:00
|
|
|
fn parse(value: &crate::Value) -> Option<Self> {
|
2020-03-19 09:20:12 +00:00
|
|
|
<$ty as crate::Scalar>::parse(value)
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 10:03:19 +00:00
|
|
|
|
2020-03-21 01:32:13 +00:00
|
|
|
#[allow(clippy::ptr_arg)]
|
2020-03-07 10:03:19 +00:00
|
|
|
#[async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl crate::OutputValueType for $ty {
|
2020-03-07 10:03:19 +00:00
|
|
|
async fn resolve(
|
|
|
|
value: &Self,
|
|
|
|
_: &crate::ContextSelectionSet<'_>,
|
|
|
|
) -> crate::Result<serde_json::Value> {
|
|
|
|
value.to_json()
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 03:56:08 +00:00
|
|
|
/// After implementing the `Scalar` trait, you must call this macro to implement some additional traits.
|
2020-03-09 10:05:52 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! impl_scalar {
|
|
|
|
($ty:ty) => {
|
2020-03-19 09:20:12 +00:00
|
|
|
impl async_graphql::Type for $ty {
|
2020-03-09 10:05:52 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
2020-03-19 09:20:12 +00:00
|
|
|
std::borrow::Cow::Borrowed(<$ty as async_graphql::Scalar>::type_name())
|
2020-03-09 10:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut async_graphql::registry::Registry) -> String {
|
|
|
|
registry.create_type::<$ty, _>(|_| async_graphql::registry::Type::Scalar {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: <$ty as async_graphql::Scalar>::type_name().to_string(),
|
2020-03-09 10:05:52 +00:00
|
|
|
description: <$ty>::description(),
|
2020-03-19 09:20:12 +00:00
|
|
|
is_valid: |value| <$ty as async_graphql::Scalar>::is_valid(value),
|
2020-03-09 10:05:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl async_graphql::InputValueType for $ty {
|
2020-03-09 10:05:52 +00:00
|
|
|
fn parse(value: &async_graphql::Value) -> Option<Self> {
|
2020-03-19 09:20:12 +00:00
|
|
|
<$ty as async_graphql::Scalar>::parse(value)
|
2020-03-09 10:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 01:32:13 +00:00
|
|
|
#[allow(clippy::ptr_arg)]
|
2020-03-09 10:05:52 +00:00
|
|
|
#[async_graphql::async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl async_graphql::OutputValueType for $ty {
|
2020-03-09 10:05:52 +00:00
|
|
|
async fn resolve(
|
|
|
|
value: &Self,
|
|
|
|
_: &async_graphql::ContextSelectionSet<'_>,
|
|
|
|
) -> async_graphql::Result<serde_json::Value> {
|
|
|
|
value.to_json()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl<T: Type + Send + Sync> Type for &T {
|
|
|
|
fn type_name() -> Cow<'static, str> {
|
|
|
|
T::type_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut Registry) -> String {
|
|
|
|
T::create_type_info(registry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
#[async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl<T: OutputValueType + Send + Sync> OutputValueType for &T {
|
2020-03-21 01:32:13 +00:00
|
|
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
2020-03-06 15:58:43 +00:00
|
|
|
async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
|
2020-03-19 09:20:12 +00:00
|
|
|
T::resolve(*value, ctx).await
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|