Removed from_json, leveraged the already implemented serialization trait and added a serialization implementation for the graphql result.
This commit is contained in:
parent
6c4b329848
commit
e890e9b4bd
1 changed files with 82 additions and 29 deletions
|
@ -3,41 +3,13 @@ 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};
|
||||||
use ast::InputValue;
|
use ast::InputValue;
|
||||||
use executor::ExecutionError;
|
use executor::ExecutionError;
|
||||||
use parser::{ParseError, Spanning, SourcePosition};
|
use parser::{ParseError, Spanning, SourcePosition};
|
||||||
use validation::RuleError;
|
use validation::RuleError;
|
||||||
use serde_json::Value as Json;
|
|
||||||
|
|
||||||
|
|
||||||
impl InputValue {
|
|
||||||
/// Converts serde_json::Value to juniper::InputValue
|
|
||||||
pub fn from_json(json: Json) -> InputValue {
|
|
||||||
match json {
|
|
||||||
Json::Number(num) => {
|
|
||||||
if let Some(number) = num.as_i64() {
|
|
||||||
InputValue::int(number)
|
|
||||||
}
|
|
||||||
else if let Some(number) = num.as_f64() {
|
|
||||||
InputValue::float(number)
|
|
||||||
}
|
|
||||||
else if let Some(number) = num.as_u64() {
|
|
||||||
InputValue::float(number as f64)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
panic!("Invalid number data type was found.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Json::String(s) => InputValue::string(s),
|
|
||||||
Json::Bool(b) => InputValue::boolean(b),
|
|
||||||
Json::Array(a) => InputValue::list(a.into_iter().map(InputValue::from_json).collect()),
|
|
||||||
Json::Object(o) => InputValue::object(o.into_iter().map(|(k, v)| (k, InputValue::from_json(v))).collect()),
|
|
||||||
Json::Null => InputValue::null(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ser::Serialize for ExecutionError {
|
impl ser::Serialize for ExecutionError {
|
||||||
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,
|
||||||
|
@ -242,3 +214,84 @@ 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 {
|
||||||
|
query: String,
|
||||||
|
#[serde(rename = "operationName")]
|
||||||
|
operation_name: Option<String>,
|
||||||
|
variables: Option<InputValue>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GraphQlQuery {
|
||||||
|
|
||||||
|
pub fn new(query: String,
|
||||||
|
operation_name: Option<String>,
|
||||||
|
variables: Option<InputValue>
|
||||||
|
) -> GraphQlQuery {
|
||||||
|
GraphQlQuery {
|
||||||
|
query: query,
|
||||||
|
operation_name: operation_name,
|
||||||
|
variables: variables
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn query(&self) -> &str {
|
||||||
|
self.query.as_str()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn operation_name(&self) -> Option<&str> {
|
||||||
|
self.operation_name.as_ref().map(|oper_name| &**oper_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn variables(&self) -> Variables {
|
||||||
|
self.variables.as_ref().and_then(|iv| {
|
||||||
|
iv.to_object_value().map(|o| {
|
||||||
|
o.into_iter().map(|(k, v)| (k.to_owned(), v.clone())).collect()
|
||||||
|
})
|
||||||
|
}).unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(feature="iron-handlers")]
|
||||||
|
pub struct WrappedGraphQLResult<'a>(Result<(Value, Vec<ExecutionError>), GraphQLError<'a>>);
|
||||||
|
|
||||||
|
impl<'a> WrappedGraphQLResult<'a> {
|
||||||
|
pub fn new(result: Result<(Value, Vec<ExecutionError>),
|
||||||
|
GraphQLError<'a>>
|
||||||
|
) -> WrappedGraphQLResult<'a> {
|
||||||
|
WrappedGraphQLResult(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ser::Serialize for WrappedGraphQLResult<'a> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: ser::Serializer,
|
||||||
|
{
|
||||||
|
match self.0 {
|
||||||
|
Ok((ref res, ref err)) => {
|
||||||
|
let mut map = try!(serializer.serialize_map(None));
|
||||||
|
|
||||||
|
try!(map.serialize_key("data"));
|
||||||
|
try!(map.serialize_value(res));
|
||||||
|
|
||||||
|
if !err.is_empty() {
|
||||||
|
try!(map.serialize_key("errors"));
|
||||||
|
try!(map.serialize_value(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
map.end()
|
||||||
|
},
|
||||||
|
Err(ref err) => {
|
||||||
|
let mut map = try!(serializer.serialize_map(Some(1)));
|
||||||
|
try!(map.serialize_key("errors"));
|
||||||
|
try!(map.serialize_value(err));
|
||||||
|
map.end()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue