2020-10-15 05:55:30 +00:00
|
|
|
use std::borrow::Cow;
|
2021-01-10 03:21:47 +00:00
|
|
|
use std::sync::Arc;
|
2020-10-15 05:55:30 +00:00
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
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};
|
2020-05-09 09:55:04 +00:00
|
|
|
use crate::{
|
2021-04-09 07:04:01 +00:00
|
|
|
ContainerType, Context, ContextSelectionSet, Error, InputValueError, InputValueResult,
|
|
|
|
Positioned, Result, ServerResult, Value,
|
2020-05-09 09:55:04 +00:00
|
|
|
};
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-10-26 13:34:05 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub trait Description {
|
|
|
|
fn description() -> &'static str;
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
/// Represents a GraphQL input type.
|
|
|
|
pub trait InputType: Send + Sync + Sized {
|
2021-11-18 07:43:12 +00:00
|
|
|
/// 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.
|
2020-05-28 07:00:55 +00:00
|
|
|
fn parse(value: Option<Value>) -> InputValueResult<Self>;
|
2020-05-26 10:34:43 +00:00
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Convert to a `Value` for introspection.
|
2020-05-26 10:34:43 +00:00
|
|
|
fn to_value(&self) -> Value;
|
2021-02-21 13:29:03 +00:00
|
|
|
|
|
|
|
/// Get the federation fields, only for InputObject.
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn federation_fields() -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
2021-11-18 07:43:12 +00:00
|
|
|
|
|
|
|
/// Returns a reference to the raw value.
|
|
|
|
fn as_raw_value(&self) -> Option<&Self::RawValueType>;
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
/// Represents a GraphQL output type.
|
2020-03-03 03:48:00 +00:00
|
|
|
#[async_trait::async_trait]
|
2021-11-09 10:52:10 +00:00
|
|
|
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
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +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
|
|
|
}
|
|
|
|
}
|
2020-04-25 06:51:26 +00:00
|
|
|
|
2021-11-09 10:52:10 +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()
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
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> {
|
2021-01-10 05:48:18 +00:00
|
|
|
match self {
|
2021-06-07 06:03:36 +00:00
|
|
|
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-06-07 06:03:36 +00:00
|
|
|
}
|
2021-01-10 05:48:18 +00:00
|
|
|
}
|
2021-01-10 03:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
/// A GraphQL object.
|
|
|
|
pub trait ObjectType: ContainerType {}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2021-01-14 04:41:59 +00:00
|
|
|
impl<T: ObjectType> ObjectType for &T {}
|
2021-01-10 05:48:18 +00:00
|
|
|
|
|
|
|
/// A GraphQL interface.
|
|
|
|
pub trait InterfaceType: ContainerType {}
|
|
|
|
|
|
|
|
/// A GraphQL interface.
|
|
|
|
pub trait UnionType: ContainerType {}
|
|
|
|
|
|
|
|
/// A GraphQL input object.
|
|
|
|
pub trait InputObjectType: InputType {}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
#[async_trait::async_trait]
|
2021-01-14 04:41:59 +00:00
|
|
|
impl<T: InputType> InputType for Box<T> {
|
2021-11-18 07:43:12 +00:00
|
|
|
type RawValueType = T::RawValueType;
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
fn type_name() -> Cow<'static, str> {
|
|
|
|
T::type_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut Registry) -> String {
|
|
|
|
T::create_type_info(registry)
|
|
|
|
}
|
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
fn to_value(&self) -> ConstValue {
|
|
|
|
T::to_value(&self)
|
2020-05-03 14:32:37 +00:00
|
|
|
}
|
2021-11-18 07:43:12 +00:00
|
|
|
|
|
|
|
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
|
|
|
|
self.as_ref().as_raw_value()
|
|
|
|
}
|
2021-01-10 05:48:18 +00:00
|
|
|
}
|
2020-05-03 14:32:37 +00:00
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: OutputType + ?Sized> OutputType for Arc<T> {
|
2021-01-10 05:48:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-10 05:48:18 +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 05:48:18 +00:00
|
|
|
T::resolve(&**self, ctx, field).await
|
2020-05-03 14:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 23:45:48 +00:00
|
|
|
|
2021-01-14 04:41:59 +00:00
|
|
|
impl<T: InputType> InputType for Arc<T> {
|
2021-11-18 07:43:12 +00:00
|
|
|
type RawValueType = T::RawValueType;
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
fn type_name() -> Cow<'static, str> {
|
|
|
|
T::type_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut Registry) -> String {
|
|
|
|
T::create_type_info(registry)
|
|
|
|
}
|
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
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)
|
2021-01-10 05:48:18 +00:00
|
|
|
}
|
2020-09-29 23:45:48 +00:00
|
|
|
|
2021-01-10 05:48:18 +00:00
|
|
|
fn to_value(&self) -> ConstValue {
|
|
|
|
T::to_value(&self)
|
|
|
|
}
|
2021-11-18 07:43:12 +00:00
|
|
|
|
|
|
|
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
|
|
|
|
self.as_ref().as_raw_value()
|
|
|
|
}
|
2021-01-10 05:48:18 +00:00
|
|
|
}
|
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>>;
|
|
|
|
}
|