Skip to content

Commit e06150f

Browse files
committed
graphql_query_derive: support deriving for input variables as well
This just hijacks the `reponse_derives` attribute right now since it controls derives for generated data structures and `Variables` shares much of that. Fixes #103.
1 parent 3a6ca10 commit e06150f

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4242

4343
- Aliases in queries are now supported.
4444

45+
- The traits in `response_derives` are now used for input types as well.
46+
4547
### Fixed
4648

4749
- Handle all Rust keywords as field names in codegen by appending `_` to the generated names, so a field called `type` in a GraphQL query will become a `type_` field in the generated struct. Thanks to @scrogson!

graphql_query_derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ proc-macro = true
1111

1212
[dependencies]
1313
failure = "0.1"
14+
itertools = "0.7"
1415
lazy_static = "1.0"
1516
quote = "^0.6"
1617
syn = "0.14"

graphql_query_derive/src/inputs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ impl GqlInput {
3030

3131
quote!(#rename pub #name: #ty)
3232
});
33+
let variables_derives = context.variables_derives();
3334

3435
Ok(quote! {
35-
#[derive(Debug, Serialize)]
36+
#variables_derives
3637
pub struct #name {
3738
#(#fields,)*
3839
}
@@ -134,7 +135,7 @@ mod tests {
134135
};
135136

136137
let expected: String = vec![
137-
"# [ derive ( Debug , Serialize ) ] ",
138+
"# [ derive ( Serialize , Clone ) ] ",
138139
"pub struct Cat { ",
139140
"pub offsprings : Vec < Cat > , ",
140141
"# [ serde ( rename = \"pawsCount\" ) ] ",
@@ -146,6 +147,7 @@ mod tests {
146147

147148
let mut context = QueryContext::new_empty();
148149
context.schema.inputs.insert(cat.name.clone(), cat);
150+
context.ingest_additional_derives("Clone").unwrap();
149151

150152
assert_eq!(
151153
format!(

graphql_query_derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
extern crate failure;
55
extern crate graphql_parser;
66
extern crate heck;
7+
extern crate itertools;
78
#[macro_use]
89
extern crate lazy_static;
910
extern crate proc_macro;

graphql_query_derive/src/operations.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ impl Operation {
4242
pub(crate) fn expand_variables(&self, context: &QueryContext) -> TokenStream {
4343
let variables = &self.variables;
4444

45+
let variables_derives = context.variables_derives();
46+
4547
if variables.is_empty() {
46-
return quote!(#[derive(Serialize)]
48+
return quote!(#variables_derives
4749
pub struct Variables;);
4850
}
4951

@@ -62,7 +64,7 @@ impl Operation {
6264
.map(|variable| variable.generate_default_value_constructor(context));
6365

6466
quote! {
65-
#[derive(Serialize)]
67+
#variables_derives
6668
pub struct Variables {
6769
#(#fields,)*
6870
}

graphql_query_derive/src/query.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use deprecation::DeprecationStrategy;
22
use failure;
33
use fragments::GqlFragment;
4+
use itertools::Itertools;
45
use operations::Operation;
56
use proc_macro2::Span;
67
use proc_macro2::TokenStream;
@@ -15,6 +16,7 @@ pub(crate) struct QueryContext {
1516
pub schema: Schema,
1617
pub selected_operation: Option<Operation>,
1718
pub deprecation_strategy: DeprecationStrategy,
19+
variables_derives: Vec<Ident>,
1820
response_derives: Vec<Ident>,
1921
}
2022

@@ -26,6 +28,7 @@ impl QueryContext {
2628
schema,
2729
selected_operation: None,
2830
deprecation_strategy,
31+
variables_derives: vec![Ident::new("Serialize", Span::call_site())],
2932
response_derives: vec![Ident::new("Deserialize", Span::call_site())],
3033
}
3134
}
@@ -38,6 +41,7 @@ impl QueryContext {
3841
schema: Schema::new(),
3942
selected_operation: None,
4043
deprecation_strategy: DeprecationStrategy::Allow,
44+
variables_derives: vec![Ident::new("Serialize", Span::call_site())],
4145
response_derives: vec![Ident::new("Deserialize", Span::call_site())],
4246
}
4347
}
@@ -71,6 +75,12 @@ impl QueryContext {
7175
));
7276
}
7377

78+
self.variables_derives.extend(
79+
attribute_value
80+
.split(',')
81+
.map(|s| s.trim())
82+
.map(|s| Ident::new(s, Span::call_site())),
83+
);
7484
self.response_derives.extend(
7585
attribute_value
7686
.split(',')
@@ -80,8 +90,16 @@ impl QueryContext {
8090
Ok(())
8191
}
8292

93+
pub(crate) fn variables_derives(&self) -> TokenStream {
94+
let derives = self.variables_derives.iter().unique();
95+
96+
quote! {
97+
#[derive( #(#derives),* )]
98+
}
99+
}
100+
83101
pub(crate) fn response_derives(&self) -> TokenStream {
84-
let derives = &self.response_derives;
102+
let derives = self.response_derives.iter().unique();
85103

86104
quote! {
87105
#[derive( #(#derives),* )]

0 commit comments

Comments
 (0)