@@ -10,20 +10,11 @@ extern crate serde_derive;
10
10
extern crate serde;
11
11
extern crate serde_json;
12
12
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;
16
15
use std:: path:: PathBuf ;
17
16
use structopt:: StructOpt ;
18
17
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
-
27
18
#[ derive( StructOpt ) ]
28
19
enum Cli {
29
20
#[ structopt( name = "introspect-schema" ) ]
@@ -69,15 +60,15 @@ fn main() -> Result<(), failure::Error> {
69
60
schema_location,
70
61
output,
71
62
authorization,
72
- } => introspect_schema ( schema_location, output, authorization) ,
63
+ } => introspect_schema:: introspect_schema ( & schema_location, output, authorization) ,
73
64
Cli :: Generate {
74
65
query_path,
75
66
schema_path,
76
67
selected_operation,
77
68
additional_derives,
78
69
deprecation_strategy,
79
70
output,
80
- } => generate_code (
71
+ } => generate :: generate_code (
81
72
query_path,
82
73
schema_path,
83
74
selected_operation,
@@ -87,79 +78,3 @@ fn main() -> Result<(), failure::Error> {
87
78
) ,
88
79
}
89
80
}
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