Skip to content

Commit

Permalink
Make Schema::execute return HTTP headers when an error occurs. #572
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Jul 15, 2021
1 parent 5ff488f commit d116dc2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,10 @@ where
};

let mut resp = match res {
Ok(value) => Response::new(value)
.http_headers(std::mem::take(&mut *env.http_headers.lock().unwrap())),
Ok(value) => Response::new(value),
Err(err) => Response::from_errors(vec![err]),
};
}
.http_headers(std::mem::take(&mut *env.http_headers.lock().unwrap()));

resp.errors
.extend(std::mem::take(&mut *env.errors.lock().unwrap()));
Expand Down
26 changes: 26 additions & 0 deletions tests/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,29 @@ pub async fn test_schema_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"));
}

0 comments on commit d116dc2

Please sign in to comment.