(juniper_iron) fallible context factory (#180)
This commit is contained in:
parent
521930e911
commit
e63b42bc8e
3 changed files with 17 additions and 11 deletions
|
@ -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
|
||||
|
||||
### Changed
|
||||
|
|
|
@ -14,8 +14,8 @@ use juniper::EmptyMutation;
|
|||
use juniper_iron::{GraphQLHandler, GraphiQLHandler};
|
||||
use juniper::tests::model::Database;
|
||||
|
||||
fn context_factory(_: &mut Request) -> Database {
|
||||
Database::new()
|
||||
fn context_factory(_: &mut Request) -> IronResult<Database> {
|
||||
Ok(Database::new())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -62,8 +62,8 @@ use juniper::{Context, EmptyMutation};
|
|||
// This function is executed for every request. Here, we would realistically
|
||||
// provide a database connection or similar. For this example, we'll be
|
||||
// creating the database from scratch.
|
||||
fn context_factory(_: &mut Request) -> Database {
|
||||
Database {
|
||||
fn context_factory(_: &mut Request) -> IronResult<Database> {
|
||||
Ok(Database {
|
||||
users: vec![
|
||||
( "1000".to_owned(), User {
|
||||
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(),
|
||||
friend_ids: vec!["1000".to_owned()] } ),
|
||||
].into_iter().collect()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
impl Context for Database {}
|
||||
|
@ -190,7 +190,7 @@ impl<'a> GraphQLBatchResponse<'a> {
|
|||
/// mapping.
|
||||
pub struct GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT>
|
||||
where
|
||||
CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static,
|
||||
CtxFactory: Fn(&mut Request) -> IronResult<CtxT> + Send + Sync + 'static,
|
||||
CtxT: 'static,
|
||||
Query: 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>
|
||||
where
|
||||
CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static,
|
||||
CtxFactory: Fn(&mut Request) -> IronResult<CtxT> + Send + Sync + 'static,
|
||||
CtxT: 'static,
|
||||
Query: 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
|
||||
for GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT>
|
||||
where
|
||||
CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static,
|
||||
CtxFactory: Fn(&mut Request) -> IronResult<CtxT> + Send + Sync + 'static,
|
||||
CtxT: 'static,
|
||||
Query: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static,
|
||||
Mutation: GraphQLType<Context = CtxT, TypeInfo = ()> + Send + Sync + 'static,
|
||||
'a: 'static,
|
||||
{
|
||||
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 {
|
||||
method::Get => self.handle_get(&mut req)?,
|
||||
|
@ -439,8 +439,8 @@ mod tests {
|
|||
http_tests::run_http_test_suite(&integration);
|
||||
}
|
||||
|
||||
fn context_factory(_: &mut Request) -> Database {
|
||||
Database::new()
|
||||
fn context_factory(_: &mut Request) -> IronResult<Database> {
|
||||
Ok(Database::new())
|
||||
}
|
||||
|
||||
fn make_test_response(response: IronResult<Response>) -> http_tests::TestResponse {
|
||||
|
|
Loading…
Reference in a new issue