Skip to content

Commit 5d43532

Browse files
LegNeatomhallin
authored andcommitted
Preserve the order of requested fields
Fixes #82
1 parent 0372de8 commit 5d43532

File tree

21 files changed

+134
-60
lines changed

21 files changed

+134
-60
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ The repository was restructured to a multi crate workspace to enable several new
77

88
### New features
99

10-
* New juniper_codegen crate which provides custom derives:
10+
* New juniper_codegen crate which provides custom derives:
1111
* `#[derive(GraphQLInputObject)]`
1212
* `#[derive(GraphQLEnum)]`
1313
* `#[derive(GraphQLObject)]`
1414

15+
## Breaking changes
16+
17+
* To better comply with the specification, order of requested fields is
18+
now preserved.
19+
([#82](https://github.com/graphql-rust/juniper/issues/82)
20+
1521
## [0.8.1] – 2017-06-15
1622

1723
Tiny release to fix broken crate metadata on crates.io.

juniper/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ expose-test-schema = []
2525
default = ["uuid"]
2626

2727
[dependencies]
28+
ordermap = { version = "^0.2.11", features = ["serde-1"] }
2829
serde = { version = "^1.0.8" }
2930
serde_derive = {version="^1.0.8" }
3031
serde_json = { version="^1.0.2", optional = true }
3132
uuid = { version = "0.5.1", optional = true }
3233

3334
[dev-dependencies]
3435
bencher = "^0.1.2"
35-
serde_json = { version = "^1.0.2" }
36+
serde_json = { version = "^1.0.2" }

juniper/src/ast.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::fmt;
22
use std::borrow::Cow;
3-
use std::collections::HashMap;
43
use std::hash::Hash;
54
use std::vec;
65
use std::slice;
76

7+
use ordermap::OrderMap;
8+
89
use executor::Variables;
910
use parser::Spanning;
1011

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

348349
/// Convert the input value to an unlocated object value.
349350
///
350-
/// This constructs a new hashmap that contain references to the keys
351+
/// This constructs a new OrderMap that contain references to the keys
351352
/// and values in `self`.
352-
pub fn to_object_value(&self) -> Option<HashMap<&str, &InputValue>> {
353+
pub fn to_object_value(&self) -> Option<OrderMap<&str, &InputValue>> {
353354
match *self {
354355
InputValue::Object(ref o) => Some(
355356
o.iter()

juniper/src/executor_tests/directives.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

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

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

@@ -36,7 +36,7 @@ where
3636

3737
fn run_query<F>(query: &str, f: F)
3838
where
39-
F: Fn(&HashMap<String, Value>) -> (),
39+
F: Fn(&OrderMap<String, Value>) -> (),
4040
{
4141
run_variable_query(query, Variables::new(), f);
4242
}

juniper/src/executor_tests/enums.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

33
use value::Value;
44
use ast::InputValue;
@@ -35,7 +35,7 @@ graphql_object!(TestType: () |&self| {
3535

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

@@ -52,7 +52,7 @@ where
5252

5353
fn run_query<F>(query: &str, f: F)
5454
where
55-
F: Fn(&HashMap<String, Value>) -> (),
55+
F: Fn(&OrderMap<String, Value>) -> (),
5656
{
5757
run_variable_query(query, Variables::new(), f);
5858
}

juniper/src/executor_tests/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ mod merge_parallel_fragments {
148148
Value::object(vec![
149149
("a", Value::string("Apple")),
150150
("b", Value::string("Banana")),
151-
("c", Value::string("Cherry")),
152151
("deep", Value::object(vec![
153152
("b", Value::string("Banana")),
154-
("c", Value::string("Cherry")),
155153
("deeper", Value::object(vec![
156154
("b", Value::string("Banana")),
157155
("c", Value::string("Cherry")),
158156
].into_iter().collect())),
157+
("c", Value::string("Cherry")),
159158
].into_iter().collect())),
159+
("c", Value::string("Cherry")),
160160
].into_iter().collect()));
161161
}
162162
}
@@ -209,7 +209,7 @@ mod threads_context_correctly {
209209
}
210210

211211
mod dynamic_context_switching {
212-
use std::collections::HashMap;
212+
use ordermap::OrderMap;
213213

214214
use value::Value;
215215
use types::scalars::EmptyMutation;
@@ -224,7 +224,7 @@ mod dynamic_context_switching {
224224
}
225225

226226
struct OuterContext {
227-
items: HashMap<i32, InnerContext>,
227+
items: OrderMap<i32, InnerContext>,
228228
}
229229

230230
impl Context for OuterContext {}

juniper/src/executor_tests/variables.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

33
use value::Value;
44
use ast::InputValue;
@@ -120,7 +120,7 @@ graphql_object!(TestType: () |&self| {
120120

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

@@ -137,7 +137,7 @@ where
137137

138138
fn run_query<F>(query: &str, f: F)
139139
where
140-
F: Fn(&HashMap<String, Value>) -> (),
140+
F: Fn(&OrderMap<String, Value>) -> (),
141141
{
142142
run_variable_query(query, Variables::new(), f);
143143
}

juniper/src/integrations/serde.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
use ordermap::OrderMap;
12
use serde::{de, ser};
23
use serde::ser::SerializeMap;
4+
35
use std::fmt;
4-
use std::collections::HashMap;
56

67
use {GraphQLError, Value};
78
use ast::InputValue;
89
use executor::ExecutionError;
910
use parser::{ParseError, SourcePosition, Spanning};
1011
use validation::RuleError;
1112

12-
1313
impl ser::Serialize for ExecutionError {
1414
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1515
where
@@ -130,7 +130,7 @@ impl<'de> de::Deserialize<'de> for InputValue {
130130
where
131131
V: de::MapAccess<'de>,
132132
{
133-
let mut values: HashMap<String, InputValue> = HashMap::new();
133+
let mut values: OrderMap<String, InputValue> = OrderMap::new();
134134

135135
while let Some((key, value)) = try!(visitor.next_entry()) {
136136
values.insert(key, value);
@@ -161,7 +161,7 @@ impl ser::Serialize for InputValue {
161161
.serialize(serializer),
162162
InputValue::Object(ref v) => v.iter()
163163
.map(|&(ref k, ref v)| (k.item.clone(), v.item.clone()))
164-
.collect::<HashMap<_, _>>()
164+
.collect::<OrderMap<_, _>>()
165165
.serialize(serializer),
166166
}
167167
}
@@ -214,7 +214,7 @@ impl<'a> ser::Serialize for Spanning<ParseError<'a>> {
214214
try!(map.serialize_key("message"));
215215
try!(map.serialize_value(&message));
216216

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

juniper/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ extern crate serde_derive;
121121
#[cfg(any(test, feature = "expose-test-schema"))]
122122
extern crate serde_json;
123123

124+
extern crate ordermap;
124125

125126
#[cfg(any(test, feature = "uuid"))]
126127
extern crate uuid;

juniper/src/macros/tests/enums.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

33
use executor::Variables;
44
use value::Value;
@@ -88,7 +88,7 @@ graphql_object!(Root: () |&self| {
8888

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

juniper/src/macros/tests/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

33
use value::Value;
44
use ast::InputValue;
@@ -59,7 +59,7 @@ graphql_interface!(Interface: () |&self| {
5959

6060
fn run_field_info_query<F>(type_name: &str, field_name: &str, f: F)
6161
where
62-
F: Fn(&HashMap<String, Value>) -> (),
62+
F: Fn(&OrderMap<String, Value>) -> (),
6363
{
6464
let doc = r#"
6565
query ($typeName: String!) {

juniper/src/macros/tests/input_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

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

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

juniper/src/macros/tests/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22
use std::marker::PhantomData;
33

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

136136
fn run_type_info_query<F>(type_name: &str, f: F)
137137
where
138-
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
138+
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
139139
{
140140
let doc = r#"
141141
query ($typeName: String!) {

juniper/src/macros/tests/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22
use std::marker::PhantomData;
33

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

120120
fn run_type_info_query<F>(type_name: &str, f: F)
121121
where
122-
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
122+
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
123123
{
124124
let doc = r#"
125125
query ($typeName: String!) {

juniper/src/macros/tests/scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

33
use executor::Variables;
44
use value::Value;
@@ -72,7 +72,7 @@ graphql_object!(Root: () |&self| {
7272

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

juniper/src/macros/tests/union.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22
use std::marker::PhantomData;
33

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

113113
fn run_type_info_query<F>(type_name: &str, f: F)
114114
where
115-
F: Fn(&HashMap<String, Value>, &Vec<Value>) -> (),
115+
F: Fn(&OrderMap<String, Value>, &Vec<Value>) -> (),
116116
{
117117
let doc = r#"
118118
query ($typeName: String!) {

juniper/src/parser/tests/value.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use std::collections::HashMap;
1+
use ordermap::OrderMap;
22

33
use ast::InputValue;
44
use parser::{Lexer, Parser, SourcePosition, Spanning};
55
use parser::value::parse_value_literal;
66

7+
78
fn parse_value(s: &str) -> Spanning<InputValue> {
89
let mut lexer = Lexer::new(s);
910
let mut parser = Parser::new(&mut lexer).expect(&format!("Lexer error on input {:#?}", s));
@@ -112,7 +113,7 @@ fn input_value_literals() {
112113
Spanning::start_end(
113114
&SourcePosition::new(0, 0, 0),
114115
&SourcePosition::new(2, 0, 2),
115-
InputValue::object(HashMap::<String, InputValue>::new())
116+
InputValue::object(OrderMap::<String, InputValue>::new())
116117
)
117118
);
118119
assert_eq!(

0 commit comments

Comments
 (0)