async-graphql/src/types/connection/edge.rs

154 lines
4.6 KiB
Rust
Raw Normal View History

2020-10-15 06:38:10 +00:00
use std::borrow::Cow;
use indexmap::map::IndexMap;
use crate::connection::EmptyFields;
2020-09-06 06:16:36 +00:00
use crate::parser::types::Field;
2020-09-29 23:45:48 +00:00
use crate::resolver_utils::{resolve_container, ContainerType};
use crate::types::connection::CursorType;
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
use crate::{
registry, Context, ContextSelectionSet, ObjectType, OutputType, Positioned, ServerResult, Type,
Value,
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
};
2020-03-19 09:20:12 +00:00
/// The edge type output by the data source
pub struct Edge<C, T, E> {
pub(crate) cursor: C,
pub(crate) node: T,
pub(crate) additional_fields: E,
2020-03-19 09:20:12 +00:00
}
impl<C, T, E> Edge<C, T, E> {
/// Create a new edge, it can have some additional fields.
pub fn with_additional_fields(cursor: C, node: T, additional_fields: E) -> Self {
Self {
cursor,
additional_fields,
node,
}
2020-04-26 04:34:08 +00:00
}
}
2020-04-26 04:34:08 +00:00
impl<C: CursorType, T> Edge<C, T, EmptyFields> {
/// Create a new edge.
pub fn new(cursor: C, node: T) -> Self {
Self {
cursor,
node,
additional_fields: EmptyFields,
}
2020-04-26 04:34:08 +00:00
}
}
impl<C, T, E> Type for Edge<C, T, E>
2020-03-19 09:20:12 +00:00
where
C: CursorType,
T: OutputType + Send + Sync,
E: ObjectType + Sync + Send,
2020-03-19 09:20:12 +00:00
{
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("{}Edge", T::type_name()))
}
fn create_type_info(registry: &mut registry::Registry) -> String {
registry.create_type::<Self, _>(|registry| {
let additional_fields = if let registry::MetaType::Object { fields, .. } =
registry.create_dummy_type::<E>()
2020-03-19 09:20:12 +00:00
{
fields
2020-03-19 09:20:12 +00:00
} else {
unreachable!()
};
registry::MetaType::Object {
2020-03-19 09:20:12 +00:00
name: Self::type_name().to_string(),
description: Some("An edge in a connection."),
fields: {
let mut fields = IndexMap::new();
2020-03-19 09:20:12 +00:00
fields.insert(
"node".to_string(),
registry::MetaField {
2020-03-19 09:20:12 +00:00
name: "node".to_string(),
description: Some("The item at the end of the edge"),
args: Default::default(),
ty: T::create_type_info(registry),
deprecation: None,
2020-03-22 08:45:59 +00:00
cache_control: Default::default(),
2020-04-09 14:03:09 +00:00
external: false,
requires: None,
provides: None,
2020-03-19 09:20:12 +00:00
},
);
fields.insert(
"cursor".to_string(),
registry::MetaField {
2020-03-19 09:20:12 +00:00
name: "cursor".to_string(),
description: Some("A cursor for use in pagination"),
args: Default::default(),
ty: String::create_type_info(registry),
deprecation: None,
2020-03-22 08:45:59 +00:00
cache_control: Default::default(),
2020-04-09 14:03:09 +00:00
external: false,
requires: None,
provides: None,
2020-03-19 09:20:12 +00:00
},
);
fields.extend(additional_fields);
2020-03-19 09:20:12 +00:00
fields
},
2020-03-22 08:45:59 +00:00
cache_control: Default::default(),
2020-04-09 14:03:09 +00:00
extends: false,
keys: None,
2020-03-19 09:20:12 +00:00
}
})
}
}
#[async_trait::async_trait]
2020-09-29 23:45:48 +00:00
impl<C, T, E> ContainerType for Edge<C, T, E>
2020-03-19 09:20:12 +00:00
where
C: CursorType + Send + Sync,
T: OutputType + Send + Sync,
E: ObjectType + Sync + Send,
2020-03-19 09:20:12 +00:00
{
2020-10-10 02:32:43 +00:00
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
2020-09-12 16:42:15 +00:00
if ctx.item.node.name.node == "node" {
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
return OutputType::resolve(&self.node, &ctx_obj, ctx.item)
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
.await
.map(Some);
2020-09-12 16:42:15 +00:00
} else if ctx.item.node.name.node == "cursor" {
2020-10-10 02:32:43 +00:00
return Ok(Some(Value::String(self.cursor.encode_cursor())));
2020-03-19 09:20:12 +00:00
}
self.additional_fields.resolve_field(ctx).await
2020-03-19 09:20:12 +00:00
}
}
#[async_trait::async_trait]
impl<C, T, E> OutputType for Edge<C, T, E>
2020-03-19 09:20:12 +00:00
where
C: CursorType + Send + Sync,
T: OutputType + Send + Sync,
E: ObjectType + Sync + Send,
2020-03-19 09:20:12 +00:00
{
2020-05-20 00:18:28 +00:00
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-03-19 09:20:12 +00:00
}
}
2020-09-29 23:45:48 +00:00
impl<C, T, E> ObjectType for Edge<C, T, E>
where
C: CursorType + Send + Sync,
T: OutputType + Send + Sync,
2020-09-29 23:45:48 +00:00
E: ObjectType + Sync + Send,
{
}