Skip to content

Commit cae30db

Browse files
committed
Generate code next to query files in CLI
my_query.graphql -> my_query.rs
1 parent f6f5b6c commit cae30db

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub(crate) struct CliCodegenParams {
1313
pub deprecation_strategy: Option<String>,
1414
pub no_formatting: bool,
1515
pub module_visibility: Option<String>,
16-
pub output: PathBuf,
1716
}
1817

1918
pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Error> {
@@ -24,7 +23,6 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err
2423

2524
let mut options = GraphQLClientCodegenOptions::new_default();
2625

27-
// options.set_module_name(module_name);
2826
options.set_module_visibility(
2927
syn::VisPublic {
3028
pub_token: <Token![pub]>::default(),
@@ -44,7 +42,8 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err
4442
options.set_deprecation_strategy(deprecation_strategy);
4543
}
4644

47-
let gen = generate_module_token_stream(params.query_path, &params.schema_path, options)?;
45+
let gen =
46+
generate_module_token_stream(params.query_path.clone(), &params.schema_path, options)?;
4847

4948
let generated_code = gen.to_string();
5049
let generated_code = if cfg!(feature = "rustfmt") && !params.no_formatting {
@@ -53,9 +52,13 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err
5352
generated_code
5453
};
5554

56-
let mut file = File::create(params.output)?;
57-
58-
write!(file, "{}", generated_code)?;
55+
let mut dest_path = params.query_path.clone();
56+
if dest_path.set_extension("rs") {
57+
let mut file = File::create(dest_path)?;
58+
write!(file, "{}", generated_code)?;
59+
} else {
60+
log::error!("Could not set the file extension on {:?}", dest_path);
61+
}
5962

6063
Ok(())
6164
}

graphql_client_cli/src/main.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ enum Cli {
3434
#[structopt(parse(from_os_str))]
3535
#[structopt(long = "output")]
3636
output: Option<PathBuf>,
37-
// Set authorizaiton header.
37+
/// Set the contents of the Authorizaiton header.
3838
#[structopt(long = "authorization")]
3939
authorization: Option<String>,
4040
},
@@ -67,8 +67,6 @@ enum Cli {
6767
/// Default value is pub.
6868
#[structopt(short = "m", long = "module_visibility")]
6969
module_visibility: Option<String>,
70-
#[structopt(parse(from_os_str))]
71-
output: PathBuf,
7270
},
7371
}
7472

@@ -90,7 +88,6 @@ fn main() -> Result<(), failure::Error> {
9088
deprecation_strategy,
9189
no_formatting,
9290
module_visibility,
93-
output,
9491
} => generate::generate_code(generate::CliCodegenParams {
9592
query_path,
9693
schema_path,
@@ -99,7 +96,6 @@ fn main() -> Result<(), failure::Error> {
9996
deprecation_strategy,
10097
no_formatting,
10198
module_visibility,
102-
output,
10399
}),
104100
}
105101
}

graphql_client_codegen/src/generated_module.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ impl<'a> GeneratedModule<'a> {
2626
let module_name = Ident::new(&self.operation.name.to_snake_case(), Span::call_site());
2727
let module_visibility = &self.options.module_visibility();
2828
let operation_name_literal = &self.operation.name;
29-
let operation_name_ident = self.options.struct_ident();
29+
let operation_name_ident = Ident::new(&self.operation.name, Span::call_site());
3030

3131
// Force cargo to refresh the generated code when the query file changes.
3232
let query_include = self
3333
.options
3434
.query_file()
3535
.map(|path| {
3636
let path = path.to_str();
37-
quote!(const __QUERY_WORKAROUND: &str = include_str!(#path))
37+
quote!(
38+
const __QUERY_WORKAROUND: &str = include_str!(#path);
39+
)
3840
})
3941
.unwrap_or_else(|| quote! {});
4042

@@ -52,7 +54,7 @@ impl<'a> GeneratedModule<'a> {
5254
pub const OPERATION_NAME: &'static str = #operation_name_literal;
5355
pub const QUERY: &'static str = #query_string;
5456

55-
#query_include;
57+
#query_include
5658

5759
#impls
5860

0 commit comments

Comments
 (0)