|
1 | 1 | use std::io::Write; |
2 | 2 | use std::path::Path; |
3 | 3 |
|
4 | | -use proc_macro2::TokenStream; |
5 | | -use proc_macro2::TokenTree; |
| 4 | +use proc_macro2::{Ident, Span, TokenStream, TokenTree}; |
6 | 5 | use quote::{quote, ToTokens}; |
7 | 6 | use syn::{self, parse::Parse, parse::ParseStream, parse_macro_input, Expr, FnArg, Token}; |
8 | 7 |
|
@@ -153,45 +152,46 @@ const OUTPUT_QUERY_FILE_NAME: &str = ".output.graphql"; |
153 | 152 | pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream { |
154 | 153 | let params = TokenStream::from(attr); |
155 | 154 |
|
156 | | - let cargo_manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 155 | + let query_path = extract_attr(¶ms, "query_path"); |
157 | 156 | let schema_path = extract_attr(¶ms, "schema_path"); |
158 | 157 |
|
159 | | - let mut output_query_path = Path::new(&cargo_manifest_dir).to_path_buf(); |
160 | | - output_query_path.push(OUTPUT_QUERY_FILE_NAME); |
161 | | - std::fs::File::create(&output_query_path) |
162 | | - .expect("Could not create output query file") |
163 | | - .write_all( |
164 | | - r" |
165 | | - mutation Output($result: FunctionResult!) { |
166 | | - handleResult(result: $result) |
167 | | - } |
168 | | - " |
169 | | - .as_bytes(), |
170 | | - ) |
171 | | - .expect("Could not write to .output.query"); |
| 158 | + let input_struct = generate_struct("Input", &query_path, &schema_path); |
| 159 | + let output_struct = generate_struct("Output", OUTPUT_QUERY_FILE_NAME, &schema_path); |
| 160 | + let output_query = |
| 161 | + "mutation Output($result: FunctionResult!) {\n handleResult(result: $result)\n}\n"; |
| 162 | + |
| 163 | + write_output_query_file(OUTPUT_QUERY_FILE_NAME, output_query); |
172 | 164 |
|
173 | 165 | quote! { |
174 | | - #[derive(graphql_client::GraphQLQuery, Clone, Debug, serde::Deserialize, PartialEq)] |
175 | | - #[serde(rename_all(deserialize = "camelCase"))] |
176 | | - #[graphql( |
177 | | - #params, |
178 | | - response_derives = "Clone,Debug,PartialEq,Eq,Deserialize", |
179 | | - variables_derives = "Clone,Debug,PartialEq,Eq,Deserialize", |
180 | | - skip_serializing_none |
181 | | - )] |
182 | | - struct Input; |
| 166 | + #input_struct |
| 167 | + #output_struct |
| 168 | + } |
| 169 | + .into() |
| 170 | +} |
| 171 | + |
| 172 | +fn generate_struct(name: &str, query_path: &str, schema_path: &str) -> TokenStream { |
| 173 | + let name_ident = Ident::new(name, Span::mixed_site()); |
183 | 174 |
|
| 175 | + quote! { |
184 | 176 | #[derive(graphql_client::GraphQLQuery, Clone, Debug, serde::Deserialize, PartialEq)] |
185 | 177 | #[graphql( |
186 | | - query_path = #OUTPUT_QUERY_FILE_NAME, |
| 178 | + query_path = #query_path, |
187 | 179 | schema_path = #schema_path, |
188 | 180 | response_derives = "Clone,Debug,PartialEq,Eq,Deserialize", |
189 | 181 | variables_derives = "Clone,Debug,PartialEq,Eq,Deserialize", |
190 | 182 | skip_serializing_none |
191 | 183 | )] |
192 | | - struct Output; |
| 184 | + struct #name_ident; |
193 | 185 | } |
194 | | - .into() |
| 186 | +} |
| 187 | + |
| 188 | +fn write_output_query_file(output_query_file_name: &str, contents: &str) { |
| 189 | + let cargo_manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 190 | + let output_query_path = Path::new(&cargo_manifest_dir).join(output_query_file_name); |
| 191 | + std::fs::File::create(output_query_path) |
| 192 | + .expect("Could not create output query file") |
| 193 | + .write_all(contents.as_bytes()) |
| 194 | + .unwrap_or_else(|_| panic!("Could not write to {}", output_query_file_name)); |
195 | 195 | } |
196 | 196 |
|
197 | 197 | #[cfg(test)] |
|
0 commit comments