Skip to content

Commit ca7f805

Browse files
committed
Fix outdated example in README.md
1 parent 96e2f98 commit ca7f805

File tree

7 files changed

+54
-35
lines changed

7 files changed

+54
-35
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ A typed GraphQL client library for Rust.
6262
extern crate graphql_client;
6363
extern crate reqwest;
6464

65+
use graphql_client::{GraphQLQuery, GraphQLResponse};
66+
6567
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
6668

6769
// this is the important line
68-
let request_body = MyQuery::expand(variables);
70+
let request_body = MyQuery::build_query(variables);
6971

7072
let client = reqwest::Client::new();
7173
let mut res = client.post("/graphql").json(&request_body).send()?;

graphql_query_derive/src/codegen.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ pub(crate) fn response_for_query(
132132
#variables_struct
133133

134134
#response_derives
135-
#[serde(rename_all = "camelCase")]
136135
pub struct ResponseData {
137136
#(#response_data_fields,)*
138137
}

graphql_query_derive/src/inputs.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ impl GqlInput {
2222
fields.sort_unstable_by(|a, b| a.name.cmp(&b.name));
2323
let fields = fields.iter().map(|field| {
2424
let ty = field.type_.to_rust(&context, "");
25-
let name = Ident::new(&field.name.to_snake_case(), Span::call_site());
26-
quote!(pub #name: #ty)
25+
let original_name = &field.name;
26+
let snake_case_name = field.name.to_snake_case();
27+
let rename = if snake_case_name.as_str() != original_name.as_str() {
28+
quote!(#[serde(rename = #original_name)])
29+
} else {
30+
quote!()
31+
};
32+
let name = Ident::new(&snake_case_name, Span::call_site());
33+
34+
quote!(#rename pub #name: #ty)
2735
});
2836

2937
Ok(quote! {
3038
#[derive(Debug, Serialize)]
31-
#[serde(rename_all = "camelCase")]
3239
pub struct #name {
3340
#(#fields,)*
3441
}
@@ -52,7 +59,8 @@ impl ::std::convert::From<graphql_parser::schema::InputObjectType> for GqlInput
5259
type_: field.value_type.into(),
5360
};
5461
(name, field)
55-
}).collect(),
62+
})
63+
.collect(),
5664
}
5765
}
5866
}
@@ -79,7 +87,8 @@ impl ::std::convert::From<introspection_response::FullType> for GqlInput {
7987
.into(),
8088
};
8189
(name, field)
82-
}).collect(),
90+
})
91+
.collect(),
8392
}
8493
}
8594
}
@@ -127,19 +136,19 @@ mod tests {
127136
},
128137
),
129138
].into_iter()
130-
.collect(),
139+
.collect(),
131140
};
132141

133142
let expected: String = vec![
134143
"# [ derive ( Debug , Serialize ) ] ",
135-
"# [ serde ( rename_all = \"camelCase\" ) ] ",
136144
"pub struct Cat { ",
137145
"pub offsprings : Vec < Cat > , ",
146+
"# [ serde ( rename = \"pawsCount\" ) ] ",
138147
"pub paws_count : Float , ",
139148
"pub requirements : Option < CatRequirements > , ",
140149
"}",
141150
].into_iter()
142-
.collect();
151+
.collect();
143152

144153
let mut context = QueryContext::new_empty();
145154
context.schema.inputs.insert(cat.name.clone(), cat);

graphql_query_derive/src/objects.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ impl GqlObject {
7676
#(#field_impls)*
7777

7878
#derives
79-
#[serde(rename_all = "camelCase")]
8079
#description
8180
pub struct #name {
8281
#(#fields,)*

graphql_query_derive/src/operations.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ impl Operation {
5050
let fields = variables.iter().map(|variable| {
5151
let name = &variable.name;
5252
let ty = variable.ty.to_rust(context, "");
53-
let name = Ident::new(&name.to_snake_case(), Span::call_site());
54-
quote!(pub #name: #ty)
53+
let snake_case_name = name.to_snake_case();
54+
let rename = if snake_case_name.as_str() != name.as_str() {
55+
quote!(#[serde(rename = #name)])
56+
} else {
57+
quote!()
58+
};
59+
let name = Ident::new(&snake_case_name, Span::call_site());
60+
quote!(#rename pub #name: #ty)
5561
});
5662

5763
let default_constructors = variables
@@ -60,7 +66,6 @@ impl Operation {
6066

6167
quote! {
6268
#[derive(Serialize)]
63-
#[serde(rename_all = "camelCase")]
6469
pub struct Variables {
6570
#(#fields,)*
6671
}

graphql_query_derive/src/shared.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ pub(crate) fn render_object_field(
2020
};
2121
}
2222

23-
let name_ident = Ident::new(&field_name.to_snake_case(), Span::call_site());
23+
let snake_case_name = field_name.to_snake_case();
2424

25-
quote!(#description pub #name_ident: #field_type)
25+
let rename = if snake_case_name != field_name {
26+
quote!(#[serde(rename = #field_name)])
27+
} else {
28+
quote!()
29+
};
30+
31+
let name_ident = Ident::new(&snake_case_name, Span::call_site());
32+
33+
quote!(#description #rename pub #name_ident: #field_type)
2634
}
2735

2836
pub(crate) fn field_impls_for_selection(
@@ -51,7 +59,8 @@ pub(crate) fn field_impls_for_selection(
5159
} else {
5260
Ok(quote!())
5361
}
54-
}).collect()
62+
})
63+
.collect()
5564
}
5665

5766
pub(crate) fn response_fields_for_selection(
@@ -94,5 +103,6 @@ pub(crate) fn response_fields_for_selection(
94103
SelectionItem::InlineFragment(_) => {
95104
Err(format_err!("inline fragment on object field"))?
96105
}
97-
}).collect()
106+
})
107+
.collect()
98108
}

graphql_query_derive/src/unions.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ pub struct GqlUnion {
1616
enum UnionError {
1717
#[fail(display = "Unknown type: {}", ty)]
1818
UnknownType { ty: String },
19-
#[fail(
20-
display = "Missing __typename in selection for {}",
21-
union_name
22-
)]
19+
#[fail(display = "Missing __typename in selection for {}", union_name)]
2320
MissingTypename { union_name: String },
2421
}
2522

@@ -150,7 +147,7 @@ mod tests {
150147
SelectionItem::InlineFragment(SelectionInlineFragment {
151148
on: "User".to_string(),
152149
fields: Selection(vec![SelectionItem::Field(SelectionField {
153-
name: "first_name".to_string(),
150+
name: "firstName".to_string(),
154151
fields: Selection(vec![]),
155152
})]),
156153
}),
@@ -178,17 +175,17 @@ mod tests {
178175
fields: vec![
179176
GqlObjectField {
180177
description: None,
181-
name: "first_name".to_string(),
178+
name: "firstName".to_string(),
182179
type_: FieldType::Named(Ident::new("String", Span::call_site())),
183180
},
184181
GqlObjectField {
185182
description: None,
186-
name: "last_name".to_string(),
183+
name: "lastName".to_string(),
187184
type_: FieldType::Named(Ident::new("String", Span::call_site())),
188185
},
189186
GqlObjectField {
190187
description: None,
191-
name: "created_at".to_string(),
188+
name: "createdAt".to_string(),
192189
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
193190
},
194191
],
@@ -235,7 +232,7 @@ mod tests {
235232
SelectionItem::InlineFragment(SelectionInlineFragment {
236233
on: "User".to_string(),
237234
fields: Selection(vec![SelectionItem::Field(SelectionField {
238-
name: "first_name".to_string(),
235+
name: "firstName".to_string(),
239236
fields: Selection(vec![]),
240237
})]),
241238
}),
@@ -272,17 +269,17 @@ mod tests {
272269
},
273270
GqlObjectField {
274271
description: None,
275-
name: "first_name".to_string(),
272+
name: "firstName".to_string(),
276273
type_: FieldType::Named(string_type()),
277274
},
278275
GqlObjectField {
279276
description: None,
280-
name: "last_name".to_string(),
277+
name: "lastName".to_string(),
281278
type_: FieldType::Named(string_type()),
282279
},
283280
GqlObjectField {
284281
description: None,
285-
name: "created_at".to_string(),
282+
name: "createdAt".to_string(),
286283
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
287284
},
288285
],
@@ -307,7 +304,7 @@ mod tests {
307304
},
308305
GqlObjectField {
309306
description: None,
310-
name: "created_at".to_string(),
307+
name: "createdAt".to_string(),
311308
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
312309
},
313310
],
@@ -324,16 +321,14 @@ mod tests {
324321
result.unwrap().to_string(),
325322
vec![
326323
"# [ derive ( Deserialize ) ] ",
327-
"# [ serde ( rename_all = \"camelCase\" ) ] ",
328-
"pub struct MeowOnUser { pub first_name : String , } ",
324+
"pub struct MeowOnUser { # [ serde ( rename = \"firstName\" ) ] pub first_name : String , } ",
329325
"# [ derive ( Deserialize ) ] ",
330-
"# [ serde ( rename_all = \"camelCase\" ) ] ",
331326
"pub struct MeowOnOrganization { pub title : String , } ",
332327
"# [ derive ( Deserialize ) ] ",
333328
"# [ serde ( tag = \"__typename\" ) ] ",
334329
"pub enum Meow { User ( MeowOnUser ) , Organization ( MeowOnOrganization ) }",
335330
].into_iter()
336-
.collect::<String>(),
331+
.collect::<String>(),
337332
);
338333
}
339334
}

0 commit comments

Comments
 (0)