Skip to content

Revive default extern enums for outputs #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shopify_function/tests/fixtures/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The result of the function.
"""
input FunctionResult {
name: String
country: CountryCode
}

enum CountryCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The result of API target B.
"""
input FunctionTargetBResult {
name: String
country: CountryCode
}

enum CountryCode {
Expand Down
3 changes: 2 additions & 1 deletion shopify_function/tests/shopify_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ generate_types!(

#[test]
fn test_function() {
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890"}"#;
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890","country":"CA"}"#;
main().unwrap();
let actual_result = std::str::from_utf8(unsafe { FUNCTION_OUTPUT.as_slice() }).unwrap();
assert_eq!(actual_result, expected_result);
Expand All @@ -29,5 +29,6 @@ fn test_function() {
fn my_function(input: input::ResponseData) -> Result<output::FunctionResult> {
Ok(output::FunctionResult {
name: Some(format!("new name: {}", input.id)),
country: Some("CA".to_string()),
})
}
3 changes: 2 additions & 1 deletion shopify_function/tests/shopify_function_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static mut TARGET_B_OUTPUT: Vec<u8> = vec![];

#[test]
fn test_mod_b_export() {
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890"}"#;
let expected_result = r#"{"name":"new name: gid://shopify/Order/1234567890","country":"CA"}"#;
mod_b::export();
let actual_result = std::str::from_utf8(unsafe { TARGET_B_OUTPUT.as_slice() }).unwrap();
assert_eq!(actual_result, expected_result);
Expand All @@ -60,6 +60,7 @@ fn some_function(
) -> Result<mod_b::output::FunctionTargetBResult> {
Ok(mod_b::output::FunctionTargetBResult {
name: Some(format!("new name: {}", input.id)),
country: Some("CA".to_string()),
})
}

Expand Down
39 changes: 23 additions & 16 deletions shopify_function_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,16 @@ pub fn shopify_function_target(
.schema_path
.expect("No value given for schema_path")
.value();
let extern_enums = args.extern_enums.as_ref().map(extract_extern_enums);
let extern_enums = args
.extern_enums
.as_ref()
.map(extract_extern_enums)
.unwrap_or_else(default_exter_enums);

let input_struct = generate_input_struct(
query_path.as_str(),
schema_path.as_str(),
extern_enums.as_deref(),
extern_enums.as_slice(),
);

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

if let Err(error) = extract_shopify_function_return_type(&ast) {
return error.to_compile_error().into();
Expand Down Expand Up @@ -411,15 +415,20 @@ pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream
.schema_path
.expect("No value given for schema_path")
.value();
let extern_enums = args.extern_enums.as_ref().map(extract_extern_enums);
let extern_enums = args
.extern_enums
.as_ref()
.map(extract_extern_enums)
.unwrap_or_else(default_exter_enums);

let input_struct = generate_input_struct(
query_path.as_str(),
schema_path.as_str(),
extern_enums.as_deref(),
extern_enums.as_slice(),
);
let output_query =
"mutation Output($result: FunctionResult!) {\n handleResult(result: $result)\n}\n";
let output_struct = generate_output_struct(output_query, &schema_path, extern_enums.as_deref());
let output_struct = generate_output_struct(output_query, &schema_path, extern_enums.as_slice());

quote! {
#input_struct
Expand All @@ -433,12 +442,8 @@ const DEFAULT_EXTERN_ENUMS: &[&str] = &["LanguageCode", "CountryCode", "Currency
fn generate_input_struct(
query_path: &str,
schema_path: &str,
extern_enums: Option<&[String]>,
extern_enums: &[String],
) -> TokenStream {
let extern_enums = extern_enums
.map(|e| e.to_owned())
.unwrap_or_else(|| DEFAULT_EXTERN_ENUMS.iter().map(|e| e.to_string()).collect());

quote! {
#[derive(graphql_client::GraphQLQuery, Clone, Debug, serde::Deserialize, PartialEq)]
#[graphql(
Expand All @@ -455,7 +460,7 @@ fn generate_input_struct(

fn graphql_codegen_options(
operation_name: String,
extern_enums: Option<&[String]>,
extern_enums: &[String],
) -> GraphQLClientCodegenOptions {
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
options.set_operation_name(operation_name);
Expand All @@ -468,17 +473,15 @@ fn graphql_codegen_options(
}
.into(),
);
if let Some(extern_enums) = extern_enums {
options.set_extern_enums(extern_enums.to_vec());
}
options.set_extern_enums(extern_enums.to_vec());

options
}

fn generate_output_struct(
query: &str,
schema_path: &str,
extern_enums: Option<&[String]>,
extern_enums: &[String],
) -> proc_macro2::TokenStream {
let options = graphql_codegen_options("Output".to_string(), extern_enums);
let cargo_manifest_dir =
Expand Down Expand Up @@ -511,6 +514,10 @@ fn extract_extern_enums(extern_enums: &ExprArray) -> Vec<String> {
.collect()
}

fn default_exter_enums() -> Vec<String> {
DEFAULT_EXTERN_ENUMS.iter().map(|e| e.to_string()).collect()
}

#[cfg(test)]
mod tests {}

Expand Down
Loading