Don't depend on serde_json by default, rename some structs

This commit is contained in:
Magnus Hallin 2017-06-14 09:02:56 +02:00
parent 7041efeda9
commit 7d7e7e6252
4 changed files with 21 additions and 28 deletions

View file

@ -20,16 +20,16 @@ name = "server"
required-features = ["iron-handlers", "expose-test-schema"] required-features = ["iron-handlers", "expose-test-schema"]
[features] [features]
default = ["serde", "serde_json"]
nightly = [] nightly = []
iron-handlers = ["iron", "default", "serde_derive", "urlencoded"] iron-handlers = ["iron", "serde_json", "urlencoded"]
expose-test-schema = [] expose-test-schema = []
[dependencies] [dependencies]
serde = { version = "^0.9.1" }
serde_derive = {version="^0.9.1" }
iron = { version = "^0.5.1", optional = true } iron = { version = "^0.5.1", optional = true }
serde = { version = "^0.9.1", optional = true }
serde_json = {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} urlencoded = {version="0.5", optional=true}

View file

@ -17,7 +17,7 @@ use serde_json;
use serde_json::error::Error as SerdeError; use serde_json::error::Error as SerdeError;
use ::{InputValue, GraphQLType, RootNode, execute}; use ::{InputValue, GraphQLType, RootNode, execute};
use super::serde::{WrappedGraphQLResult, GraphQlQuery}; use super::serde::{WrappedGraphQLResult, GraphQLQuery};
/// Handler that executes GraphQL queries in the given schema /// 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<GraphQlQuery> { fn handle_get(&self, req: &mut Request) -> IronResult<GraphQLQuery> {
match req.get_mut::<UrlEncodedQuery>() { match req.get_mut::<UrlEncodedQuery>() {
Ok(ref mut query_string) => { Ok(ref mut query_string) => {
let input_query = parse_url_param(query_string.remove("query").to_owned())?; 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"))?; parse_url_param(query_string.remove("operationName"))?;
let input_variables = let input_variables =
parse_variable_param(query_string.remove("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 { } else {
Err(IronError::new( Err(IronError::new(
Box::new(GraphQlIronError::IO(IoError::new(ErrorKind::InvalidData, 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<GraphQlQuery> { fn handle_post(&self, req: &mut Request) -> IronResult<GraphQLQuery> {
let mut request_payload = String::new(); let mut request_payload = String::new();
itry!(req.body.read_to_string(&mut request_payload)); itry!(req.body.read_to_string(&mut request_payload));
let graphql_query = serde_json::from_str::<GraphQlQuery>(request_payload.as_str()).map_err(|err|{ let graphql_query = serde_json::from_str::<GraphQLQuery>(request_payload.as_str()).map_err(|err|{
IronError::new( IronError::new(
Box::new(GraphQlIronError::Serde(err)), Box::new(GraphQlIronError::Serde(err)),
(status::BadRequest, "No JSON object was decoded.")) (status::BadRequest, "No JSON object was decoded."))
@ -149,7 +149,7 @@ impl<'a, CtxFactory, Query, Mutation, CtxT>
graphql_query graphql_query
} }
fn respond(&self, req: &mut Request, graphql: GraphQlQuery) -> IronResult<Response> { fn respond(&self, req: &mut Request, graphql: GraphQLQuery) -> IronResult<Response> {
let context = (self.context_factory)(req); let context = (self.context_factory)(req);
let variables = graphql.variables(); let variables = graphql.variables();
let result = execute(graphql.query(), let result = execute(graphql.query(),

View file

@ -3,9 +3,7 @@ use serde::ser::SerializeMap;
use std::fmt; use std::fmt;
use std::collections::HashMap; use std::collections::HashMap;
use ::{GraphQLError, Value}; use ::{GraphQLError, Value, Variables};
#[cfg(feature="iron-handlers")]
use ::Variables;
use ast::InputValue; use ast::InputValue;
use executor::ExecutionError; use executor::ExecutionError;
use parser::{ParseError, Spanning, SourcePosition}; 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. /// The expected structure of the decoded JSON Document for either Post or Get requests.
#[cfg(feature="iron-handlers")]
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GraphQlQuery { pub struct GraphQLQuery {
query: String, query: String,
#[serde(rename = "operationName")] #[serde(rename = "operationName")]
operation_name: Option<String>, operation_name: Option<String>,
variables: Option<InputValue> variables: Option<InputValue>
} }
#[cfg(feature="iron-handlers")] impl GraphQLQuery {
impl GraphQlQuery {
pub fn new(query: String, pub fn new(query: String,
operation_name: Option<String>, operation_name: Option<String>,
variables: Option<InputValue> variables: Option<InputValue>
) -> GraphQlQuery { ) -> GraphQLQuery {
GraphQlQuery { GraphQLQuery {
query: query, query: query,
operation_name: operation_name, operation_name: operation_name,
variables: variables variables: variables
@ -260,10 +255,8 @@ impl GraphQlQuery {
} }
#[cfg(feature="iron-handlers")]
pub struct WrappedGraphQLResult<'a>(Result<(Value, Vec<ExecutionError>), GraphQLError<'a>>); pub struct WrappedGraphQLResult<'a>(Result<(Value, Vec<ExecutionError>), GraphQLError<'a>>);
#[cfg(feature="iron-handlers")]
impl<'a> WrappedGraphQLResult<'a> { impl<'a> WrappedGraphQLResult<'a> {
pub fn new(result: Result<(Value, Vec<ExecutionError>), pub fn new(result: Result<(Value, Vec<ExecutionError>),
GraphQLError<'a>> GraphQLError<'a>>
@ -272,7 +265,6 @@ impl<'a> WrappedGraphQLResult<'a> {
} }
} }
#[cfg(feature="iron-handlers")]
impl<'a> ser::Serialize for WrappedGraphQLResult<'a> { impl<'a> ser::Serialize for WrappedGraphQLResult<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer, where S: ser::Serializer,

View file

@ -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 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. schema developer the control of the query execution path.
Juniper only depends on `serde` and `serde_json` by default, making it lightweight and Juniper only depends on `serde` and `serde_derive` by default, making it
easy to drop into any project. Through the `iron-handlers` feature, it also lightweight and easy to drop into any project. Through the `iron-handlers`
depends on Iron. feature, it also depends on Iron.
## Exposing data types ## Exposing data types
@ -190,8 +190,9 @@ built-in [GraphiQL][6] handler included.
#[cfg(feature="iron-handlers")] extern crate urlencoded; #[cfg(feature="iron-handlers")] extern crate urlencoded;
#[cfg(test)] extern crate iron_test; #[cfg(test)] extern crate iron_test;
extern crate serde; extern crate serde;
extern crate serde_json; #[macro_use] extern crate serde_derive;
#[cfg(feature="iron-handlers")] #[macro_use] extern crate serde_derive;
#[cfg(feature="iron-handlers")] extern crate serde_json;
#[macro_use] mod macros; #[macro_use] mod macros;
mod ast; mod ast;