17 releases

0.7.6 Feb 2, 2022
0.7.4 Jun 8, 2021
0.7.2 Jan 28, 2021
0.7.0 Dec 10, 2020
0.1.1 Dec 3, 2017

#191 in HTTP server

Download history 19/week @ 2022-11-30 33/week @ 2022-12-07 16/week @ 2022-12-14 42/week @ 2022-12-21 19/week @ 2022-12-28 2/week @ 2023-01-04 20/week @ 2023-01-11 27/week @ 2023-01-18 41/week @ 2023-01-25 39/week @ 2023-02-01 42/week @ 2023-02-08 75/week @ 2023-02-15 34/week @ 2023-02-22 22/week @ 2023-03-01 29/week @ 2023-03-08 22/week @ 2023-03-15

112 downloads per month

BSD-2-Clause

1MB
37K SLoC

juniper_iron

This repository contains the Iron web framework integration for Juniper, a GraphQL implementation for Rust.

For documentation, including guides and examples, check out Juniper.

A basic usage example can also be found in the Api documentation.

Examples

Check examples/iron_server.rs for example code of a working Iron server with GraphQL handlers.

License

This project is under the BSD-2 license.

Check the LICENSE file for details.


lib.rs:

juniper_iron

This repository contains the Iron web framework integration for Juniper, a GraphQL implementation for Rust.

For documentation, including guides and examples, check out Juniper.

A basic usage example can also be found in the Api documentation.

Integrating with Iron

For example, continuing from the schema created above and using Iron to expose the schema on an HTTP endpoint supporting both GET and POST requests:

# use std::collections::HashMap;
#
use iron::prelude::*;
use juniper_iron::GraphQLHandler;
use juniper::{Context, EmptyMutation, EmptySubscription};
#
# use juniper::FieldResult;
#
# struct User { id: String, name: String, friend_ids: Vec<String>  }
# struct QueryRoot;
# struct Database { users: HashMap<String, User> }
#
# #[juniper::graphql_object(context = Database)]
# impl User {
#     fn id(&self) -> FieldResult<&String> {
#         Ok(&self.id)
#     }
#
#     fn name(&self) -> FieldResult<&String> {
#         Ok(&self.name)
#     }
#
#     fn friends(&self, context: &Database) -> FieldResult<Vec<&User>> {
#         Ok(self.friend_ids.iter()
#             .filter_map(|id| executor.context().users.get(id))
#             .collect())
#     }
# }
#
# #[juniper::graphql_object(context = Database, scalar = juniper::DefaultScalarValue)]
# impl QueryRoot {
#     fn user(context: &Database, id: String) -> FieldResult<Option<&User>> {
#         Ok(executor.context().users.get(&id))
#     }
# }

// 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) -> IronResult<Database> {
Ok(Database {
users: vec![
( "1000".to_owned(), User {
id: "1000".to_owned(), name: "Robin".to_owned(),
friend_ids: vec!["1001".to_owned()] } ),
( "1001".to_owned(), User {
id: "1001".to_owned(), name: "Max".to_owned(),
friend_ids: vec!["1000".to_owned()] } ),
].into_iter().collect()
})
}

impl Context for Database {}

fn main() {
// GraphQLHandler takes a context factory function, the root object,
// and the mutation object. If we don't have any mutations to expose, we
// can use the empty tuple () to indicate absence.
let graphql_endpoint = GraphQLHandler::new(
context_factory,
QueryRoot,
EmptyMutation::<Database>::new(),
EmptySubscription::<Database>::new(),
);

// Start serving the schema at the root on port 8080.
Iron::new(graphql_endpoint).http("localhost:8080").unwrap();
}

See the the GraphQLHandler documentation for more information on what request methods are supported.

Dependencies

~10MB
~233K SLoC