(juniper_iron) fallible context factory (#180)

This commit is contained in:
Andrey Kutejko 2018-07-13 10:30:14 +02:00 committed by Christian Legnitto
parent 521930e911
commit e63b42bc8e
3 changed files with 17 additions and 11 deletions

View file

@ -1,3 +1,9 @@
## [0.2.0] - 2018-05-10
### Changed
* (**breaking**) `context_factory` should return `IronResult<Context>` instead of `Context` now.
## [0.1.2] - 2018-02-10 ## [0.1.2] - 2018-02-10
### Changed ### Changed

View file

@ -14,8 +14,8 @@ use juniper::EmptyMutation;
use juniper_iron::{GraphQLHandler, GraphiQLHandler}; use juniper_iron::{GraphQLHandler, GraphiQLHandler};
use juniper::tests::model::Database; use juniper::tests::model::Database;
fn context_factory(_: &mut Request) -> Database { fn context_factory(_: &mut Request) -> IronResult<Database> {
Database::new() Ok(Database::new())
} }
fn main() { fn main() {

View file

@ -62,8 +62,8 @@ use juniper::{Context, EmptyMutation};
// This function is executed for every request. Here, we would realistically // This function is executed for every request. Here, we would realistically
// provide a database connection or similar. For this example, we'll be // provide a database connection or similar. For this example, we'll be
// creating the database from scratch. // creating the database from scratch.
fn context_factory(_: &mut Request) -> Database { fn context_factory(_: &mut Request) -> IronResult<Database> {
Database { Ok(Database {
users: vec![ users: vec![
( "1000".to_owned(), User { ( "1000".to_owned(), User {
id: "1000".to_owned(), name: "Robin".to_owned(), id: "1000".to_owned(), name: "Robin".to_owned(),
@ -72,7 +72,7 @@ fn context_factory(_: &mut Request) -> Database {
id: "1001".to_owned(), name: "Max".to_owned(), id: "1001".to_owned(), name: "Max".to_owned(),
friend_ids: vec!["1000".to_owned()] } ), friend_ids: vec!["1000".to_owned()] } ),
].into_iter().collect() ].into_iter().collect()
} })
} }
impl Context for Database {} impl Context for Database {}
@ -190,7 +190,7 @@ impl<'a> GraphQLBatchResponse<'a> {
/// mapping. /// mapping.
pub struct GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT> pub struct GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT>
where where
CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static, CtxFactory: Fn(&mut Request) -> IronResult<CtxT> + Send + Sync + 'static,
CtxT: 'static, CtxT: 'static,
Query: GraphQLType<Context = CtxT> + Send + Sync + 'static, Query: GraphQLType<Context = CtxT> + Send + Sync + 'static,
Mutation: GraphQLType<Context = CtxT> + Send + Sync + 'static, Mutation: GraphQLType<Context = CtxT> + Send + Sync + 'static,
@ -234,7 +234,7 @@ fn parse_variable_param(params: Option<Vec<String>>) -> IronResult<Option<InputV
impl<'a, CtxFactory, Query, Mutation, CtxT> GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT> impl<'a, CtxFactory, Query, Mutation, CtxT> GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT>
where where
CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static, CtxFactory: Fn(&mut Request) -> IronResult<CtxT> + Send + Sync + 'static,
CtxT: 'static, CtxT: 'static,
Query: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static, Query: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static,
Mutation: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static, Mutation: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static,
@ -307,14 +307,14 @@ impl GraphiQLHandler {
impl<'a, CtxFactory, Query, Mutation, CtxT> Handler impl<'a, CtxFactory, Query, Mutation, CtxT> Handler
for GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT> for GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT>
where where
CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static, CtxFactory: Fn(&mut Request) -> IronResult<CtxT> + Send + Sync + 'static,
CtxT: 'static, CtxT: 'static,
Query: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static, Query: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static,
Mutation: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static, Mutation: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static,
'a: 'static, 'a: 'static,
{ {
fn handle(&self, mut req: &mut Request) -> IronResult<Response> { fn handle(&self, mut req: &mut Request) -> IronResult<Response> {
let context = (self.context_factory)(req); let context = (self.context_factory)(req)?;
let graphql_request = match req.method { let graphql_request = match req.method {
method::Get => self.handle_get(&mut req)?, method::Get => self.handle_get(&mut req)?,
@ -439,8 +439,8 @@ mod tests {
http_tests::run_http_test_suite(&integration); http_tests::run_http_test_suite(&integration);
} }
fn context_factory(_: &mut Request) -> Database { fn context_factory(_: &mut Request) -> IronResult<Database> {
Database::new() Ok(Database::new())
} }
fn make_test_response(response: IronResult<Response>) -> http_tests::TestResponse { fn make_test_response(response: IronResult<Response>) -> http_tests::TestResponse {