Skip to content

Commit ab629c3

Browse files
committed
Enable generating graphql clent code by cli
1 parent 68d10bd commit ab629c3

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

graphql_client_cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ structopt = "0.2"
1717
serde = "1.0"
1818
serde_derive = "1.0"
1919
serde_json = "1.0"
20+
graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.4.0" }

graphql_client_cli/src/main.rs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ extern crate reqwest;
44
extern crate structopt;
55
#[macro_use]
66
extern crate graphql_client;
7+
extern crate graphql_client_codegen;
78
#[macro_use]
89
extern crate serde_derive;
910
extern crate serde;
1011
extern crate serde_json;
1112

1213
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE, ACCEPT};
14+
use std::fs::File;
15+
use std::io::Write as IoWrite;
1316
use std::path::PathBuf;
1417
use structopt::StructOpt;
1518

@@ -38,9 +41,21 @@ enum Cli {
3841
#[structopt(name = "generate")]
3942
Generate {
4043
// should be a glob
41-
paths: String,
44+
/// Path to graphql query file
4245
#[structopt(parse(from_os_str))]
43-
schema: PathBuf,
46+
query_path: PathBuf,
47+
/// Path to graphql schema file
48+
#[structopt(parse(from_os_str))]
49+
schema_path: PathBuf,
50+
/// Name of struct
51+
selected_operation: String,
52+
/// Additional derives
53+
/// --additional-derives='Serialize,PartialEq'
54+
#[structopt(short = "a", long = "additional-derives")]
55+
additional_derives: Option<String>,
56+
/// allow, deny, or warn
57+
#[structopt(short = "d", long = "deprecation-strategy",)]
58+
deprecation_strategy: Option<String>,
4459
#[structopt(parse(from_os_str))]
4560
output: PathBuf,
4661
},
@@ -55,10 +70,20 @@ fn main() -> Result<(), failure::Error> {
5570
authorization,
5671
} => introspect_schema(schema_location, output, authorization),
5772
Cli::Generate {
58-
paths: _,
59-
schema: _,
60-
output: _,
61-
} => unimplemented!(),
73+
query_path,
74+
schema_path,
75+
selected_operation,
76+
additional_derives,
77+
deprecation_strategy,
78+
output,
79+
} => generate_code(
80+
query_path,
81+
schema_path,
82+
selected_operation,
83+
additional_derives,
84+
deprecation_strategy,
85+
output,
86+
),
6287
}
6388
}
6489

@@ -106,3 +131,34 @@ fn construct_headers() -> HeaderMap {
106131
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
107132
headers
108133
}
134+
135+
fn generate_code(
136+
query_path: PathBuf,
137+
schema_path: PathBuf,
138+
selected_operation: String,
139+
additional_derives: Option<String>,
140+
deprecation_strategy: Option<String>,
141+
output: PathBuf,
142+
) -> Result<(), failure::Error> {
143+
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
144+
let deprecation_strategy = match deprecation_strategy {
145+
Some("allow") => Some(graphql_client_codegen::deprecation::DeprecationStrategy::Allow),
146+
Some("deny") => Some(graphql_client_codegen::deprecation::DeprecationStrategy::Deny),
147+
Some("warn") => Some(graphql_client_codegen::deprecation::DeprecationStrategy::Warn),
148+
_ => None,
149+
};
150+
151+
let options = graphql_client_codegen::GraphQLClientDeriveOptions {
152+
selected_operation,
153+
additional_derives: additional_derives,
154+
deprecation_strategy,
155+
};
156+
let gen = graphql_client_codegen::generate_module_token_stream(
157+
query_path,
158+
schema_path,
159+
Some(options),
160+
)?;
161+
let mut file = File::create(output)?;
162+
write!(file, "{}", gen.to_string());
163+
Ok(())
164+
}

0 commit comments

Comments
 (0)