With the new #typegen
and #query
macros in the shopify_function
crate, is there any way at all to pass additional #[derive()]
macros to the generated types? To my understanding, you can’t, which really limits where you can use the auto-generated types.
Currently, the #typegen
macro is incompatible with my requirements, so I’m using:
- Handwritten custom structs that match the Discount Function API input schema
serde_json
for serialization to JSON- Custom code generator for JSON parsing (to reduce code size bloat)
For example, my app has a feature where the calculated Vec<ProductDiscountCandidate>
is serialized to JSON (using serde_json
) as a field of a custom struct:
/// A JSON object that contains the results of a discount visit.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VisitDiscountJsonResult {
pub discounts: Vec<ProductDiscountCandidate>,
pub metadata: VisitDiscountMetadata,
}
With the new #typegen
, since there is no way for me to add serde::Serialize
to the generated types, I would have to manually implement the serde::Serialize
trait for each type, which defeats the purpose of code generation.
I would like to use the new WASM Query API to reduce JSON parsing overhead in my Functions, but my code also runs in other contexts where the Query API doesn’t exist. Right now, it’s looking like the only way to make this work is going to be to implement the shopify_function_wasm_api::{Serialize, Deserialize}
traits manually (or with a custom code generator).
Thanks,
Tobe