From 7d7e7e625225cda3c5bcced8c2931d3103a8e6b5 Mon Sep 17 00:00:00 2001 From: Magnus Hallin Date: Wed, 14 Jun 2017 09:02:56 +0200 Subject: [PATCH] Don't depend on serde_json by default, rename some structs --- Cargo.toml | 8 ++++---- src/integrations/iron_handlers.rs | 12 ++++++------ src/integrations/serde.rs | 18 +++++------------- src/lib.rs | 11 ++++++----- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a90db630..8a2edc40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,16 +20,16 @@ name = "server" required-features = ["iron-handlers", "expose-test-schema"] [features] -default = ["serde", "serde_json"] nightly = [] -iron-handlers = ["iron", "default", "serde_derive", "urlencoded"] +iron-handlers = ["iron", "serde_json", "urlencoded"] expose-test-schema = [] [dependencies] +serde = { version = "^0.9.1" } +serde_derive = {version="^0.9.1" } + iron = { version = "^0.5.1", optional = true } -serde = { version = "^0.9.1", optional = true } serde_json = {version ="^0.9.1", optional = true } -serde_derive = {version="^0.9.1", optional = true } urlencoded = {version="0.5", optional=true} diff --git a/src/integrations/iron_handlers.rs b/src/integrations/iron_handlers.rs index 87f9776a..494cfbd8 100644 --- a/src/integrations/iron_handlers.rs +++ b/src/integrations/iron_handlers.rs @@ -17,7 +17,7 @@ use serde_json; use serde_json::error::Error as SerdeError; use ::{InputValue, GraphQLType, RootNode, execute}; -use super::serde::{WrappedGraphQLResult, GraphQlQuery}; +use super::serde::{WrappedGraphQLResult, GraphQLQuery}; /// Handler that executes GraphQL queries in the given schema /// @@ -112,7 +112,7 @@ impl<'a, CtxFactory, Query, Mutation, CtxT> } - fn handle_get(&self, req: &mut Request) -> IronResult { + fn handle_get(&self, req: &mut Request) -> IronResult { match req.get_mut::() { Ok(ref mut query_string) => { let input_query = parse_url_param(query_string.remove("query").to_owned())?; @@ -121,7 +121,7 @@ impl<'a, CtxFactory, Query, Mutation, CtxT> parse_url_param(query_string.remove("operationName"))?; let input_variables = parse_variable_param(query_string.remove("variables"))?; - Ok(GraphQlQuery::new(query,operation_name,input_variables)) + Ok(GraphQLQuery::new(query,operation_name,input_variables)) } else { Err(IronError::new( Box::new(GraphQlIronError::IO(IoError::new(ErrorKind::InvalidData, @@ -138,10 +138,10 @@ impl<'a, CtxFactory, Query, Mutation, CtxT> } } - fn handle_post(&self, req: &mut Request) -> IronResult { + fn handle_post(&self, req: &mut Request) -> IronResult { let mut request_payload = String::new(); itry!(req.body.read_to_string(&mut request_payload)); - let graphql_query = serde_json::from_str::(request_payload.as_str()).map_err(|err|{ + let graphql_query = serde_json::from_str::(request_payload.as_str()).map_err(|err|{ IronError::new( Box::new(GraphQlIronError::Serde(err)), (status::BadRequest, "No JSON object was decoded.")) @@ -149,7 +149,7 @@ impl<'a, CtxFactory, Query, Mutation, CtxT> graphql_query } - fn respond(&self, req: &mut Request, graphql: GraphQlQuery) -> IronResult { + fn respond(&self, req: &mut Request, graphql: GraphQLQuery) -> IronResult { let context = (self.context_factory)(req); let variables = graphql.variables(); let result = execute(graphql.query(), diff --git a/src/integrations/serde.rs b/src/integrations/serde.rs index 3595b9b5..63c40cce 100644 --- a/src/integrations/serde.rs +++ b/src/integrations/serde.rs @@ -3,9 +3,7 @@ use serde::ser::SerializeMap; use std::fmt; use std::collections::HashMap; -use ::{GraphQLError, Value}; -#[cfg(feature="iron-handlers")] -use ::Variables; +use ::{GraphQLError, Value, Variables}; use ast::InputValue; use executor::ExecutionError; use parser::{ParseError, Spanning, SourcePosition}; @@ -218,23 +216,20 @@ impl ser::Serialize for Value { } /// The expected structure of the decoded JSON Document for either Post or Get requests. -#[cfg(feature="iron-handlers")] #[derive(Deserialize)] -pub struct GraphQlQuery { +pub struct GraphQLQuery { query: String, #[serde(rename = "operationName")] operation_name: Option, variables: Option } -#[cfg(feature="iron-handlers")] -impl GraphQlQuery { - +impl GraphQLQuery { pub fn new(query: String, operation_name: Option, variables: Option - ) -> GraphQlQuery { - GraphQlQuery { + ) -> GraphQLQuery { + GraphQLQuery { query: query, operation_name: operation_name, variables: variables @@ -260,10 +255,8 @@ impl GraphQlQuery { } -#[cfg(feature="iron-handlers")] pub struct WrappedGraphQLResult<'a>(Result<(Value, Vec), GraphQLError<'a>>); -#[cfg(feature="iron-handlers")] impl<'a> WrappedGraphQLResult<'a> { pub fn new(result: Result<(Value, Vec), GraphQLError<'a>> @@ -272,7 +265,6 @@ impl<'a> WrappedGraphQLResult<'a> { } } -#[cfg(feature="iron-handlers")] impl<'a> ser::Serialize for WrappedGraphQLResult<'a> { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, diff --git a/src/lib.rs b/src/lib.rs index 0e0bc631..4ae89a93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,9 +14,9 @@ schema, as well as an optional integration into the [Iron framework][2]. It tries to keep the number of dynamic operations to a minimum, and give you as the schema developer the control of the query execution path. -Juniper only depends on `serde` and `serde_json` by default, making it lightweight and -easy to drop into any project. Through the `iron-handlers` feature, it also -depends on Iron. +Juniper only depends on `serde` and `serde_derive` by default, making it +lightweight and easy to drop into any project. Through the `iron-handlers` +feature, it also depends on Iron. ## Exposing data types @@ -190,8 +190,9 @@ built-in [GraphiQL][6] handler included. #[cfg(feature="iron-handlers")] extern crate urlencoded; #[cfg(test)] extern crate iron_test; extern crate serde; -extern crate serde_json; -#[cfg(feature="iron-handlers")] #[macro_use] extern crate serde_derive; +#[macro_use] extern crate serde_derive; + +#[cfg(feature="iron-handlers")] extern crate serde_json; #[macro_use] mod macros; mod ast;