async-graphql/src/base.rs

118 lines
3.2 KiB
Rust
Raw Normal View History

2020-09-06 06:16:36 +00:00
use crate::parser::types::Field;
2020-03-19 09:20:12 +00:00
use crate::registry::Registry;
use crate::{
2020-09-29 23:45:48 +00:00
registry, ContainerType, ContextSelectionSet, FieldResult, InputValueResult, Positioned,
Result, 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())
}
/// Introspection type name
///
2020-04-03 01:31:58 +00:00
/// Is the return value of field `__typename`, the interface and union should return the current type, and the others return `Type::type_name`.
fn introspection_type_name(&self) -> Cow<'static, str> {
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-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-09-06 05:38:31 +00:00
/// Parse from `Value`. None represents undefined.
fn parse(value: Option<Value>) -> InputValueResult<Self>;
2020-09-06 05:38:31 +00:00
/// Convert to a `Value` for introspection.
fn to_value(&self) -> Value;
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-05-20 00:18:28 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> Result<serde_json::Value>;
2020-03-03 03:48:00 +00:00
}
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-05-20 00:18:28 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> Result<serde_json::Value> {
T::resolve(*self, ctx, field).await
2020-03-01 10:54:34 +00:00
}
}
2020-05-03 14:32:37 +00:00
impl<T: Type> Type for FieldResult<T> {
fn type_name() -> Cow<'static, str> {
T::type_name()
}
fn qualified_type_name() -> String {
T::qualified_type_name()
}
fn create_type_info(registry: &mut registry::Registry) -> String {
2020-05-03 15:00:20 +00:00
T::create_type_info(registry)
2020-05-03 14:32:37 +00:00
}
}
#[async_trait::async_trait]
impl<T: OutputValueType + Sync> OutputValueType for FieldResult<T> {
async fn resolve(
&self,
2020-05-03 14:32:37 +00:00
ctx: &ContextSelectionSet<'_>,
2020-05-20 00:18:28 +00:00
field: &Positioned<Field>,
2020-05-06 02:02:25 +00:00
) -> crate::Result<serde_json::Value> {
match self {
2020-05-20 00:18:28 +00:00
Ok(value) => Ok(OutputValueType::resolve(value, ctx, field).await?),
2020-07-15 10:05:24 +00:00
Err(err) => Err(err
.clone()
2020-09-06 05:38:31 +00:00
.into_error_with_path(field.pos, ctx.path_node.as_ref())),
2020-05-03 14:32:37 +00:00
}
}
}
2020-09-29 23:45:48 +00:00
/// A GraphQL object.
pub trait ObjectType: ContainerType {}
#[async_trait::async_trait]
impl<T: ObjectType + Send + Sync> ObjectType for &T {}
/// A GraphQL interface.
pub trait InterfaceType: ContainerType {}
/// A GraphQL interface.
pub trait UnionType: ContainerType {}
/// A GraphQL input object.
pub trait InputObjectType: InputValueType {}