Skip to content

Commit 3758f94

Browse files
committed
WIP Use graphql_client_codegen with output query string
1 parent 4816796 commit 3758f94

File tree

6 files changed

+87
-53
lines changed

6 files changed

+87
-53
lines changed

Cargo.lock

Lines changed: 28 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_with_targets/.target_a.output.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

example_with_targets/.target_b.output.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

shopify_function/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ anyhow = "1.0.62"
1313
shopify_function_macro = { version = "0.5.0", path = "../shopify_function_macro" }
1414

1515
[dev-dependencies]
16-
graphql_client = "0.13.0"
16+
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }

shopify_function_macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ syn = { version = "1.0", features = ["full"] }
1414
quote = "1.0"
1515
proc-macro2 = "1.0.43"
1616
convert_case = "0.6.0"
17+
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }

shopify_function_macro/src/lib.rs

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use convert_case::{Case, Casing};
2-
use std::io::Write;
2+
use graphql_client_codegen::{
3+
generate_module_token_stream, generate_module_token_stream_from_string, CodegenMode,
4+
GraphQLClientCodegenOptions,
5+
};
36
use std::path::Path;
47

58
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
@@ -237,23 +240,20 @@ pub fn shopify_function_target(
237240
|module_name| Ident::new(module_name.value().as_str(), Span::mixed_site()),
238241
);
239242

240-
let query_path = args.query_path.expect("No value given for query_path");
241-
let schema_path = args.schema_path.expect("No value given for schema_path");
242-
let output_query_file_name = format!(".{}{}", &target_handle_string, OUTPUT_QUERY_FILE_NAME);
243-
244-
let input_struct = generate_struct(
245-
"Input",
246-
query_path.value().as_str(),
247-
schema_path.value().as_str(),
243+
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
244+
let query_path = Path::new(&dir).join(
245+
args.query_path
246+
.expect("No value given for query_path")
247+
.value()
248+
.as_str(),
248249
);
249-
let output_struct = generate_struct(
250-
"Output",
251-
&output_query_file_name,
252-
schema_path.value().as_str(),
250+
let schema_path = Path::new(&dir).join(
251+
args.schema_path
252+
.expect("No value given for schema_path")
253+
.value()
254+
.as_str(),
253255
);
254-
if let Err(error) = extract_shopify_function_return_type(&ast) {
255-
return error.to_compile_error().into();
256-
}
256+
257257
let output_result_type = extract_shopify_function_return_type(&ast)
258258
.unwrap()
259259
.to_token_stream()
@@ -263,8 +263,12 @@ pub fn shopify_function_target(
263263
output_result_type,
264264
&target_handle_string.to_case(Case::Camel)
265265
);
266+
let input_struct = generate_input_struct(query_path.as_path(), &schema_path);
267+
let output_struct = generate_output_struct(&output_query, &schema_path);
266268

267-
write_output_query_file(&output_query_file_name, &output_query);
269+
if let Err(error) = extract_shopify_function_return_type(&ast) {
270+
return error.to_compile_error().into();
271+
}
268272

269273
let input_stream = args
270274
.input_stream
@@ -318,8 +322,6 @@ fn extract_attr(attrs: &TokenStream, attr: &str) -> String {
318322
value.as_str()[1..value.len() - 1].to_string()
319323
}
320324

321-
const OUTPUT_QUERY_FILE_NAME: &str = ".output.graphql";
322-
323325
/// Generate the types to interact with Shopify's API.
324326
///
325327
/// The macro generates two inline modules: `input` and `output`. The
@@ -340,15 +342,17 @@ const OUTPUT_QUERY_FILE_NAME: &str = ".output.graphql";
340342
pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream {
341343
let params = TokenStream::from(attr);
342344

343-
let query_path = extract_attr(&params, "query_path");
344-
let schema_path = extract_attr(&params, "schema_path");
345+
let query_path_attr = extract_attr(&params, "query_path");
346+
let schema_path_attr = extract_attr(&params, "schema_path");
347+
348+
let schema_path = build_path(&schema_path_attr);
349+
350+
let input_query_path = build_path(&query_path_attr);
351+
let input_struct = generate_input_struct(input_query_path.as_path(), &schema_path);
345352

346-
let input_struct = generate_struct("Input", &query_path, &schema_path);
347-
let output_struct = generate_struct("Output", OUTPUT_QUERY_FILE_NAME, &schema_path);
348353
let output_query =
349354
"mutation Output($result: FunctionResult!) {\n handleResult(result: $result)\n}\n";
350-
351-
write_output_query_file(OUTPUT_QUERY_FILE_NAME, output_query);
355+
let output_struct = generate_output_struct(output_query, &schema_path);
352356

353357
quote! {
354358
#input_struct
@@ -357,29 +361,39 @@ pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream
357361
.into()
358362
}
359363

360-
fn generate_struct(name: &str, query_path: &str, schema_path: &str) -> TokenStream {
361-
let name_ident = Ident::new(name, Span::mixed_site());
364+
fn build_path(path_attr: &str) -> std::path::PathBuf {
365+
let cargo_manifest_dir =
366+
std::env::var("CARGO_MANIFEST_DIR").expect("Error reading CARGO_MANIFEST_DIR from env");
367+
Path::new(&cargo_manifest_dir).join(path_attr)
368+
}
369+
370+
fn generate_input_struct(
371+
query_path: &std::path::Path,
372+
schema_path: &Path,
373+
) -> proc_macro2::TokenStream {
374+
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
375+
options.set_operation_name("Input".to_string());
376+
377+
let token_stream = generate_module_token_stream(query_path.to_path_buf(), schema_path, options)
378+
.expect("Error generating Input struct");
362379

363380
quote! {
364-
#[derive(graphql_client::GraphQLQuery, Clone, Debug, serde::Deserialize, PartialEq)]
365-
#[graphql(
366-
query_path = #query_path,
367-
schema_path = #schema_path,
368-
response_derives = "Clone,Debug,PartialEq,Deserialize,Serialize",
369-
variables_derives = "Clone,Debug,PartialEq,Deserialize",
370-
skip_serializing_none
371-
)]
372-
pub struct #name_ident;
381+
#token_stream
382+
pub struct Input;
373383
}
374384
}
375385

376-
fn write_output_query_file(output_query_file_name: &str, contents: &str) {
377-
let cargo_manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
378-
let output_query_path = Path::new(&cargo_manifest_dir).join(output_query_file_name);
379-
std::fs::File::create(output_query_path)
380-
.expect("Could not create output query file")
381-
.write_all(contents.as_bytes())
382-
.unwrap_or_else(|_| panic!("Could not write to {}", output_query_file_name));
386+
fn generate_output_struct(query: &str, schema_path: &Path) -> proc_macro2::TokenStream {
387+
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
388+
options.set_operation_name("Output".to_string());
389+
390+
let token_stream = generate_module_token_stream_from_string(query, schema_path, &options)
391+
.expect("Error generating Output struct");
392+
393+
quote! {
394+
#token_stream
395+
pub struct Output;
396+
}
383397
}
384398

385399
#[cfg(test)]

0 commit comments

Comments
 (0)