@@ -4,12 +4,15 @@ extern crate reqwest;
44extern crate structopt;
55#[ macro_use]
66extern crate graphql_client;
7+ extern crate graphql_client_codegen;
78#[ macro_use]
89extern crate serde_derive;
910extern crate serde;
1011extern crate serde_json;
1112
1213use reqwest:: header:: { HeaderMap , HeaderValue , CONTENT_TYPE , ACCEPT } ;
14+ use std:: fs:: File ;
15+ use std:: io:: Write as IoWrite ;
1316use std:: path:: PathBuf ;
1417use structopt:: StructOpt ;
1518
@@ -38,9 +41,22 @@ 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 that is implementation target.
51+ selected_operation : String ,
52+ /// Additional derives that will be added to the generated structs and enums for the response and the variables.
53+ /// --additional-derives='Serialize,PartialEq'
54+ #[ structopt( short = "a" , long = "additional-derives" ) ]
55+ additional_derives : Option < String > ,
56+ /// You can choose deprecation strategy from allow, deny, or warn.
57+ /// Default value is warn.
58+ #[ structopt( short = "d" , long = "deprecation-strategy" , ) ]
59+ deprecation_strategy : Option < String > ,
4460 #[ structopt( parse( from_os_str) ) ]
4561 output : PathBuf ,
4662 } ,
@@ -55,10 +71,20 @@ fn main() -> Result<(), failure::Error> {
5571 authorization,
5672 } => introspect_schema ( schema_location, output, authorization) ,
5773 Cli :: Generate {
58- paths : _,
59- schema : _,
60- output : _,
61- } => unimplemented ! ( ) ,
74+ query_path,
75+ schema_path,
76+ selected_operation,
77+ additional_derives,
78+ deprecation_strategy,
79+ output,
80+ } => generate_code (
81+ query_path,
82+ schema_path,
83+ selected_operation,
84+ additional_derives,
85+ deprecation_strategy,
86+ output,
87+ ) ,
6288 }
6389}
6490
@@ -106,3 +132,34 @@ fn construct_headers() -> HeaderMap {
106132 headers. insert ( ACCEPT , HeaderValue :: from_static ( "application/json" ) ) ;
107133 headers
108134}
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