@@ -4,12 +4,15 @@ extern crate reqwest;
4
4
extern crate structopt;
5
5
#[ macro_use]
6
6
extern crate graphql_client;
7
+ extern crate graphql_client_codegen;
7
8
#[ macro_use]
8
9
extern crate serde_derive;
9
10
extern crate serde;
10
11
extern crate serde_json;
11
12
12
13
use reqwest:: header:: { HeaderMap , HeaderValue , CONTENT_TYPE , ACCEPT } ;
14
+ use std:: fs:: File ;
15
+ use std:: io:: Write as IoWrite ;
13
16
use std:: path:: PathBuf ;
14
17
use structopt:: StructOpt ;
15
18
@@ -38,9 +41,22 @@ enum Cli {
38
41
#[ structopt( name = "generate" ) ]
39
42
Generate {
40
43
// should be a glob
41
- paths : String ,
44
+ /// Path to graphql query file.
42
45
#[ 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 > ,
44
60
#[ structopt( parse( from_os_str) ) ]
45
61
output : PathBuf ,
46
62
} ,
@@ -55,10 +71,20 @@ fn main() -> Result<(), failure::Error> {
55
71
authorization,
56
72
} => introspect_schema ( schema_location, output, authorization) ,
57
73
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
+ ) ,
62
88
}
63
89
}
64
90
@@ -106,3 +132,34 @@ fn construct_headers() -> HeaderMap {
106
132
headers. insert ( ACCEPT , HeaderValue :: from_static ( "application/json" ) ) ;
107
133
headers
108
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