async-graphql/tests/result.rs

281 lines
6.9 KiB
Rust
Raw Normal View History

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 async_graphql::*;
use futures_util::stream::Stream;
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
#[tokio::test]
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
pub async fn test_fieldresult() {
struct Query;
#[Object]
impl Query {
async fn error(&self) -> Result<i32> {
Err("TestError".into())
}
async fn opt_error(&self) -> Option<Result<i32>> {
Some(Err("TestError".into()))
}
async fn vec_error(&self) -> Vec<Result<i32>> {
vec![Ok(1), Err("TestError".into())]
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
2022-04-19 03:06:54 +00:00
let resp = schema.execute("{ error1:optError error2:optError }").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
assert_eq!(
2022-04-19 03:06:54 +00:00
resp.data,
value!({
"error1": null,
"error2": null,
})
);
assert_eq!(
resp.errors,
vec![
ServerError {
message: "TestError".to_string(),
source: None,
locations: vec![Pos { line: 1, column: 3 }],
path: vec![PathSegment::Field("error1".to_owned())],
extensions: None,
},
ServerError {
message: "TestError".to_string(),
source: None,
locations: vec![Pos {
line: 1,
column: 19,
}],
path: vec![PathSegment::Field("error2".to_owned())],
extensions: None,
},
]
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
);
assert_eq!(
schema
.execute("{ optError }")
.await
.into_result()
.unwrap_err(),
vec![ServerError {
message: "TestError".to_string(),
2021-11-07 11:11:17 +00:00
source: None,
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
locations: vec![Pos { line: 1, column: 3 }],
path: vec![PathSegment::Field("optError".to_owned())],
extensions: None,
}]
);
assert_eq!(
schema
.execute("{ vecError }")
.await
.into_result()
.unwrap_err(),
vec![ServerError {
message: "TestError".to_string(),
2021-11-07 11:11:17 +00:00
source: None,
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
locations: vec![Pos { line: 1, column: 3 }],
path: vec![
PathSegment::Field("vecError".to_owned()),
PathSegment::Index(1)
],
extensions: None,
}]
);
}
#[tokio::test]
pub async fn test_custom_error() {
#[derive(Clone)]
struct MyError;
impl From<MyError> for Error {
fn from(_: MyError) -> Self {
Error::new("custom error")
}
}
#[derive(SimpleObject)]
#[graphql(complex)]
struct MyObj {
value1: i32,
}
#[ComplexObject]
impl MyObj {
async fn value2(&self) -> Result<i32, MyError> {
Err(MyError)
}
}
#[derive(Interface)]
#[graphql(field(name = "value2", type = "i32"))]
enum MyInterface {
MyObj(MyObj),
}
struct Query;
#[Object]
impl Query {
async fn value(&self) -> Result<i32, MyError> {
Err(MyError)
}
}
struct Subscription;
#[Subscription]
impl Subscription {
async fn value1(&self) -> Result<impl Stream<Item = i32>, MyError> {
Err::<futures_util::stream::Once<futures_util::future::Ready<i32>>, _>(MyError)
}
async fn value2(&self) -> impl Stream<Item = Result<i32, MyError>> {
futures_util::stream::once(async move { Err(MyError) })
}
}
}
2021-06-07 12:51:20 +00:00
#[tokio::test]
pub async fn test_error_propagation() {
struct ParentObject;
#[Object]
impl ParentObject {
async fn child(&self) -> ChildObject {
ChildObject
}
async fn child_opt(&self) -> Option<ChildObject> {
Some(ChildObject)
}
}
struct ChildObject;
#[Object]
impl ChildObject {
async fn name(&self) -> Result<i32> {
Err("myerror".into())
}
async fn name_opt(&self) -> Option<Result<i32>> {
Some(Err("myerror".into()))
}
}
struct Query;
#[Object]
impl Query {
async fn parent(&self) -> ParentObject {
ParentObject
}
async fn parent_opt(&self) -> Option<ParentObject> {
Some(ParentObject)
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
2022-04-19 03:06:54 +00:00
let resp = schema.execute("{ parent { child { name } } }").await;
2021-06-07 12:51:20 +00:00
assert_eq!(
2022-04-19 03:06:54 +00:00
resp.errors,
vec![ServerError {
message: "myerror".to_string(),
source: None,
locations: vec![Pos {
line: 1,
column: 20,
2021-06-07 12:51:20 +00:00
}],
2022-04-19 03:06:54 +00:00
path: vec![
PathSegment::Field("parent".to_owned()),
PathSegment::Field("child".to_owned()),
PathSegment::Field("name".to_owned()),
],
extensions: None,
}]
2021-06-07 12:51:20 +00:00
);
2022-04-19 03:06:54 +00:00
let resp = schema.execute("{ parent { childOpt { name } } }").await;
2021-06-07 12:51:20 +00:00
assert_eq!(
2022-04-19 03:06:54 +00:00
resp.data,
value!({
"parent": {
"childOpt": null,
}
})
);
assert_eq!(
resp.errors,
vec![ServerError {
message: "myerror".to_string(),
source: None,
locations: vec![Pos {
line: 1,
column: 23,
2021-06-07 12:51:20 +00:00
}],
2022-04-19 03:06:54 +00:00
path: vec![
PathSegment::Field("parent".to_owned()),
PathSegment::Field("childOpt".to_owned()),
PathSegment::Field("name".to_owned()),
],
extensions: None,
}]
2021-06-07 12:51:20 +00:00
);
2022-04-19 03:06:54 +00:00
let resp = schema.execute("{ parentOpt { child { name } } }").await;
assert_eq!(resp.data, value!({ "parentOpt": null }));
2021-06-07 12:51:20 +00:00
assert_eq!(
2022-04-19 03:06:54 +00:00
resp.errors,
vec![ServerError {
message: "myerror".to_string(),
source: None,
locations: vec![Pos {
line: 1,
column: 23,
2021-06-07 12:51:20 +00:00
}],
2022-04-19 03:06:54 +00:00
path: vec![
PathSegment::Field("parentOpt".to_owned()),
PathSegment::Field("child".to_owned()),
PathSegment::Field("name".to_owned()),
],
extensions: None,
}]
2021-06-08 11:04:06 +00:00
);
2022-04-19 03:06:54 +00:00
let resp = schema.execute("{ parentOpt { child { nameOpt } } }").await;
2021-06-08 11:04:06 +00:00
assert_eq!(
2022-04-19 03:06:54 +00:00
resp.data,
value!({
"parentOpt": {
"child": {
"nameOpt": null,
}
},
})
);
assert_eq!(
resp.errors,
vec![ServerError {
message: "myerror".to_string(),
source: None,
locations: vec![Pos {
line: 1,
column: 23,
2021-06-08 11:04:06 +00:00
}],
2022-04-19 03:06:54 +00:00
path: vec![
PathSegment::Field("parentOpt".to_owned()),
PathSegment::Field("child".to_owned()),
PathSegment::Field("nameOpt".to_owned()),
],
extensions: None,
}]
2021-06-07 12:51:20 +00:00
);
}