Add test for ErrorExtensions.
This commit is contained in:
parent
fab82f15f3
commit
c3d70b0553
46
src/base.rs
46
src/base.rs
|
@ -4,7 +4,6 @@ use crate::{
|
|||
registry, ContextSelectionSet, FieldResult, InputValueResult, Positioned, Result, Value,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Represents a GraphQL type
|
||||
///
|
||||
|
@ -113,51 +112,6 @@ impl<T: OutputValueType + Send + Sync> OutputValueType for &T {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Type + Send + Sync> Type for Box<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
T::create_type_info(registry)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputValueType + Send + Sync> OutputValueType for Box<T> {
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
#[allow(clippy::borrowed_box)]
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> Result<serde_json::Value> {
|
||||
T::resolve(&*self, ctx, field).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Type + Send + Sync> Type for Arc<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
T::create_type_info(registry)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputValueType + Send + Sync> OutputValueType for Arc<T> {
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> Result<serde_json::Value> {
|
||||
T::resolve(&*self, ctx, field).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Type> Type for FieldResult<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
|
|
72
tests/error_ext.rs
Normal file
72
tests/error_ext.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use async_graphql::*;
|
||||
|
||||
#[async_std::test]
|
||||
pub async fn test_error_extensions() {
|
||||
struct Query;
|
||||
|
||||
#[GQLObject]
|
||||
impl Query {
|
||||
async fn extend_err(&self) -> FieldResult<i32> {
|
||||
Err("my error".extend_with(|err| {
|
||||
serde_json::json!({
|
||||
"msg": err,
|
||||
"code": 100
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
async fn extend_result(&self) -> FieldResult<i32> {
|
||||
Err(FieldError::from("my error"))
|
||||
.extend_err(|_| {
|
||||
serde_json::json!({
|
||||
"msg": "my error",
|
||||
"code": 100
|
||||
})
|
||||
})
|
||||
.extend_err(|_| {
|
||||
serde_json::json!({
|
||||
"code2": 20
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(&schema.execute("{ extendErr }").await).unwrap(),
|
||||
serde_json::json!({
|
||||
"errors": [{
|
||||
"message": "my error",
|
||||
"locations": [{
|
||||
"column": 3,
|
||||
"line": 1,
|
||||
}],
|
||||
"path": ["extendErr"],
|
||||
"extensions": {
|
||||
"msg": "my error",
|
||||
"code": 100
|
||||
}
|
||||
}]
|
||||
})
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_value(&schema.execute("{ extendResult }").await).unwrap(),
|
||||
serde_json::json!({
|
||||
"errors": [{
|
||||
"message": "my error",
|
||||
"locations": [{
|
||||
"column": 3,
|
||||
"line": 1,
|
||||
}],
|
||||
"path": ["extendResult"],
|
||||
"extensions": {
|
||||
"msg": "my error",
|
||||
"code": 100,
|
||||
"code2": 20
|
||||
}
|
||||
}]
|
||||
})
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue
Block a user