Skip to content

Commit f99e15b

Browse files
authored
Merge pull request #66 from Shopify/jb/extern-enums-default-output
Revive default extern enums for outputs
2 parents f207d62 + d94fc4c commit f99e15b

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

shopify_function/tests/fixtures/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The result of the function.
5151
"""
5252
input FunctionResult {
5353
name: String
54+
country: CountryCode
5455
}
5556

5657
enum CountryCode {

shopify_function/tests/fixtures/schema_with_targets.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ The result of API target B.
6969
"""
7070
input FunctionTargetBResult {
7171
name: String
72+
country: CountryCode
7273
}
7374

7475
enum CountryCode {

shopify_function/tests/shopify_function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ generate_types!(
1616

1717
#[test]
1818
fn test_function() {
19-
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890"}"#;
19+
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890","country":"CA"}"#;
2020
main().unwrap();
2121
let actual_result = std::str::from_utf8(unsafe { FUNCTION_OUTPUT.as_slice() }).unwrap();
2222
assert_eq!(actual_result, expected_result);
@@ -29,5 +29,6 @@ fn test_function() {
2929
fn my_function(input: input::ResponseData) -> Result<output::FunctionResult> {
3030
Ok(output::FunctionResult {
3131
name: Some(format!("new name: {}", input.id)),
32+
country: Some("CA".to_string()),
3233
})
3334
}

shopify_function/tests/shopify_function_target.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static mut TARGET_B_OUTPUT: Vec<u8> = vec![];
4141

4242
#[test]
4343
fn test_mod_b_export() {
44-
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890"}"#;
44+
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890","country":"CA"}"#;
4545
mod_b::export();
4646
let actual_result = std::str::from_utf8(unsafe { TARGET_B_OUTPUT.as_slice() }).unwrap();
4747
assert_eq!(actual_result, expected_result);
@@ -60,6 +60,7 @@ fn some_function(
6060
) -> Result<mod_b::output::FunctionTargetBResult> {
6161
Ok(mod_b::output::FunctionTargetBResult {
6262
name: Some(format!("new name: {}", input.id)),
63+
country: Some("CA".to_string()),
6364
})
6465
}
6566

shopify_function_macro/src/lib.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,16 @@ pub fn shopify_function_target(
320320
.schema_path
321321
.expect("No value given for schema_path")
322322
.value();
323-
let extern_enums = args.extern_enums.as_ref().map(extract_extern_enums);
323+
let extern_enums = args
324+
.extern_enums
325+
.as_ref()
326+
.map(extract_extern_enums)
327+
.unwrap_or_else(default_exter_enums);
324328

325329
let input_struct = generate_input_struct(
326330
query_path.as_str(),
327331
schema_path.as_str(),
328-
extern_enums.as_deref(),
332+
extern_enums.as_slice(),
329333
);
330334

331335
if let Err(error) = extract_shopify_function_return_type(&ast) {
@@ -341,7 +345,7 @@ pub fn shopify_function_target(
341345
&target_handle_string.to_case(Case::Camel)
342346
);
343347
let output_struct =
344-
generate_output_struct(&output_query, schema_path.as_str(), extern_enums.as_deref());
348+
generate_output_struct(&output_query, schema_path.as_str(), extern_enums.as_slice());
345349

346350
if let Err(error) = extract_shopify_function_return_type(&ast) {
347351
return error.to_compile_error().into();
@@ -411,15 +415,20 @@ pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream
411415
.schema_path
412416
.expect("No value given for schema_path")
413417
.value();
414-
let extern_enums = args.extern_enums.as_ref().map(extract_extern_enums);
418+
let extern_enums = args
419+
.extern_enums
420+
.as_ref()
421+
.map(extract_extern_enums)
422+
.unwrap_or_else(default_exter_enums);
423+
415424
let input_struct = generate_input_struct(
416425
query_path.as_str(),
417426
schema_path.as_str(),
418-
extern_enums.as_deref(),
427+
extern_enums.as_slice(),
419428
);
420429
let output_query =
421430
"mutation Output($result: FunctionResult!) {\n handleResult(result: $result)\n}\n";
422-
let output_struct = generate_output_struct(output_query, &schema_path, extern_enums.as_deref());
431+
let output_struct = generate_output_struct(output_query, &schema_path, extern_enums.as_slice());
423432

424433
quote! {
425434
#input_struct
@@ -433,12 +442,8 @@ const DEFAULT_EXTERN_ENUMS: &[&str] = &["LanguageCode", "CountryCode", "Currency
433442
fn generate_input_struct(
434443
query_path: &str,
435444
schema_path: &str,
436-
extern_enums: Option<&[String]>,
445+
extern_enums: &[String],
437446
) -> TokenStream {
438-
let extern_enums = extern_enums
439-
.map(|e| e.to_owned())
440-
.unwrap_or_else(|| DEFAULT_EXTERN_ENUMS.iter().map(|e| e.to_string()).collect());
441-
442447
quote! {
443448
#[derive(graphql_client::GraphQLQuery, Clone, Debug, serde::Deserialize, PartialEq)]
444449
#[graphql(
@@ -455,7 +460,7 @@ fn generate_input_struct(
455460

456461
fn graphql_codegen_options(
457462
operation_name: String,
458-
extern_enums: Option<&[String]>,
463+
extern_enums: &[String],
459464
) -> GraphQLClientCodegenOptions {
460465
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
461466
options.set_operation_name(operation_name);
@@ -468,17 +473,15 @@ fn graphql_codegen_options(
468473
}
469474
.into(),
470475
);
471-
if let Some(extern_enums) = extern_enums {
472-
options.set_extern_enums(extern_enums.to_vec());
473-
}
476+
options.set_extern_enums(extern_enums.to_vec());
474477

475478
options
476479
}
477480

478481
fn generate_output_struct(
479482
query: &str,
480483
schema_path: &str,
481-
extern_enums: Option<&[String]>,
484+
extern_enums: &[String],
482485
) -> proc_macro2::TokenStream {
483486
let options = graphql_codegen_options("Output".to_string(), extern_enums);
484487
let cargo_manifest_dir =
@@ -511,6 +514,10 @@ fn extract_extern_enums(extern_enums: &ExprArray) -> Vec<String> {
511514
.collect()
512515
}
513516

517+
fn default_exter_enums() -> Vec<String> {
518+
DEFAULT_EXTERN_ENUMS.iter().map(|e| e.to_string()).collect()
519+
}
520+
514521
#[cfg(test)]
515522
mod tests {}
516523

0 commit comments

Comments
 (0)