10 unstable releases (4 breaking)
new 0.5.0 | Nov 30, 2023 |
---|---|
0.4.0 | Aug 22, 2023 |
0.3.0 | Aug 4, 2023 |
0.2.5 | May 30, 2023 |
0.1.0 | Oct 3, 2022 |
#226 in Parser implementations
2,826 downloads per month
9KB
82 lines
Shopify Functions Rust Crate
A crate to help developers build Shopify Functions.
Dependencies
-
Make sure you have
graphql_client
in your dependenciescargo add graphql_client@0.13.0
Usage
- The
generate_types
macro allows you to generate structs based on your input query. It will also generate output/response types for the current Function API, based on the provided schema.- It will automatically generate an
.output.graphql
file for code generation purposes. This file can be added to your.gitignore
.
- It will automatically generate an
- The
shopify_function
attribute macro marks the following function as the entry point for a Shopify Function. It manages the FunctionsSTDIN
input parsing andSTDOUT
output serialization for you. - The
run_function_with_input
function is a utility for unit testing which allows you to quickly add new tests based on a given JSON input string.
See the example for details on usage, or use the following guide to convert an existing Rust-based function.
Updating an existing function to use shopify_function
-
cargo add shopify_function
-
cargo add graphql_client@0.13.0
-
Delete
src/api.rs
. -
In
main.rs
:-
Add imports for
shopify_function
.use shopify_function::prelude::*; use shopify_function::Result;
-
Remove references to
mod api
. -
Add type generation, right under your imports.
generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql");
-
Remove the
main
function entirely. -
Attribute the
function
function with theshopify_function
macro, and change its return type.#[shopify_function] fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
-
Update the types and fields utilized in the function to the new, auto-generated structs. For example:
Old New input::Input
input::ResponseData
input::Metafield
input::InputDiscountNodeMetafield
input::DiscountNode
input::InputDiscountNode
FunctionResult
output::FunctionResult
DiscountApplicationStrategy::First
output::DiscountApplicationStrategy::FIRST
-
-
Add
.output.graphql
to your.gitignore
.
Viewing the generated types
To preview the types generated by the generate_types
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 Apache-2.0
Dependencies
~1.4–2.2MB
~45K SLoC