Skip to content

Commit 68d10bd

Browse files
committed
Change GraphQLClientDeriveOptions fields
1 parent 5b8e01e commit 68d10bd

File tree

2 files changed

+30
-11
lines changed
  • graphql_client_codegen/src
  • graphql_query_derive/src

2 files changed

+30
-11
lines changed

graphql_client_codegen/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ lazy_static! {
5555
CacheMap::default();
5656
}
5757

58-
pub struct GraphQLClientDeriveOptions<'a> {
59-
pub input: &'a syn::DeriveInput,
58+
pub struct GraphQLClientDeriveOptions {
59+
pub selected_operation: String,
60+
pub additional_derives: Option<String>,
61+
pub deprecation_strategy: Option<deprecation::DeprecationStrategy>,
6062
}
6163

6264
#[derive(Serialize, Deserialize, Debug)]
@@ -69,12 +71,13 @@ pub fn generate_module_token_stream(
6971
schema_path: std::path::PathBuf,
7072
options: Option<GraphQLClientDeriveOptions>,
7173
) -> Result<TokenStream, failure::Error> {
72-
let input = options.unwrap().input;
74+
let options = options.unwrap();
7375

74-
let response_derives = attributes::extract_attr(input, "response_derives").ok();
76+
let response_derives = options.additional_derives;
7577

7678
// The user can determine what to do about deprecations.
77-
let deprecation_strategy = deprecation::extract_deprecation_strategy(input)
79+
let deprecation_strategy = options
80+
.deprecation_strategy
7881
.unwrap_or(deprecation::DeprecationStrategy::Warn);
7982

8083
// We need to qualify the query with the path to the crate it is part of
@@ -122,13 +125,16 @@ pub fn generate_module_token_stream(
122125
}
123126
};
124127

125-
let operation_string = input.ident.to_string();
126-
let module_name = Ident::new(&operation_string.to_snake_case(), Span::call_site());
127-
let struct_name = &input.ident;
128+
let operation_string = options.selected_operation.clone();
129+
let module_name = Ident::new(
130+
options.selected_operation.to_snake_case().as_str(),
131+
Span::call_site(),
132+
);
133+
let struct_name = Ident::new(options.selected_operation.as_str(), Span::call_site());
128134
let schema_output = codegen::response_for_query(
129135
schema,
130136
query,
131-
input.ident.to_string(),
137+
options.selected_operation.clone(),
132138
response_derives,
133139
deprecation_strategy,
134140
)?;

graphql_query_derive/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::Token
1111
let input = TokenStream::from(input);
1212
let ast = syn::parse2(input).expect("Derive input is well formed");
1313
let (query_path, schema_path) = build_query_and_schema_path(&ast);
14-
let option = GraphQLClientDeriveOptions { input: &ast };
15-
let gen = generate_module_token_stream(query_path, schema_path, Some(option)).unwrap();
14+
let options = build_graphql_client_derive_options(&ast);
15+
let gen = generate_module_token_stream(query_path, schema_path, Some(options)).unwrap();
1616
gen.into()
1717
}
1818

@@ -29,3 +29,16 @@ fn build_query_and_schema_path(
2929
let schema_path = ::std::path::Path::new(&cargo_manifest_dir).join(schema_path);
3030
(query_path, schema_path)
3131
}
32+
33+
fn build_graphql_client_derive_options(input: &syn::DeriveInput) -> GraphQLClientDeriveOptions {
34+
let response_derives = attributes::extract_attr(input, "response_derives").ok();
35+
// The user can determine what to do about deprecations.
36+
let deprecation_strategy = deprecation::extract_deprecation_strategy(input)
37+
.unwrap_or(deprecation::DeprecationStrategy::Warn);
38+
39+
GraphQLClientDeriveOptions {
40+
selected_operation: input.ident.to_string(),
41+
additional_derives: response_derives,
42+
deprecation_strategy: Some(deprecation_strategy),
43+
}
44+
}

0 commit comments

Comments
 (0)