Skip to content

Fix Rocket integration #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Change log
==========

## [Unreleased]

The repository was restructured to a multi crate workspace to enable several new features like custom_derive and an extracted parser.

### New features

* New juniper_codegen crate which provides custom derives:
* `#[derive(GraphQLInputObject)]`
* `#[derive(GraphQLEnum)]`

## [0.8.1] – 2017-06-15

Tiny release to fix broken crate metadata on crates.io.
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ members = [
"juniper",
"juniper_codegen",
"juniper_tests",
"juniper_rocket_integration",
]
4 changes: 2 additions & 2 deletions juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ serde_json = { version = "^1.0.2", optional = true }
iron = { version = "^0.5.1", optional = true }
urlencoded = { version = "^0.5.0", optional = true }

rocket = { version = "^0.2.8", optional = true }
rocket_codegen = { version = "^0.2.8", optional = true }
rocket = { version = "^0.3.0", optional = true }
rocket_codegen = { version = "^0.3.0", optional = true }

[dev-dependencies]
iron = "^0.5.1"
Expand Down
4 changes: 2 additions & 2 deletions juniper/examples/rocket-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use juniper::rocket_handlers;
type Schema = RootNode<'static, Database, EmptyMutation<Database>>;

#[get("/")]
fn graphiql() -> content::HTML<String> {
fn graphiql() -> content::Html<String> {
rocket_handlers::graphiql_source("/graphql")
}

Expand Down Expand Up @@ -43,4 +43,4 @@ fn main() {
.manage(Schema::new(Database::new(), EmptyMutation::<Database>::new()))
.mount("/", routes![graphiql, get_graphql_handler, post_graphql_handler])
.launch();
}
}
31 changes: 19 additions & 12 deletions juniper/src/integrations/rocket_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::error::Error;
use serde_json;

use rocket::Request;
use rocket::request::{FromForm, FormItems, FromFormValue};
use rocket::request::{FromForm, FormItems};
use rocket::data::{FromData, Outcome as FromDataOutcome};
use rocket::response::{Responder, Response, content};
use rocket::http::{ContentType, Status};
Expand All @@ -37,8 +37,8 @@ pub struct GraphQLRequest(http::GraphQLRequest);
pub struct GraphQLResponse(Status, String);

/// Generate an HTML page containing GraphiQL
pub fn graphiql_source(graphql_endpoint_url: &str) -> content::HTML<String> {
content::HTML(::graphiql::graphiql_source(graphql_endpoint_url))
pub fn graphiql_source(graphql_endpoint_url: &str) -> content::Html<String> {
content::Html(::graphiql::graphiql_source(graphql_endpoint_url))
}

impl GraphQLRequest {
Expand All @@ -63,39 +63,46 @@ impl GraphQLRequest {
impl<'f> FromForm<'f> for GraphQLRequest {
type Error = String;

fn from_form_items(form_items: &mut FormItems<'f>) -> Result<Self, String> {
fn from_form(
form_items: &mut FormItems<'f>,
strict: bool
) -> Result<Self, String> {
let mut query = None;
let mut operation_name = None;
let mut variables = None;

for (key, value) in form_items {
match key {
match key.as_str() {
"query" => {
if query.is_some() {
return Err("Query parameter must not occur more than once".to_owned());
}
else {
query = Some(String::from_form_value(value)?);
query = Some(value.as_str().to_string());
}
}
"operation_name" => {
if operation_name.is_some() {
return Err("Operation name parameter must not occur more than once".to_owned());
}
else {
operation_name = Some(String::from_form_value(value)?);
operation_name = Some(value.as_str().to_string());
}
}
"variables" => {
if variables.is_some() {
return Err("Variables parameter must not occur more than once".to_owned());
}
else {
variables = Some(serde_json::from_str::<InputValue>(&String::from_form_value(value)?)
variables = Some(serde_json::from_str::<InputValue>(value.as_str())
.map_err(|err| err.description().to_owned())?);
}
}
_ => {}
_ => {
if strict {
return Err(format!("Prohibited extra field '{}'", key).to_owned());
}
}
}
}

Expand All @@ -115,7 +122,7 @@ impl<'f> FromForm<'f> for GraphQLRequest {
impl FromData for GraphQLRequest {
type Error = String;

fn from_data(request: &Request, data: Data) -> FromDataOutcome<Self, String> {
fn from_data(request: &Request, data: Data) -> FromDataOutcome<Self, Self::Error> {
if !request.content_type().map_or(false, |ct| ct.is_json()) {
return Forward(data);
}
Expand All @@ -135,7 +142,7 @@ impl FromData for GraphQLRequest {
}

impl<'r> Responder<'r> for GraphQLResponse {
fn respond(self) -> Result<Response<'r>, Status> {
fn respond_to(self, _: &Request) -> Result<Response<'r>, Status> {
let GraphQLResponse(status, body) = self;

Ok(Response::build()
Expand Down Expand Up @@ -230,4 +237,4 @@ mod tests {
content_type: content_type,
}
}
}
}
21 changes: 21 additions & 0 deletions juniper_rocket_integration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "juniper_rocket_integration"
version = "0.0.1"
authors = ["Magnus Hallin <[email protected]>"]
description = "Juniper GraphQL integration with Rocket"
license = "BSD-2-Clause"
documentation = "https://docs.rs/juniper"
repository = "https://github.com/mhallin/juniper"

[dependencies]
juniper = { path = "../juniper" }
juniper_codegen = { path = "../juniper_codegen" }
rocket = { version = "^0.3.0" }
rocket_codegen = { version = "^0.3.0" }
serde = { version = "^1.0.8" }
serde_derive = {version="^1.0.8" }
serde_json = { version = "^1.0.2" }

[badges]
travis-ci = { repository = "mhallin/juniper" }
appveyor = { repository = "mhallin/juniper" }
Loading