1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#![warn(missing_docs)]
use artemis_codegen::{
deprecation::DeprecationStrategy, generate_module_token_stream, generate_root_token_stream,
CodegenError, CodegenMode, GraphQLClientCodegenOptions
};
use std::{
env,
error::Error,
fmt,
fs::File,
io::Write,
path::{Path, PathBuf}
};
use syn::Token;
#[cfg(feature = "introspect")]
mod introspect;
#[cfg(feature = "introspect")]
pub use introspect::IntrospectionError;
#[derive(Debug)]
pub enum BuildError {
ArgumentError(String),
CodegenError(CodegenError),
IoError(std::io::Error)
}
impl Error for BuildError {}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BuildError::ArgumentError(msg) => write!(f, "error parsing arguments: {}", msg),
BuildError::CodegenError(e) => write!(f, "error generating code: {}", e),
BuildError::IoError(e) => write!(f, "io error: {}", e)
}
}
}
impl From<CodegenError> for BuildError {
fn from(e: CodegenError) -> Self {
BuildError::CodegenError(e)
}
}
impl From<std::io::Error> for BuildError {
fn from(e: std::io::Error) -> Self {
BuildError::IoError(e)
}
}
#[derive(Debug, Default)]
pub struct CodegenBuilder {
query_paths: Vec<PathBuf>,
variable_derives: Option<String>,
response_derives: Option<String>,
deprecation_strategy: Option<DeprecationStrategy>,
output_directory: Option<PathBuf>,
schema_path: Option<PathBuf>
}
impl CodegenBuilder {
pub fn new() -> Self {
Self {
query_paths: Vec::new(),
variable_derives: None,
response_derives: None,
deprecation_strategy: None,
output_directory: None,
schema_path: None
}
}
pub fn add_query<T: AsRef<Path>>(mut self, query_path: T) -> Self {
self.query_paths.push(query_path.as_ref().to_path_buf());
self
}
pub fn with_derives_on_variables<T: Into<String>>(mut self, derives: T) -> Self {
self.variable_derives = Some(derives.into());
self
}
pub fn with_derives_on_response<T: Into<String>>(mut self, derives: T) -> Self {
self.response_derives = Some(derives.into());
self
}
pub fn with_deprecation_strategy(mut self, strategy: DeprecationStrategy) -> Self {
self.deprecation_strategy = Some(strategy);
self
}
pub fn with_out_dir<T: AsRef<Path>>(mut self, out_dir: T) -> Self {
self.output_directory = Some(out_dir.as_ref().to_path_buf());
self
}
pub fn with_schema<T: AsRef<Path>>(mut self, schema_path: T) -> Self {
self.schema_path = Some(schema_path.as_ref().to_path_buf());
self
}
#[cfg(feature = "introspect")]
pub fn introspect_schema<T: AsRef<str>>(
mut self,
schema_url: T,
authorization: Option<String>,
extra_headers: Vec<introspect::Header>
) -> Result<Self, IntrospectionError> {
let schema_path =
introspect::introspect(schema_url.as_ref(), authorization, extra_headers)?;
self.schema_path = Some(schema_path);
Ok(self)
}
pub fn build(self) -> Result<(), BuildError> {
if self.schema_path.is_none() {
let msg = if cfg!(feature = "introspect") {
"Missing schema path. Either use 'with_schema' to specify a file or 'introspect schema' to introspect a remote server."
} else {
r#"
Missing schema path. Please use 'with_schema' to specify a file.
Alternatively, enable the 'introspect' feature and use 'introspect_schema' to automatically introspect the schema from a remote server.
"#
};
return Err(BuildError::ArgumentError(msg.to_string()));
}
let schema_path = self.schema_path.unwrap();
let output_directory: PathBuf = self
.output_directory
.map(Ok)
.unwrap_or_else(|| env::var("OUT_DIR").map(Into::into))
.map_err(|_| {
BuildError::ArgumentError(
"Missing out dir. Either set 'OUT_DIR' or use 'with_out_dir'.".to_string()
)
})?;
let params = CodegenParams {
schema_path,
selected_operation: None,
variables_derives: self.variable_derives.clone(),
response_derives: self.response_derives.clone(),
deprecation_strategy: self.deprecation_strategy.clone(),
output_directory
};
generate_code(self.query_paths, params)?;
Ok(())
}
}
#[derive(Debug)]
pub(crate) struct CodegenParams {
pub schema_path: PathBuf,
pub selected_operation: Option<String>,
pub variables_derives: Option<String>,
pub response_derives: Option<String>,
pub deprecation_strategy: Option<DeprecationStrategy>,
pub output_directory: PathBuf
}
pub(crate) fn generate_code(
query_paths: Vec<PathBuf>,
params: CodegenParams
) -> Result<(), BuildError> {
let CodegenParams {
variables_derives,
response_derives,
deprecation_strategy,
output_directory,
schema_path,
selected_operation
} = params;
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Cli);
options.set_module_visibility(
syn::VisPublic {
pub_token: <Token![pub]>::default()
}
.into()
);
if let Some(selected_operation) = selected_operation {
options.set_operation_name(selected_operation);
}
if let Some(variables_derives) = variables_derives {
options.set_variables_derives(variables_derives);
}
if let Some(response_derives) = response_derives {
options.set_response_derives(response_derives);
}
if let Some(deprecation_strategy) = deprecation_strategy {
options.set_deprecation_strategy(deprecation_strategy);
}
let mut all_queries = Vec::new();
let mut modules = Vec::new();
for query_path in query_paths {
let (module, variants) =
generate_module_token_stream(query_path.clone(), &schema_path, options.clone())?;
let module = module.to_string();
let query_file_name: ::std::ffi::OsString = query_path
.file_name()
.map(ToOwned::to_owned)
.ok_or_else(|| {
CodegenError::InputError(
"Failed to find a file name in the provided query path.".to_string()
)
})?;
let module_name = query_file_name.clone().into_string().unwrap();
let module_name = module_name.splitn(2, '.').next().unwrap().to_string();
modules.push(module_name);
let dest_file_path: PathBuf = output_directory.join(query_file_name).with_extension("rs");
let mut file = File::create(dest_file_path)?;
write!(file, "{}", module)?;
all_queries.extend(variants)
}
let tokens = generate_root_token_stream(modules, all_queries, options);
let dest_file_path: PathBuf = output_directory.join("mod").with_extension("rs");
let mut file = File::create(dest_file_path)?;
write!(file, "{}", tokens.to_string())?;
Ok(())
}