async-graphql/src/types/connection/connection_type.rs
Koxiaet 50009b66ce 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 20:06:44 +01:00

237 lines
7.7 KiB
Rust

use crate::connection::edge::Edge;
use crate::connection::page_info::PageInfo;
use crate::parser::types::Field;
use crate::resolver_utils::{resolve_object, ObjectType};
use crate::types::connection::{CursorType, EmptyFields};
use crate::{
registry, Context, ContextSelectionSet, OutputValueType, Positioned, Result, ServerResult, Type,
};
use futures::{Stream, StreamExt, TryStreamExt};
use indexmap::map::IndexMap;
use std::borrow::Cow;
/// Connection type
///
/// Connection is the result of a query for `connection::query`.
pub struct Connection<C, T, EC = EmptyFields, EE = EmptyFields> {
/// All edges of the current page.
edges: Vec<Edge<C, T, EE>>,
additional_fields: EC,
has_previous_page: bool,
has_next_page: bool,
}
impl<C, T, EE> Connection<C, T, EmptyFields, EE> {
/// Create a new connection.
pub fn new(has_previous_page: bool, has_next_page: bool) -> Self {
Connection {
additional_fields: EmptyFields,
has_previous_page,
has_next_page,
edges: Vec::new(),
}
}
}
impl<C, T, EC, EE> Connection<C, T, EC, EE> {
/// Create a new connection, it can have some additional fields.
pub fn with_additional_fields(
has_previous_page: bool,
has_next_page: bool,
additional_fields: EC,
) -> Self {
Connection {
additional_fields,
has_previous_page,
has_next_page,
edges: Vec::new(),
}
}
}
impl<C, T, EC, EE> Connection<C, T, EC, EE> {
/// Convert the edge type and return a new `Connection`.
pub fn map<T2, EE2, F>(self, mut f: F) -> Connection<C, T2, EC, EE2>
where
F: FnMut(Edge<C, T, EE>) -> Edge<C, T2, EE2>,
{
let mut new_edges = Vec::with_capacity(self.edges.len());
for edge in self.edges {
new_edges.push(f(edge));
}
Connection {
edges: new_edges,
additional_fields: self.additional_fields,
has_previous_page: self.has_previous_page,
has_next_page: self.has_next_page,
}
}
/// Convert the node type and return a new `Connection`.
pub fn map_node<T2, F>(self, mut f: F) -> Connection<C, T2, EC, EE>
where
F: FnMut(T) -> T2,
{
self.map(|edge| Edge {
cursor: edge.cursor,
node: f(edge.node),
additional_fields: edge.additional_fields,
})
}
/// Append edges with `IntoIterator<Item = Edge<C, T, EE>>`
pub fn append<I>(&mut self, iter: I)
where
I: IntoIterator<Item = Edge<C, T, EE>>,
{
self.edges.extend(iter);
}
/// Append edges with `IntoIterator<Item = Edge<C, T, EE>>`
pub fn try_append<I>(&mut self, iter: I) -> Result<()>
where
I: IntoIterator<Item = Result<Edge<C, T, EE>>>,
{
for edge in iter {
self.edges.push(edge?);
}
Ok(())
}
/// Append edges with `Stream<Item = Result<Edge<C, T, EE>>>`
pub async fn append_stream<S>(&mut self, stream: S)
where
S: Stream<Item = Edge<C, T, EE>> + Unpin,
{
self.edges.extend(stream.collect::<Vec<_>>().await);
}
/// Append edges with `Stream<Item = Result<Edge<C, T, EE>>>`
pub async fn try_append_stream<S>(&mut self, stream: S) -> Result<()>
where
S: Stream<Item = Result<Edge<C, T, EE>>> + Unpin,
{
self.edges.extend(stream.try_collect::<Vec<_>>().await?);
Ok(())
}
}
impl<C, T, EC, EE> Type for Connection<C, T, EC, EE>
where
C: CursorType,
T: OutputValueType + Send + Sync,
EC: ObjectType + Sync + Send,
EE: ObjectType + Sync + Send,
{
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("{}Connection", T::type_name()))
}
fn create_type_info(registry: &mut registry::Registry) -> String {
registry.create_type::<Self, _>(|registry| {
EC::create_type_info(registry);
let additional_fields = if let Some(registry::MetaType::Object { fields, .. }) =
registry.types.remove(EC::type_name().as_ref())
{
fields
} else {
unreachable!()
};
registry::MetaType::Object {
name: Self::type_name().to_string(),
description: None,
fields: {
let mut fields = IndexMap::new();
fields.insert(
"pageInfo".to_string(),
registry::MetaField {
name: "pageInfo".to_string(),
description: Some("Information to aid in pagination."),
args: Default::default(),
ty: PageInfo::create_type_info(registry),
deprecation: None,
cache_control: Default::default(),
external: false,
requires: None,
provides: None,
},
);
fields.insert(
"edges".to_string(),
registry::MetaField {
name: "edges".to_string(),
description: Some("A list of edges."),
args: Default::default(),
ty: <Option<Vec<Option<Edge<C, T, EE>>>> as Type>::create_type_info(
registry,
),
deprecation: None,
cache_control: Default::default(),
external: false,
requires: None,
provides: None,
},
);
fields.extend(additional_fields);
fields
},
cache_control: Default::default(),
extends: false,
keys: None,
}
})
}
}
#[async_trait::async_trait]
impl<C, T, EC, EE> ObjectType for Connection<C, T, EC, EE>
where
C: CursorType + Send + Sync,
T: OutputValueType + Send + Sync,
EC: ObjectType + Sync + Send,
EE: ObjectType + Sync + Send,
{
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<serde_json::Value>> {
if ctx.item.node.name.node == "pageInfo" {
let page_info = PageInfo {
has_previous_page: self.has_previous_page,
has_next_page: self.has_next_page,
start_cursor: self.edges.first().map(|edge| edge.cursor.encode_cursor()),
end_cursor: self.edges.last().map(|edge| edge.cursor.encode_cursor()),
};
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
return OutputValueType::resolve(&page_info, &ctx_obj, ctx.item)
.await
.map(Some);
} else if ctx.item.node.name.node == "edges" {
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
return OutputValueType::resolve(&self.edges, &ctx_obj, ctx.item)
.await
.map(Some);
}
self.additional_fields.resolve_field(ctx).await
}
}
#[async_trait::async_trait]
impl<C, T, EC, EE> OutputValueType for Connection<C, T, EC, EE>
where
C: CursorType + Send + Sync,
T: OutputValueType + Send + Sync,
EC: ObjectType + Sync + Send,
EE: ObjectType + Sync + Send,
{
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
_field: &Positioned<Field>,
) -> ServerResult<serde_json::Value> {
resolve_object(ctx, self).await
}
}