Skip to content

Commit 3405863

Browse files
authored
Merge pull request #354 from miterst/custom-scalars-module
Add an option to provide module for the custom scalar definitions
2 parents a69e720 + 91283ff commit 3405863

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) struct CliCodegenParams {
1717
pub no_formatting: bool,
1818
pub module_visibility: Option<String>,
1919
pub output_directory: Option<PathBuf>,
20+
pub custom_scalars_module: Option<String>,
2021
}
2122

2223
pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
@@ -30,6 +31,7 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
3031
query_path,
3132
schema_path,
3233
selected_operation,
34+
custom_scalars_module,
3335
} = params;
3436

3537
let deprecation_strategy = deprecation_strategy.as_ref().and_then(|s| s.parse().ok());
@@ -59,6 +61,17 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
5961
options.set_deprecation_strategy(deprecation_strategy);
6062
}
6163

64+
if let Some(custom_scalars_module) = custom_scalars_module {
65+
let custom_scalars_module = syn::parse_str(&custom_scalars_module).with_context(|| {
66+
format!(
67+
"Invalid custom scalars module path: {}",
68+
custom_scalars_module
69+
)
70+
})?;
71+
72+
options.set_custom_scalars_module(custom_scalars_module);
73+
}
74+
6275
let gen = generate_module_token_stream(query_path.clone(), &schema_path, options).unwrap();
6376

6477
let generated_code = gen.to_string();

graphql_client_cli/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ enum Cli {
7070
/// file, with the same name and the .rs extension.
7171
#[structopt(short = "o", long = "output-directory")]
7272
output_directory: Option<PathBuf>,
73+
/// The module where the custom scalar definitions are located.
74+
/// --custom-scalars-module='crate::gql::custom_scalars'
75+
#[structopt(short = "p", long = "custom-scalars-module")]
76+
custom_scalars_module: Option<String>,
7377
},
7478
}
7579

@@ -101,6 +105,7 @@ fn main() -> anyhow::Result<()> {
101105
query_path,
102106
schema_path,
103107
selected_operation,
108+
custom_scalars_module,
104109
} => generate::generate_code(generate::CliCodegenParams {
105110
variables_derives,
106111
response_derives,
@@ -111,6 +116,7 @@ fn main() -> anyhow::Result<()> {
111116
query_path,
112117
schema_path,
113118
selected_operation,
119+
custom_scalars_module,
114120
}),
115121
}
116122
}

graphql_client_codegen/src/codegen.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ fn generate_scalar_definitions<'a, 'schema: 'a>(
156156
proc_macro2::Span::call_site(),
157157
);
158158

159-
quote!(type #ident = super::#ident;)
159+
if let Some(custom_scalars_module) = options.custom_scalars_module() {
160+
quote!(type #ident = #custom_scalars_module::#ident;)
161+
} else {
162+
quote!(type #ident = super::#ident;)
163+
}
160164
})
161165
}
162166

graphql_client_codegen/src/codegen_options.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::deprecation::DeprecationStrategy;
22
use crate::normalization::Normalization;
33
use proc_macro2::Ident;
44
use std::path::{Path, PathBuf};
5-
use syn::Visibility;
5+
use syn::{self, Visibility};
66

77
/// Which context is this code generation effort taking place.
88
#[derive(Debug)]
@@ -39,6 +39,8 @@ pub struct GraphQLClientCodegenOptions {
3939
schema_file: Option<PathBuf>,
4040
/// Normalization pattern for query types and names.
4141
normalization: Normalization,
42+
/// Custom scalar definitions module path
43+
custom_scalars_module: Option<syn::Path>,
4244
}
4345

4446
impl GraphQLClientCodegenOptions {
@@ -56,6 +58,7 @@ impl GraphQLClientCodegenOptions {
5658
query_file: Default::default(),
5759
schema_file: Default::default(),
5860
normalization: Normalization::None,
61+
custom_scalars_module: Default::default(),
5962
}
6063
}
6164

@@ -174,4 +177,14 @@ impl GraphQLClientCodegenOptions {
174177
pub fn normalization(&self) -> &Normalization {
175178
&self.normalization
176179
}
180+
181+
/// Get the custom scalar definitions module
182+
pub fn custom_scalars_module(&self) -> Option<&syn::Path> {
183+
self.custom_scalars_module.as_ref()
184+
}
185+
186+
/// Set the custom scalar definitions module
187+
pub fn set_custom_scalars_module(&mut self, module: syn::Path) {
188+
self.custom_scalars_module = Some(module)
189+
}
177190
}

graphql_query_derive/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn build_graphql_client_derive_options(
7070
) -> Result<GraphQLClientCodegenOptions, BoxError> {
7171
let variables_derives = attributes::extract_attr(input, "variables_derives").ok();
7272
let response_derives = attributes::extract_attr(input, "response_derives").ok();
73+
let custom_scalars_module = attributes::extract_attr(input, "custom_scalars_module").ok();
7374

7475
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
7576
options.set_query_file(query_path);
@@ -92,6 +93,18 @@ fn build_graphql_client_derive_options(
9293
options.set_normalization(normalization);
9394
};
9495

96+
// The user can give a path to a module that provides definitions for the custom scalars.
97+
if let Some(custom_scalars_module) = custom_scalars_module {
98+
let custom_scalars_module = syn::parse_str(&custom_scalars_module).map_err(|err| {
99+
GeneralError(format!(
100+
"Invalid custom scalars module path: {}. {}",
101+
custom_scalars_module, err
102+
))
103+
})?;
104+
105+
options.set_custom_scalars_module(custom_scalars_module);
106+
}
107+
95108
options.set_struct_ident(input.ident.clone());
96109
options.set_module_visibility(input.vis.clone());
97110
options.set_operation_name(input.ident.to_string());

0 commit comments

Comments
 (0)