A crate to help developers build [Shopify Functions].
Make sure you have graphql_client
in your dependencies
cargo add graphql_client@0.13.0
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.
.output.graphql
file for code generation purposes. This file can be added to your .gitignore
.shopify_function
] attribute macro marks the following function as the entry point for a Shopify Function. It manages the Functions STDIN
input parsing and STDOUT
output serialization for you.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.
shopify_function
cargo add shopify_function
cargo add graphql_client@0.13.0
src/api.rs
.In main.rs
:
Add imports for shopify_function
.
rust
use shopify_function::prelude::*;
use shopify_function::Result;
Remove references to mod api
.
Add type generation, right under your imports.
rust
generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql");
Remove the main
function entirely.
Attribute the function
function with the shopify_function
macro, and change its return type.
```rust
fn function(input: input::ResponseData) -> Result
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
.
License Apache-2.0