@@ -10,20 +10,11 @@ extern crate serde_derive;
1010extern crate serde;
1111extern 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;
1615use std:: path:: PathBuf ;
1716use 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 ) ]
2819enum 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