async-graphql/src/base.rs

260 lines
6.7 KiB
Rust
Raw Normal View History

2022-04-19 04:25:11 +00:00
use std::{borrow::Cow, sync::Arc};
use async_graphql_value::ConstValue;
use crate::{
2022-04-19 04:25:11 +00:00
parser::types::Field,
registry::{self, Registry},
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;
}
2022-04-19 04:25:11 +00:00
/// Used to specify the GraphQL Type name.
pub trait TypeName: Send + Sync {
/// Returns a GraphQL type name.
fn type_name() -> Cow<'static, str>;
}
/// Represents a GraphQL input type.
pub trait InputType: Send + Sync + Sized {
/// The raw type used for validator.
///
/// Usually it is `Self`, but the wrapper type is its internal type.
///
/// For example:
///
/// `i32::RawValueType` is `i32`
/// `Option<i32>::RawValueType` is `i32`.
type RawValueType;
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-03 03:48:00 +00:00
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
}
/// Returns a reference to the raw value.
fn as_raw_value(&self) -> Option<&Self::RawValueType>;
2020-03-03 03:48:00 +00:00
}
/// Represents a GraphQL output type.
2020-03-03 03:48:00 +00:00
#[async_trait::async_trait]
pub trait OutputType: Send + Sync {
/// Type the name.
fn type_name() -> Cow<'static, str>;
/// Qualified typename.
fn qualified_type_name() -> String {
format!("{}!", Self::type_name())
}
/// Introspection type name
///
2022-04-19 04:25:11 +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()
}
/// Create type information in the registry and return qualified typename.
fn create_type_info(registry: &mut registry::Registry) -> String;
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
}
#[async_trait::async_trait]
impl<T: OutputType + ?Sized> OutputType 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-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
}
}
#[async_trait::async_trait]
impl<T: OutputType + Sync, E: Into<Error> + Send + Sync + Clone> OutputType for Result<T, E> {
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 {
2021-01-10 03:21:47 +00:00
T::create_type_info(registry)
}
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 + ?Sized> ObjectType for &T {}
#[async_trait::async_trait]
impl<T: ObjectType + ?Sized> ObjectType for Box<T> {}
#[async_trait::async_trait]
impl<T: ObjectType + ?Sized> ObjectType for Arc<T> {}
/// A GraphQL interface.
pub trait InterfaceType: ContainerType {}
/// A GraphQL interface.
pub trait UnionType: ContainerType {}
/// A GraphQL input object.
pub trait InputObjectType: InputType {}
/// A GraphQL oneof input object.
pub trait OneofObjectType: InputObjectType {}
#[async_trait::async_trait]
impl<T: OutputType + ?Sized> OutputType 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)
}
#[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> {
type RawValueType = T::RawValueType;
fn type_name() -> Cow<'static, str> {
T::type_name()
}
fn create_type_info(registry: &mut Registry) -> String {
T::create_type_info(registry)
}
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
}
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
self.as_ref().as_raw_value()
}
}
2020-05-03 14:32:37 +00:00
#[async_trait::async_trait]
impl<T: OutputType + ?Sized> OutputType 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
}
#[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> {
type RawValueType = T::RawValueType;
fn type_name() -> Cow<'static, str> {
T::type_name()
}
fn create_type_info(registry: &mut Registry) -> String {
T::create_type_info(registry)
}
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)
}
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
self.as_ref().as_raw_value()
}
}
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>>;
}