Skip to content

Commit

Permalink
Merge pull request #354 from miterst/custom-scalars-module
Browse files Browse the repository at this point in the history
Add an option to provide module for the custom scalar definitions
  • Loading branch information
tomhoule authored Feb 13, 2021
2 parents a69e720 + 91283ff commit 3405863
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
13 changes: 13 additions & 0 deletions graphql_client_cli/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) struct CliCodegenParams {
pub no_formatting: bool,
pub module_visibility: Option<String>,
pub output_directory: Option<PathBuf>,
pub custom_scalars_module: Option<String>,
}

pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
Expand All @@ -30,6 +31,7 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
query_path,
schema_path,
selected_operation,
custom_scalars_module,
} = params;

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

if let Some(custom_scalars_module) = custom_scalars_module {
let custom_scalars_module = syn::parse_str(&custom_scalars_module).with_context(|| {
format!(
"Invalid custom scalars module path: {}",
custom_scalars_module
)
})?;

options.set_custom_scalars_module(custom_scalars_module);
}

let gen = generate_module_token_stream(query_path.clone(), &schema_path, options).unwrap();

let generated_code = gen.to_string();
Expand Down
6 changes: 6 additions & 0 deletions graphql_client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ enum Cli {
/// file, with the same name and the .rs extension.
#[structopt(short = "o", long = "output-directory")]
output_directory: Option<PathBuf>,
/// The module where the custom scalar definitions are located.
/// --custom-scalars-module='crate::gql::custom_scalars'
#[structopt(short = "p", long = "custom-scalars-module")]
custom_scalars_module: Option<String>,
},
}

Expand Down Expand Up @@ -101,6 +105,7 @@ fn main() -> anyhow::Result<()> {
query_path,
schema_path,
selected_operation,
custom_scalars_module,
} => generate::generate_code(generate::CliCodegenParams {
variables_derives,
response_derives,
Expand All @@ -111,6 +116,7 @@ fn main() -> anyhow::Result<()> {
query_path,
schema_path,
selected_operation,
custom_scalars_module,
}),
}
}
Expand Down
6 changes: 5 additions & 1 deletion graphql_client_codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ fn generate_scalar_definitions<'a, 'schema: 'a>(
proc_macro2::Span::call_site(),
);

quote!(type #ident = super::#ident;)
if let Some(custom_scalars_module) = options.custom_scalars_module() {
quote!(type #ident = #custom_scalars_module::#ident;)
} else {
quote!(type #ident = super::#ident;)
}
})
}

Expand Down
15 changes: 14 additions & 1 deletion graphql_client_codegen/src/codegen_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::deprecation::DeprecationStrategy;
use crate::normalization::Normalization;
use proc_macro2::Ident;
use std::path::{Path, PathBuf};
use syn::Visibility;
use syn::{self, Visibility};

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

impl GraphQLClientCodegenOptions {
Expand All @@ -56,6 +58,7 @@ impl GraphQLClientCodegenOptions {
query_file: Default::default(),
schema_file: Default::default(),
normalization: Normalization::None,
custom_scalars_module: Default::default(),
}
}

Expand Down Expand Up @@ -174,4 +177,14 @@ impl GraphQLClientCodegenOptions {
pub fn normalization(&self) -> &Normalization {
&self.normalization
}

/// Get the custom scalar definitions module
pub fn custom_scalars_module(&self) -> Option<&syn::Path> {
self.custom_scalars_module.as_ref()
}

/// Set the custom scalar definitions module
pub fn set_custom_scalars_module(&mut self, module: syn::Path) {
self.custom_scalars_module = Some(module)
}
}
13 changes: 13 additions & 0 deletions graphql_query_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn build_graphql_client_derive_options(
) -> Result<GraphQLClientCodegenOptions, BoxError> {
let variables_derives = attributes::extract_attr(input, "variables_derives").ok();
let response_derives = attributes::extract_attr(input, "response_derives").ok();
let custom_scalars_module = attributes::extract_attr(input, "custom_scalars_module").ok();

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

// The user can give a path to a module that provides definitions for the custom scalars.
if let Some(custom_scalars_module) = custom_scalars_module {
let custom_scalars_module = syn::parse_str(&custom_scalars_module).map_err(|err| {
GeneralError(format!(
"Invalid custom scalars module path: {}. {}",
custom_scalars_module, err
))
})?;

options.set_custom_scalars_module(custom_scalars_module);
}

options.set_struct_ident(input.ident.clone());
options.set_module_visibility(input.vis.clone());
options.set_operation_name(input.ident.to_string());
Expand Down

0 comments on commit 3405863

Please sign in to comment.