async-graphql/src/base.rs

210 lines
5.4 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
2021-01-10 03:21:47 +00:00
use std::sync::Arc;
use async_graphql_value::ConstValue;
2020-09-06 06:16:36 +00:00
use crate::parser::types::Field;
2021-03-17 12:51:30 +00:00
use crate::registry::{self, Registry};
use crate::{
ContainerType, Context, ContextSelectionSet, Error, InputValueError, InputValueResult,
Positioned, Result, ServerResult, Value,
};
2020-03-01 10:54:34 +00:00
#[doc(hidden)]
pub trait Description {
fn description() -> &'static str;
}
2020-10-13 02:19:30 +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-10-13 02:19:30 +00:00
/// Represents a GraphQL input value.
pub trait InputType: Type + Send + Sync + 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;
/// Get the federation fields, only for InputObject.
#[doc(hidden)]
fn federation_fields() -> Option<String> {
None
}
2020-03-03 03:48:00 +00:00
}
2020-10-13 02:19:30 +00:00
/// Represents a GraphQL output value.
2020-03-03 03:48:00 +00:00
#[async_trait::async_trait]
pub trait OutputType: Type + Send + Sync {
2020-10-10 02:32:43 +00:00
/// Resolve an output value to `async_graphql::Value`.
2021-06-07 12:51:20 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value>;
2020-03-03 03:48:00 +00:00
}
impl<T: Type + ?Sized> Type for &T {
2020-03-19 09:20:12 +00:00
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]
impl<T: OutputType + ?Sized> OutputType for &T {
2020-03-21 01:32:13 +00:00
#[allow(clippy::trivially_copy_pass_by_ref)]
2021-06-07 12:51:20 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value> {
2020-05-20 00:18:28 +00:00
T::resolve(*self, ctx, field).await
2020-03-01 10:54:34 +00:00
}
}
impl<T: Type, E: Into<Error> + Send + Sync + Clone> Type for Result<T, E> {
2021-01-10 03:21:47 +00:00
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 {
2021-01-10 03:21:47 +00:00
T::create_type_info(registry)
}
}
#[async_trait::async_trait]
impl<T: OutputType + Sync, E: Into<Error> + Send + Sync + Clone> OutputType for Result<T, E> {
2021-06-07 12:51:20 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value> {
match self {
Ok(value) => value.resolve(ctx, field).await,
Err(err) => {
2021-06-07 12:51:20 +00:00
return Err(ctx.set_error_path(err.clone().into().into_server_error(field.pos)))
}
}
2021-01-10 03:21:47 +00:00
}
}
/// A GraphQL object.
pub trait ObjectType: ContainerType {}
#[async_trait::async_trait]
impl<T: ObjectType> ObjectType for &T {}
/// A GraphQL interface.
pub trait InterfaceType: ContainerType {}
/// A GraphQL interface.
pub trait UnionType: ContainerType {}
/// A GraphQL input object.
pub trait InputObjectType: InputType {}
impl<T: Type + ?Sized> Type for Box<T> {
2021-01-10 03:21:47 +00:00
fn type_name() -> Cow<'static, str> {
T::type_name()
}
fn create_type_info(registry: &mut Registry) -> String {
T::create_type_info(registry)
}
}
#[async_trait::async_trait]
impl<T: OutputType + ?Sized> OutputType for Box<T> {
2021-01-10 03:21:47 +00:00
#[allow(clippy::trivially_copy_pass_by_ref)]
2021-06-07 12:51:20 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value> {
2021-01-10 03:21:47 +00:00
T::resolve(&**self, ctx, field).await
}
}
#[async_trait::async_trait]
impl<T: InputType> InputType for Box<T> {
fn parse(value: Option<ConstValue>) -> InputValueResult<Self> {
2021-01-11 01:01:28 +00:00
T::parse(value)
.map(Box::new)
.map_err(InputValueError::propagate)
2020-05-03 14:32:37 +00:00
}
fn to_value(&self) -> ConstValue {
T::to_value(&self)
2020-05-03 14:32:37 +00:00
}
}
2020-05-03 14:32:37 +00:00
impl<T: Type + ?Sized> Type for Arc<T> {
fn type_name() -> Cow<'static, str> {
T::type_name()
}
fn create_type_info(registry: &mut 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: OutputType + ?Sized> OutputType for Arc<T> {
#[allow(clippy::trivially_copy_pass_by_ref)]
2021-06-07 12:51:20 +00:00
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value> {
T::resolve(&**self, ctx, field).await
2020-05-03 14:32:37 +00:00
}
}
2020-09-29 23:45:48 +00:00
impl<T: InputType> InputType for Arc<T> {
fn parse(value: Option<ConstValue>) -> InputValueResult<Self> {
2021-01-11 01:01:28 +00:00
T::parse(value)
.map(Arc::new)
.map_err(InputValueError::propagate)
}
2020-09-29 23:45:48 +00:00
fn to_value(&self) -> ConstValue {
T::to_value(&self)
}
}
2021-03-17 12:51:30 +00:00
#[doc(hidden)]
#[async_trait::async_trait]
pub trait ComplexObject {
fn fields(registry: &mut registry::Registry) -> Vec<(String, registry::MetaField)>;
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>>;
}