Skip to content

Commit 6f61148

Browse files
authored
Merge pull request #138 from Shopify/ap.array-reference-fix
Optional array reference fix
2 parents 34de444 + 89289a3 commit 6f61148

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

example_with_targets/b.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
query Input {
22
id
33
targetAResult
4+
optionalArray
5+
optionalArrayOfArrays
6+
optionalArrayOfOptionalArrays
47
}

example_with_targets/schema.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ type Input {
7474
timeWithoutTimezone: TimeWithoutTimezone
7575
targetAResult: Int @restrictTarget(only: ["test.target-b"])
7676
country: CountryCode
77+
optionalArray: [String!]
78+
optionalArrayOfArrays: [[String!]!]
79+
optionalArrayOfOptionalArrays: [[String!]]
7780
}
7881

7982
"""

shopify_function_macro/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
339339
let field_type = Self::type_for_field(executable_struct, field.r#type(), true);
340340

341341
let properly_referenced_value =
342-
Self::reference_variable_for_type(field.r#type(), &format_ident!("value"));
342+
Self::reference_variable_for_type(field.r#type(), &format_ident!("value_ref"));
343343

344344
let description: Option<syn::Attribute> = field.description().map(|description| {
345345
let description_lit_str = syn::LitStr::new(description, Span::mixed_site());
@@ -356,6 +356,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
356356
let value = self.__wasm_value.get_interned_obj_prop(interned_string_id);
357357
shopify_function::wasm_api::Deserialize::deserialize(&value).unwrap()
358358
});
359+
let value_ref = &value;
359360
#properly_referenced_value
360361
}
361362
}
@@ -693,12 +694,16 @@ impl ShopifyFunctionCodeGenerator {
693694
variable: &syn::Ident,
694695
) -> syn::Expr {
695696
match r#type {
696-
WrappedExecutableType::Base(_) | WrappedExecutableType::Vec(_) => {
697-
parse_quote! { &#variable }
697+
WrappedExecutableType::Base(_) => {
698+
parse_quote! { #variable }
699+
}
700+
WrappedExecutableType::Vec(_) => {
701+
parse_quote! { #variable.as_slice()}
698702
}
699703
WrappedExecutableType::Optional(inner) => {
700-
let inner_reference = Self::reference_variable_for_type(inner, variable);
701-
parse_quote! { ::std::option::Option::as_ref(#inner_reference) }
704+
let inner_variable = format_ident!("v_inner");
705+
let inner_reference = Self::reference_variable_for_type(inner, &inner_variable);
706+
parse_quote! { ::std::option::Option::as_ref(#variable).map(|#inner_variable| #inner_reference) }
702707
}
703708
}
704709
}

0 commit comments

Comments
 (0)