Skip to content

Commit 55fdc16

Browse files
authored
Merge pull request #223 from graphql-rust/cargo-recompile-on-query-change
Include query file in generated modules
2 parents 79d757c + dd6a96a commit 55fdc16

File tree

10 files changed

+256
-143
lines changed

10 files changed

+256
-143
lines changed

CHANGELOG.md

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

88
## Unreleased
99

10+
### Fixed
11+
12+
- Changes to query files will now always trigger code generation for the corresponding modules on the next build.
13+
1014
## 0.6.1 - 2019-01-19
1115

1216
### Added

graphql_client/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl Display for PathFragment {
154154
/// },
155155
/// {
156156
/// "message": "Seismic activity detected",
157-
/// "path": ["undeground", 20]
157+
/// "path": ["underground", 20]
158158
/// },
159159
/// ],
160160
/// }))?;
@@ -177,7 +177,7 @@ impl Display for PathFragment {
177177
/// message: "Seismic activity detected".to_owned(),
178178
/// locations: None,
179179
/// path: Some(vec![
180-
/// PathFragment::Key("undeground".into()),
180+
/// PathFragment::Key("underground".into()),
181181
/// PathFragment::Index(20),
182182
/// ]),
183183
/// extensions: None,

graphql_client_cli/src/generate.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use failure;
2-
use graphql_client_codegen::*;
2+
use graphql_client_codegen::{
3+
deprecation, generate_module_token_stream, GraphQLClientCodegenOptions,
4+
};
35
use std::fs::File;
46
use std::io::Write as IoWrite;
57
use std::path::{Path, PathBuf};
@@ -12,20 +14,18 @@ pub fn generate_code(
1214
module_name: String,
1315
selected_operation: Option<String>,
1416
additional_derives: Option<String>,
15-
deprecation_strategy: &Option<String>,
17+
deprecation_strategy: Option<&str>,
1618
no_formatting: bool,
17-
module_visibility: &Option<String>,
18-
output: &PathBuf,
19+
module_visibility: Option<&str>,
20+
output: &Path,
1921
) -> Result<(), failure::Error> {
20-
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
2122
let deprecation_strategy = match deprecation_strategy {
2223
Some("allow") => Some(deprecation::DeprecationStrategy::Allow),
2324
Some("deny") => Some(deprecation::DeprecationStrategy::Deny),
2425
Some("warn") => Some(deprecation::DeprecationStrategy::Warn),
2526
_ => None,
2627
};
2728

28-
let module_visibility = module_visibility.as_ref().map(|s| s.as_str());
2929
let module_visibility = match module_visibility {
3030
Some("pub") => syn::VisPublic {
3131
pub_token: <Token![pub]>::default(),
@@ -38,28 +38,36 @@ pub fn generate_code(
3838
.into(),
3939
};
4040

41-
let options = GraphQLClientDeriveOptions {
42-
operation_name: selected_operation,
43-
struct_name: None,
44-
module_name: Some(module_name),
45-
additional_derives,
46-
deprecation_strategy,
47-
module_visibility,
48-
};
41+
let mut options = GraphQLClientCodegenOptions::new_default();
4942

50-
let gen = generate_module_token_stream(query_path, &schema_path, Some(options))?;
43+
options.set_module_name(module_name);
44+
options.set_module_visibility(module_visibility);
5145

52-
let mut file = File::create(output.clone())?;
46+
if let Some(selected_operation) = selected_operation {
47+
options.set_operation_name(selected_operation);
48+
}
5349

54-
let codes = gen.to_string();
50+
if let Some(additional_derives) = additional_derives {
51+
options.set_additional_derives(additional_derives);
52+
}
5553

56-
if cfg!(feature = "rustfmt") && !no_formatting {
57-
let codes = format(&codes);
58-
write!(file, "{}", codes)?;
59-
} else {
60-
write!(file, "{}", codes)?;
54+
if let Some(deprecation_strategy) = deprecation_strategy {
55+
options.set_deprecation_strategy(deprecation_strategy);
6156
}
6257

58+
let gen = generate_module_token_stream(query_path, &schema_path, options)?;
59+
60+
let generated_code = gen.to_string();
61+
let generated_code = if cfg!(feature = "rustfmt") && !no_formatting {
62+
format(&generated_code)
63+
} else {
64+
generated_code
65+
};
66+
67+
let mut file = File::create(output)?;
68+
69+
write!(file, "{}", generated_code)?;
70+
6371
Ok(())
6472
}
6573

graphql_client_cli/src/introspect_schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn introspect_schema(
4242
let mut res = req_builder.json(&request_body).send()?;
4343

4444
if res.status().is_success() {
45+
// do nothing
4546
} else if res.status().is_server_error() {
4647
println!("server error!");
4748
} else {

graphql_client_cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ fn main() -> Result<(), failure::Error> {
100100
module_name,
101101
selected_operation,
102102
additional_derives,
103-
&deprecation_strategy,
103+
deprecation_strategy.as_ref().map(String::as_str),
104104
no_formatting,
105-
&module_visibility,
105+
module_visibility.as_ref().map(String::as_str),
106106
&output,
107107
),
108108
}

graphql_client_codegen/src/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) fn response_for_query(
3838
schema: &schema::Schema,
3939
query: &query::Document,
4040
operation: &Operation,
41-
additional_derives: Option<String>,
41+
additional_derives: Option<&str>,
4242
deprecation_strategy: deprecation::DeprecationStrategy,
4343
multiple_operation: bool,
4444
) -> Result<TokenStream, failure::Error> {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use crate::deprecation::DeprecationStrategy;
2+
use heck::SnakeCase;
3+
use proc_macro2::{Ident, Span};
4+
use std::path::{Path, PathBuf};
5+
use syn::Visibility;
6+
7+
/// Used to configure code generation.
8+
#[derive(Debug, Default)]
9+
pub struct GraphQLClientCodegenOptions {
10+
/// Name of the operation we want to generate code for. If it does not match, we use all queries.
11+
pub operation_name: Option<String>,
12+
/// The name of implemention target struct.
13+
pub struct_name: Option<String>,
14+
/// The name of the module that will contains queries.
15+
pub module_name: Option<String>,
16+
/// Comma-separated list of additional traits we want to derive.
17+
additional_derives: Option<String>,
18+
/// The deprecation strategy to adopt.
19+
deprecation_strategy: Option<DeprecationStrategy>,
20+
/// Target module visibility.
21+
module_visibility: Option<Visibility>,
22+
/// A path to a file to include in the module to force Cargo to take into account changes in
23+
/// the query files when recompiling.
24+
query_file: Option<PathBuf>,
25+
/// A path to a file to include in the module to force Cargo to take into account changes in
26+
/// the schema files when recompiling.
27+
schema_file: Option<PathBuf>,
28+
}
29+
30+
impl GraphQLClientCodegenOptions {
31+
/// Creates an empty options object with default params. It probably wants to be configured.
32+
pub fn new_default() -> GraphQLClientCodegenOptions {
33+
std::default::Default::default()
34+
}
35+
36+
/// The module name, either one that was set explicitly, or the operation name, as snake case.
37+
pub(crate) fn module_name_ident(&self) -> Option<Ident> {
38+
self.module_name
39+
.as_ref()
40+
.or_else(|| self.operation_name.as_ref())
41+
.map(|s| s.to_snake_case())
42+
.map(|module_name| Ident::new(&module_name, Span::call_site()))
43+
}
44+
45+
/// The visibility (public/private) to apply to the target module.
46+
pub(crate) fn module_visibility(&self) -> &Visibility {
47+
self.module_visibility
48+
.as_ref()
49+
.unwrap_or(&Visibility::Inherited)
50+
}
51+
52+
/// The deprecation strategy to adopt.
53+
pub(crate) fn deprecation_strategy(&self) -> DeprecationStrategy {
54+
self.deprecation_strategy.clone().unwrap_or_default()
55+
}
56+
57+
/// A path to a file to include in the module to force Cargo to take into account changes in
58+
/// the query files when recompiling.
59+
pub fn set_query_file(&mut self, path: PathBuf) {
60+
self.query_file = Some(path);
61+
}
62+
63+
/// Comma-separated list of additional traits we want to derive.
64+
pub fn additional_derives(&self) -> Option<&str> {
65+
self.additional_derives.as_ref().map(String::as_str)
66+
}
67+
68+
/// Comma-separated list of additional traits we want to derive.
69+
pub fn set_additional_derives(&mut self, additional_derives: String) {
70+
self.additional_derives = Some(additional_derives);
71+
}
72+
73+
/// The deprecation strategy to adopt.
74+
pub fn set_deprecation_strategy(&mut self, deprecation_strategy: DeprecationStrategy) {
75+
self.deprecation_strategy = Some(deprecation_strategy);
76+
}
77+
78+
/// The name of the module that will contains queries.
79+
pub fn set_module_name(&mut self, module_name: String) {
80+
self.module_name = Some(module_name);
81+
}
82+
83+
/// Target module visibility.
84+
pub fn set_module_visibility(&mut self, visibility: Visibility) {
85+
self.module_visibility = Some(visibility);
86+
}
87+
88+
/// The name of implemention target struct.
89+
pub fn set_struct_name(&mut self, struct_name: String) {
90+
self.struct_name = Some(struct_name);
91+
}
92+
93+
/// Name of the operation we want to generate code for. If none is selected, it means all
94+
/// operations.
95+
pub fn set_operation_name(&mut self, operation_name: String) {
96+
self.operation_name = Some(operation_name);
97+
}
98+
99+
/// A path to a file to include in the module to force Cargo to take into account changes in
100+
/// the schema files when recompiling.
101+
pub fn schema_file(&self) -> Option<&Path> {
102+
self.schema_file.as_ref().map(PathBuf::as_path)
103+
}
104+
105+
/// A path to a file to include in the module to force Cargo to take into account changes in
106+
/// the query files when recompiling.
107+
pub fn query_file(&self) -> Option<&Path> {
108+
self.query_file.as_ref().map(PathBuf::as_path)
109+
}
110+
}

graphql_client_codegen/src/deprecation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub enum DeprecationStatus {
77
Deprecated(Option<String>),
88
}
99

10-
/// The available deprecation startegies.
10+
/// The available deprecation strategies.
1111
#[derive(Debug, PartialEq, Clone)]
1212
pub enum DeprecationStrategy {
1313
/// Allow use of deprecated items in queries, and say nothing.

0 commit comments

Comments
 (0)