Skip to content

Commit

Permalink
Preserve the order of requested fields
Browse files Browse the repository at this point in the history
Fixes #82
  • Loading branch information
LegNeato authored and mhallin committed Sep 2, 2017
1 parent 0372de8 commit 5d43532
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 60 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ The repository was restructured to a multi crate workspace to enable several new

### New features

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

## Breaking changes

* To better comply with the specification, order of requested fields is
now preserved.
([#82](https://github.com/graphql-rust/juniper/issues/82)

## [0.8.1] – 2017-06-15

Tiny release to fix broken crate metadata on crates.io.
Expand Down
3 changes: 2 additions & 1 deletion juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ expose-test-schema = []
default = ["uuid"]

[dependencies]
ordermap = { version = "^0.2.11", features = ["serde-1"] }
serde = { version = "^1.0.8" }
serde_derive = {version="^1.0.8" }
serde_json = { version="^1.0.2", optional = true }
uuid = { version = "0.5.1", optional = true }

[dev-dependencies]
bencher = "^0.1.2"
serde_json = { version = "^1.0.2" }
serde_json = { version = "^1.0.2" }
9 changes: 5 additions & 4 deletions juniper/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fmt;
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::Hash;
use std::vec;
use std::slice;

use ordermap::OrderMap;

use executor::Variables;
use parser::Spanning;

Expand Down Expand Up @@ -258,7 +259,7 @@ impl InputValue {
///
/// Similar to `InputValue::list`, it makes each key and value in the given
/// hash map not contain any location information.
pub fn object<K>(o: HashMap<K, InputValue>) -> InputValue
pub fn object<K>(o: OrderMap<K, InputValue>) -> InputValue
where
K: AsRef<str> + Eq + Hash,
{
Expand Down Expand Up @@ -347,9 +348,9 @@ impl InputValue {

/// Convert the input value to an unlocated object value.
///
/// This constructs a new hashmap that contain references to the keys
/// This constructs a new OrderMap that contain references to the keys
/// and values in `self`.
pub fn to_object_value(&self) -> Option<HashMap<&str, &InputValue>> {
pub fn to_object_value(&self) -> Option<OrderMap<&str, &InputValue>> {
match *self {
InputValue::Object(ref o) => Some(
o.iter()
Expand Down
6 changes: 3 additions & 3 deletions juniper/src/executor_tests/directives.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use value::Value;
use executor::Variables;
Expand All @@ -19,7 +19,7 @@ graphql_object!(TestType: () |&self| {

fn run_variable_query<F>(query: &str, vars: Variables, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
let schema = RootNode::new(TestType, EmptyMutation::<()>::new());

Expand All @@ -36,7 +36,7 @@ where

fn run_query<F>(query: &str, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
run_variable_query(query, Variables::new(), f);
}
Expand Down
6 changes: 3 additions & 3 deletions juniper/src/executor_tests/enums.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use value::Value;
use ast::InputValue;
Expand Down Expand Up @@ -35,7 +35,7 @@ graphql_object!(TestType: () |&self| {

fn run_variable_query<F>(query: &str, vars: Variables, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
let schema = RootNode::new(TestType, EmptyMutation::<()>::new());

Expand All @@ -52,7 +52,7 @@ where

fn run_query<F>(query: &str, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
run_variable_query(query, Variables::new(), f);
}
Expand Down
8 changes: 4 additions & 4 deletions juniper/src/executor_tests/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ mod merge_parallel_fragments {
Value::object(vec![
("a", Value::string("Apple")),
("b", Value::string("Banana")),
("c", Value::string("Cherry")),
("deep", Value::object(vec![
("b", Value::string("Banana")),
("c", Value::string("Cherry")),
("deeper", Value::object(vec![
("b", Value::string("Banana")),
("c", Value::string("Cherry")),
].into_iter().collect())),
("c", Value::string("Cherry")),
].into_iter().collect())),
("c", Value::string("Cherry")),
].into_iter().collect()));
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ mod threads_context_correctly {
}

mod dynamic_context_switching {
use std::collections::HashMap;
use ordermap::OrderMap;

use value::Value;
use types::scalars::EmptyMutation;
Expand All @@ -224,7 +224,7 @@ mod dynamic_context_switching {
}

struct OuterContext {
items: HashMap<i32, InnerContext>,
items: OrderMap<i32, InnerContext>,
}

impl Context for OuterContext {}
Expand Down
6 changes: 3 additions & 3 deletions juniper/src/executor_tests/variables.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use value::Value;
use ast::InputValue;
Expand Down Expand Up @@ -120,7 +120,7 @@ graphql_object!(TestType: () |&self| {

fn run_variable_query<F>(query: &str, vars: Variables, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
let schema = RootNode::new(TestType, EmptyMutation::<()>::new());

Expand All @@ -137,7 +137,7 @@ where

fn run_query<F>(query: &str, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
run_variable_query(query, Variables::new(), f);
}
Expand Down
10 changes: 5 additions & 5 deletions juniper/src/integrations/serde.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use ordermap::OrderMap;
use serde::{de, ser};
use serde::ser::SerializeMap;

use std::fmt;
use std::collections::HashMap;

use {GraphQLError, Value};
use ast::InputValue;
use executor::ExecutionError;
use parser::{ParseError, SourcePosition, Spanning};
use validation::RuleError;


impl ser::Serialize for ExecutionError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'de> de::Deserialize<'de> for InputValue {
where
V: de::MapAccess<'de>,
{
let mut values: HashMap<String, InputValue> = HashMap::new();
let mut values: OrderMap<String, InputValue> = OrderMap::new();

while let Some((key, value)) = try!(visitor.next_entry()) {
values.insert(key, value);
Expand Down Expand Up @@ -161,7 +161,7 @@ impl ser::Serialize for InputValue {
.serialize(serializer),
InputValue::Object(ref v) => v.iter()
.map(|&(ref k, ref v)| (k.item.clone(), v.item.clone()))
.collect::<HashMap<_, _>>()
.collect::<OrderMap<_, _>>()
.serialize(serializer),
}
}
Expand Down Expand Up @@ -214,7 +214,7 @@ impl<'a> ser::Serialize for Spanning<ParseError<'a>> {
try!(map.serialize_key("message"));
try!(map.serialize_value(&message));

let mut location = HashMap::new();
let mut location = OrderMap::new();
location.insert("line".to_owned(), self.start.line() + 1);
location.insert("column".to_owned(), self.start.column() + 1);

Expand Down
1 change: 1 addition & 0 deletions juniper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ extern crate serde_derive;
#[cfg(any(test, feature = "expose-test-schema"))]
extern crate serde_json;

extern crate ordermap;

#[cfg(any(test, feature = "uuid"))]
extern crate uuid;
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/enums.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use executor::Variables;
use value::Value;
Expand Down Expand Up @@ -88,7 +88,7 @@ graphql_object!(Root: () |&self| {

fn run_type_info_query<F>(doc: &str, f: F)
where
F: Fn((&HashMap<String, Value>, &Vec<Value>)) -> (),
F: Fn((&OrderMap<String, Value>, &Vec<Value>)) -> (),
{
let schema = RootNode::new(Root {}, EmptyMutation::<()>::new());

Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use value::Value;
use ast::InputValue;
Expand Down Expand Up @@ -59,7 +59,7 @@ graphql_interface!(Interface: () |&self| {

fn run_field_info_query<F>(type_name: &str, field_name: &str, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
let doc = r#"
query ($typeName: String!) {
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/input_object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use ast::{FromInputValue, InputValue};
use executor::Variables;
Expand Down Expand Up @@ -105,7 +105,7 @@ graphql_object!(Root: () |&self| {

fn run_type_info_query<F>(doc: &str, f: F)
where
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
{
let schema = RootNode::new(Root {}, EmptyMutation::<()>::new());

Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;
use std::marker::PhantomData;

use ast::InputValue;
Expand Down Expand Up @@ -135,7 +135,7 @@ graphql_object!(<'a> Root: () as "Root" |&self| {

fn run_type_info_query<F>(type_name: &str, f: F)
where
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
{
let doc = r#"
query ($typeName: String!) {
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;
use std::marker::PhantomData;

use ast::InputValue;
Expand Down Expand Up @@ -119,7 +119,7 @@ graphql_object!(<'a> Root: () as "Root" |&self| {

fn run_type_info_query<F>(type_name: &str, f: F)
where
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
{
let doc = r#"
query ($typeName: String!) {
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use executor::Variables;
use value::Value;
Expand Down Expand Up @@ -72,7 +72,7 @@ graphql_object!(Root: () |&self| {

fn run_type_info_query<F>(doc: &str, f: F)
where
F: Fn(&HashMap<String, Value>) -> (),
F: Fn(&OrderMap<String, Value>) -> (),
{
let schema = RootNode::new(Root {}, EmptyMutation::<()>::new());

Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/union.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use ordermap::OrderMap;
use std::marker::PhantomData;

use ast::InputValue;
Expand Down Expand Up @@ -112,7 +112,7 @@ graphql_object!(<'a> Root: () as "Root" |&self| {

fn run_type_info_query<F>(type_name: &str, f: F)
where
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
{
let doc = r#"
query ($typeName: String!) {
Expand Down
5 changes: 3 additions & 2 deletions juniper/src/parser/tests/value.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::HashMap;
use ordermap::OrderMap;

use ast::InputValue;
use parser::{Lexer, Parser, SourcePosition, Spanning};
use parser::value::parse_value_literal;


fn parse_value(s: &str) -> Spanning<InputValue> {
let mut lexer = Lexer::new(s);
let mut parser = Parser::new(&mut lexer).expect(&format!("Lexer error on input {:#?}", s));
Expand Down Expand Up @@ -112,7 +113,7 @@ fn input_value_literals() {
Spanning::start_end(
&SourcePosition::new(0, 0, 0),
&SourcePosition::new(2, 0, 2),
InputValue::object(HashMap::<String, InputValue>::new())
InputValue::object(OrderMap::<String, InputValue>::new())
)
);
assert_eq!(
Expand Down
Loading

0 comments on commit 5d43532

Please sign in to comment.