Make Schema::execute
return HTTP headers when an error occurs. #572
This commit is contained in:
parent
5ff488fe68
commit
d116dc2950
|
@ -479,10 +479,10 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut resp = match res {
|
let mut resp = match res {
|
||||||
Ok(value) => Response::new(value)
|
Ok(value) => Response::new(value),
|
||||||
.http_headers(std::mem::take(&mut *env.http_headers.lock().unwrap())),
|
|
||||||
Err(err) => Response::from_errors(vec![err]),
|
Err(err) => Response::from_errors(vec![err]),
|
||||||
};
|
}
|
||||||
|
.http_headers(std::mem::take(&mut *env.http_headers.lock().unwrap()));
|
||||||
|
|
||||||
resp.errors
|
resp.errors
|
||||||
.extend(std::mem::take(&mut *env.errors.lock().unwrap()));
|
.extend(std::mem::take(&mut *env.errors.lock().unwrap()));
|
||||||
|
|
|
@ -16,3 +16,29 @@ pub async fn test_schema_default() {
|
||||||
|
|
||||||
let _schema = MySchema::default();
|
let _schema = MySchema::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
pub async fn test_http_headers() {
|
||||||
|
#[derive(Default)]
|
||||||
|
struct QueryRoot;
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl QueryRoot {
|
||||||
|
async fn value(&self, ctx: &Context<'_>) -> i32 {
|
||||||
|
ctx.insert_http_header("A", "1");
|
||||||
|
10
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn err(&self, ctx: &Context<'_>) -> FieldResult<i32> {
|
||||||
|
ctx.insert_http_header("A", "1");
|
||||||
|
Err("error".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
||||||
|
let resp = schema.execute("{ value }").await;
|
||||||
|
assert_eq!(resp.http_headers.get("A").map(|s| &**s), Some("1"));
|
||||||
|
|
||||||
|
let resp = schema.execute("{ err }").await;
|
||||||
|
assert_eq!(resp.http_headers.get("A").map(|s| &**s), Some("1"));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user