Skip to content

Commit 02d1a68

Browse files
authored
Merge pull request #135 from h-michael/refactor
Refactoring graphql_client_cli
2 parents 5783848 + 4d95424 commit 02d1a68

File tree

5 files changed

+96
-89
lines changed

5 files changed

+96
-89
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use failure;
2+
use graphql_client_codegen::*;
3+
use std::fs::File;
4+
use std::io::Write as IoWrite;
5+
use std::path::PathBuf;
6+
7+
pub fn generate_code(
8+
query_path: PathBuf,
9+
schema_path: PathBuf,
10+
selected_operation: String,
11+
additional_derives: Option<String>,
12+
deprecation_strategy: Option<String>,
13+
output: PathBuf,
14+
) -> Result<(), failure::Error> {
15+
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
16+
let deprecation_strategy = match deprecation_strategy {
17+
Some("allow") => Some(deprecation::DeprecationStrategy::Allow),
18+
Some("deny") => Some(deprecation::DeprecationStrategy::Deny),
19+
Some("warn") => Some(deprecation::DeprecationStrategy::Warn),
20+
_ => None,
21+
};
22+
23+
let options = GraphQLClientDeriveOptions {
24+
selected_operation,
25+
additional_derives,
26+
deprecation_strategy,
27+
};
28+
let gen = generate_module_token_stream(query_path, schema_path, Some(options))?;
29+
let mut file = File::create(output)?;
30+
write!(file, "{}", gen.to_string());
31+
Ok(())
32+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use failure;
2+
use graphql_client;
3+
use reqwest;
4+
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE};
5+
use serde_json;
6+
use std::path::PathBuf;
7+
8+
#[derive(GraphQLQuery)]
9+
#[graphql(
10+
schema_path = "src/graphql/introspection_schema.graphql",
11+
query_path = "src/graphql/introspection_query.graphql",
12+
response_derives = "Serialize"
13+
)]
14+
#[allow(dead_code)]
15+
struct IntrospectionQuery;
16+
17+
pub fn introspect_schema(
18+
location: &str,
19+
output: Option<PathBuf>,
20+
authorization: Option<String>,
21+
) -> Result<(), failure::Error> {
22+
use std::io::Write;
23+
24+
let out: Box<Write> = match output {
25+
Some(path) => Box::new(::std::fs::File::create(path)?),
26+
None => Box::new(::std::io::stdout()),
27+
};
28+
29+
let request_body: graphql_client::QueryBody<()> = graphql_client::QueryBody {
30+
variables: (),
31+
query: introspection_query::QUERY,
32+
operation_name: introspection_query::OPERATION_NAME,
33+
};
34+
35+
let client = reqwest::Client::new();
36+
37+
let mut req_builder = client.post(location).headers(construct_headers());
38+
if let Some(token) = authorization {
39+
req_builder = req_builder.bearer_auth(token.as_str());
40+
};
41+
42+
let mut res = req_builder.json(&request_body).send()?;
43+
44+
if res.status().is_success() {
45+
} else if res.status().is_server_error() {
46+
println!("server error!");
47+
} else {
48+
println!("Something else happened. Status: {:?}", res.status());
49+
}
50+
51+
let json: serde_json::Value = res.json()?;
52+
Ok(serde_json::to_writer_pretty(out, &json)?)
53+
}
54+
55+
fn construct_headers() -> HeaderMap {
56+
let mut headers = HeaderMap::new();
57+
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
58+
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
59+
headers
60+
}

graphql_client_cli/src/main.rs

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,11 @@ extern crate serde_derive;
1010
extern crate serde;
1111
extern crate serde_json;
1212

13-
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE, ACCEPT};
14-
use std::fs::File;
15-
use std::io::Write as IoWrite;
13+
mod generate;
14+
mod introspect_schema;
1615
use std::path::PathBuf;
1716
use structopt::StructOpt;
1817

19-
#[derive(GraphQLQuery)]
20-
#[graphql(
21-
schema_path = "src/introspection_schema.graphql",
22-
query_path = "src/introspection_query.graphql",
23-
response_derives = "Serialize"
24-
)]
25-
struct IntrospectionQuery;
26-
2718
#[derive(StructOpt)]
2819
enum Cli {
2920
#[structopt(name = "introspect-schema")]
@@ -69,15 +60,15 @@ fn main() -> Result<(), failure::Error> {
6960
schema_location,
7061
output,
7162
authorization,
72-
} => introspect_schema(schema_location, output, authorization),
63+
} => introspect_schema::introspect_schema(&schema_location, output, authorization),
7364
Cli::Generate {
7465
query_path,
7566
schema_path,
7667
selected_operation,
7768
additional_derives,
7869
deprecation_strategy,
7970
output,
80-
} => generate_code(
71+
} => generate::generate_code(
8172
query_path,
8273
schema_path,
8374
selected_operation,
@@ -87,79 +78,3 @@ fn main() -> Result<(), failure::Error> {
8778
),
8879
}
8980
}
90-
91-
fn introspect_schema(
92-
location: String,
93-
output: Option<PathBuf>,
94-
authorization: Option<String>,
95-
) -> Result<(), failure::Error> {
96-
use std::io::Write;
97-
98-
let out: Box<Write> = match output {
99-
Some(path) => Box::new(::std::fs::File::create(path)?),
100-
None => Box::new(::std::io::stdout()),
101-
};
102-
103-
let request_body: graphql_client::QueryBody<()> = graphql_client::QueryBody {
104-
variables: (),
105-
query: introspection_query::QUERY,
106-
operation_name: introspection_query::OPERATION_NAME,
107-
};
108-
109-
let client = reqwest::Client::new();
110-
111-
let mut req_builder = client.post(&location).headers(construct_headers());
112-
if let Some(token) = authorization {
113-
req_builder = req_builder.bearer_auth(token.as_str());
114-
};
115-
116-
let mut res = req_builder.json(&request_body).send()?;
117-
118-
if res.status().is_success() {
119-
} else if res.status().is_server_error() {
120-
println!("server error!");
121-
} else {
122-
println!("Something else happened. Status: {:?}", res.status());
123-
}
124-
125-
let json: serde_json::Value = res.json()?;
126-
Ok(serde_json::to_writer_pretty(out, &json)?)
127-
}
128-
129-
fn construct_headers() -> HeaderMap {
130-
let mut headers = HeaderMap::new();
131-
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
132-
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
133-
headers
134-
}
135-
136-
fn generate_code(
137-
query_path: PathBuf,
138-
schema_path: PathBuf,
139-
selected_operation: String,
140-
additional_derives: Option<String>,
141-
deprecation_strategy: Option<String>,
142-
output: PathBuf,
143-
) -> Result<(), failure::Error> {
144-
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
145-
let deprecation_strategy = match deprecation_strategy {
146-
Some("allow") => Some(graphql_client_codegen::deprecation::DeprecationStrategy::Allow),
147-
Some("deny") => Some(graphql_client_codegen::deprecation::DeprecationStrategy::Deny),
148-
Some("warn") => Some(graphql_client_codegen::deprecation::DeprecationStrategy::Warn),
149-
_ => None,
150-
};
151-
152-
let options = graphql_client_codegen::GraphQLClientDeriveOptions {
153-
selected_operation,
154-
additional_derives: additional_derives,
155-
deprecation_strategy,
156-
};
157-
let gen = graphql_client_codegen::generate_module_token_stream(
158-
query_path,
159-
schema_path,
160-
Some(options),
161-
)?;
162-
let mut file = File::create(output)?;
163-
write!(file, "{}", gen.to_string());
164-
Ok(())
165-
}

0 commit comments

Comments
 (0)