22 releases (8 stable)
| 2.1.0 | Feb 24, 2026 |
|---|---|
| 2.0.3 | Nov 11, 2025 |
| 2.0.2 | Oct 29, 2025 |
| 1.1.1 | Jun 18, 2025 |
| 0.2.3 | Nov 8, 2022 |
#168 in Encoding
50,195 downloads per month
19KB
302 lines
Shopify Functions Rust Crate
A crate to help developers build Shopify Functions.
Usage
- The
typegenmacro allows you to generate structs based on your Function API (based on the provided GraphQL schema) and multiple input queries. - The
shopify_functionattribute macro marks the following function as the entry point for a Shopify Function. It manages the Functions input parsing and output serialization for you. - The
run_function_with_inputfunction is a utility for unit testing which allows you to quickly add new tests based on a given JSON input string.
See the example_with_targets for details on usage, or use the following guide to convert an existing Rust-based function.
Updating an existing function to using shopify_function 2.0.0 and higher
If you are using a version less than 1.0.0, you should update to version 1.1.1 as outlined below before following these steps.
-
Update to the latest Shopify CLI version.
-
Install the
wasm32-unknown-unknownbuild target usingrustup target:rustup target add wasm32-unknown-unknown -
Update your build
commandandpathin the[extensions.build]section of yourshopify.extension.tomlto usewasm32-unknown-unknowninstead ofwasm32-wasip1. ReplaceRUST-PACKAGE-NAMEwith thenamefrom yourCargo.toml:[extensions.build] command = "cargo build --target=wasm32-unknown-unknown --release" path = "target/wasm32-unknown-unknown/release/[RUST-PACKAGE-NAME].wasm" -
Throughout all of your source files, update any references to
eprintln!to uselog!instead.#[shopify_function] fn run(input: schema::run::Input) -> Result<schema::FunctionRunResult> { log!("This will be logged"); todo!(); } -
Throughout all of your source files, update any references to
process::exit(1)to useprocess::abort()instead.#[shopify_function] fn run(input: schema::run::Input) -> Result<schema::FunctionRunResult> { log!("Please invoke a named export."); process::abort(); }
Updating an existing function using a version of shopify_function below 1.0.0 to use version 1.0.0 and above
-
In
main.rs, add imports forshopify_function.use shopify_function::prelude::*; use shopify_function::Result; -
In
main.rs, add type generation, right under your imports. Remove any references to thegenerate_types!macro. Replace./input.graphqlwith the location of your input query file (e.g.src/run.graphql).#[typegen("./schema.graphql")] pub mod schema { #[query("./input.graphql")] pub mod input {} }If your Function has multiple targets each with their own input query, add a nested module for each. For example:
#[typegen("./schema.graphql")] pub mod schema { #[query("src/target_a.graphql")] pub mod target_a {} #[query("src/target_b.graphql")] pub mod target_b {} } -
In
main.rs, ensure that you have amainfunction that returns an error indicating to invoke a named export:fn main() { eprintln!("Invoke a named import"); std::process::exit(1); } -
Throughout all of your source files, replace any references to
#[shopify_function_target]with theshopify_functionmacro, and change its return type. Typically, this is located in a file with a name equal to the target, e.g.run.rs.#[shopify_function] fn run(input: schema::input::Input) -> Result<schema::FunctionRunResult> { -
Update the types and fields utilized in the function to the new, auto-generated structs. For example:
Old New input::ResponseDataschema::input::Inputinput::InputDiscountNodeMetafieldschema::input::input::discount_node::Metafieldinput::InputDiscountNodeschema::input::input::DiscountNodeoutput::FunctionRunResultschema::FunctionRunResultoutput::DiscountApplicationStrategy::FIRSTschema::DiscountApplicationStrategy::FirstIf referencing generated types from a file other than
main.rswhere they are defined, you'll need to import the schema. For example inrun.rsyou would need to add:use crate::schema;
Viewing the generated types
To preview the types generated by the typegen macro, use the cargo doc command.
cargo doc --open
You can also use the cargo-expand crate to view the generated source, or use the rust-analyzer VSCode extension to get IntelliSense for Rust and the generated types.
License MIT
Dependencies
~5MB
~90K SLoC