async-graphql/src/types/merged_object.rs

112 lines
3.0 KiB
Rust
Raw Normal View History

2020-10-15 06:38:10 +00:00
use std::borrow::Cow;
use indexmap::IndexMap;
2020-09-06 06:16:36 +00:00
use crate::parser::types::Field;
2020-08-09 04:35:15 +00:00
use crate::registry::{MetaType, Registry};
use crate::resolver_utils::resolve_container;
2020-08-09 04:35:15 +00:00
use crate::{
CacheControl, ContainerType, Context, ContextSelectionSet, ObjectType, OutputType, Positioned,
ServerResult, SimpleObject, Type, Value,
2020-08-09 04:35:15 +00:00
};
use async_graphql_value::ConstValue;
2020-08-09 04:35:15 +00:00
#[doc(hidden)]
pub struct MergedObject<A, B>(pub A, pub B);
2020-08-27 07:35:48 +00:00
impl<A: Type, B: Type> Type for MergedObject<A, B> {
2020-08-09 04:35:15 +00:00
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("{}_{}", A::type_name(), B::type_name()))
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_type::<Self, _>(|registry| {
let mut fields = IndexMap::new();
let mut cc = CacheControl::default();
if let MetaType::Object {
2020-08-09 04:35:15 +00:00
fields: a_fields,
cache_control: a_cc,
..
} = registry.create_dummy_type::<A>()
2020-08-09 04:35:15 +00:00
{
fields.extend(a_fields);
2020-09-17 08:39:55 +00:00
cc = cc.merge(&a_cc);
2020-08-09 04:35:15 +00:00
}
if let MetaType::Object {
2020-08-09 04:35:15 +00:00
fields: b_fields,
cache_control: b_cc,
..
} = registry.create_dummy_type::<B>()
2020-08-09 04:35:15 +00:00
{
fields.extend(b_fields);
2020-09-17 08:39:55 +00:00
cc = cc.merge(&b_cc);
2020-08-09 04:35:15 +00:00
}
MetaType::Object {
name: Self::type_name().to_string(),
description: None,
fields,
cache_control: cc,
extends: false,
keys: None,
}
})
}
}
#[async_trait::async_trait]
2020-09-29 23:45:48 +00:00
impl<A, B> ContainerType for MergedObject<A, B>
2020-08-09 04:35:15 +00:00
where
A: ObjectType + Send + Sync,
B: ObjectType + Send + Sync,
{
2020-10-10 02:32:43 +00:00
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
2020-08-09 04:35:15 +00:00
match self.0.resolve_field(ctx).await {
Rework errors This completely overhauls the error system used in async-graphql. - `Error` has been renamed to `ServerError` and `FieldError` has been renamed to just `Error`. This is because `FieldError` is by far the most common error that users will have to use so it makes sense to use the most obvious error name. Also, the current name didn't make sense as it was used for things other than field errors, such as the data callback for websockets. - `ServerError` has been made completely opaque. Before it was an enum of all the possible errors, but now it just contains an error message, the locations, the path and extensions. It is a shame that we lose information, it makes more sense as _conceptually_ GraphQL does not provide that information. It also frees us to change the internals of async-graphql a lot more. - The path of errors is no longer an opaque JSON value but a regular type, `Vec<PathSegment>`. The type duplication of `PathSegment` and `QueryPathSegment` is unfortunate, I plan to work on this in the future. - Now that `ServerError` is opaque, `RuleError` has been removed from the public API, making it simpler. - Additionally `QueryError` has been completely removed. Instead the error messages are constructed ad-hoc; I took care to never repeat an error message. - Instead of constructing field-not-found errors inside the implementations of field resolvers they now return `Option`s, where a `None` value is representative of the field not being found. - As an unfortunate consequence of the last change, self-referential types based on the output of a subscription resolver can no longer be created. This does not mean anything for users, but causes lifetime issues in the implementation of merged objects. I fixed it with a bit of a hack, but this'll have to be looked into further. - `InputValueError` now has a generic parameter - it's kind of weird but it's necessary for ergonomics. It also improves error messages. - The `ErrorExtensions` trait has been removed. I didn't think the `extend` method was necessary since `From` impls exist. But the ergonomics are still there with a new trait `ExtendError`, which is implemented for both errors and results. - `Response` now supports serializing multiple errors. This allows for nice things like having multiple validation errors not be awkwardly shoved into a single error. - When an error occurs in execution, data is sent as `null`. This is slightly more compliant with the spec but the algorithm described in <https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has yet to be implemented.
2020-09-29 19:06:44 +00:00
Ok(Some(value)) => Ok(Some(value)),
Ok(None) => self.1.resolve_field(ctx).await,
2020-08-09 04:35:15 +00:00
Err(err) => Err(err),
}
}
async fn find_entity(
&self,
ctx: &Context<'_>,
params: &ConstValue,
) -> ServerResult<Option<ConstValue>> {
match self.0.find_entity(ctx, params).await {
Ok(Some(value)) => Ok(Some(value)),
Ok(None) => self.1.find_entity(ctx, params).await,
Err(err) => Err(err),
}
}
2020-08-09 04:35:15 +00:00
}
#[async_trait::async_trait]
impl<A, B> OutputType for MergedObject<A, B>
2020-08-09 04:35:15 +00:00
where
A: ObjectType + Send + Sync,
B: ObjectType + Send + Sync,
{
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
_field: &Positioned<Field>,
2020-10-10 02:32:43 +00:00
) -> ServerResult<Value> {
2020-09-29 23:45:48 +00:00
resolve_container(ctx, self).await
2020-08-09 04:35:15 +00:00
}
}
2020-09-29 23:45:48 +00:00
impl<A, B> ObjectType for MergedObject<A, B>
where
A: ObjectType + Send + Sync,
B: ObjectType + Send + Sync,
{
}
2020-08-09 04:35:15 +00:00
#[doc(hidden)]
#[derive(SimpleObject, Default)]
#[graphql(internal, dummy)]
2020-08-09 04:35:15 +00:00
pub struct MergedObjectTail;