@@ -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,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