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"]
[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}

View file

@ -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<GraphQlQuery> {
fn handle_get(&self, req: &mut Request) -> IronResult<GraphQLQuery> {
match req.get_mut::<UrlEncodedQuery>() {
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<GraphQlQuery> {
fn handle_post(&self, req: &mut Request) -> IronResult<GraphQLQuery> {
let mut request_payload = String::new();
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(
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<Response> {
fn respond(&self, req: &mut Request, graphql: GraphQLQuery) -> IronResult<Response> {
let context = (self.context_factory)(req);
let variables = graphql.variables();
let result = execute(graphql.query(),

View file

@ -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<String>,
variables: Option<InputValue>
}
#[cfg(feature="iron-handlers")]
impl GraphQlQuery {
impl GraphQLQuery {
pub fn new(query: String,
operation_name: Option<String>,
variables: Option<InputValue>
) -> 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<ExecutionError>), GraphQLError<'a>>);
#[cfg(feature="iron-handlers")]
impl<'a> WrappedGraphQLResult<'a> {
pub fn new(result: Result<(Value, Vec<ExecutionError>),
GraphQLError<'a>>
@ -272,7 +265,6 @@ impl<'a> WrappedGraphQLResult<'a> {
}
}
#[cfg(feature="iron-handlers")]
impl<'a> ser::Serialize for WrappedGraphQLResult<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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
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;