# Shopify Functions Rust Crate
A crate to help developers build [Shopify Functions].
## Usage
- The [`typegen`] macro allows you to generate structs based on your Function API (based on the provided GraphQL schema) and multiple [input queries][input query].
- The [`shopify_function`] attribute 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_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_with_targets] for details on usage, or use the following guide to convert an existing Rust-based function.
## Updating an existing function using a version of `shopify_function` below `1.0.0` to use version `1.0.0` and above
1. In `main.rs`, add imports for `shopify_function`.
```rust
use shopify_function::prelude::*;
use shopify_function::Result;
```
1. In `main.rs`, add type generation, right under your imports. Remove any references to the `generate_types!` macro. Replace `./input.graphql` with the location of your input query file (e.g. `src/run.graphql`).
```rust
#[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:
```rust
#[typegen("./schema.graphql")]
pub mod schema {
#[query("src/target_a.graphql")]
pub mod target_a {}
#[query("src/target_b.graphql")]
pub mod target_b {}
}
```
1. In `main.rs`, ensure that you have a `main` function that returns an error indicating to invoke a named export:
```rust
fn main() {
eprintln!("Invoke a named import");
std::process::exit(1);
}
```
1. Throughout all of your source files, replace any references to `#[shopify_function_target]` with the `shopify_function` macro, and change its return type. Typically, this is located in a file with a name equal to the target, e.g. `run.rs`.
```rust
#[shopify_function]
fn run(input: schema::input::Input) -> Result<schema::FunctionRunResult> {
```
1. Update the types and fields utilized in the function to the new, auto-generated structs. For example:
| Old | New |
| --- | --- |
| `input::ResponseData` | `schema::input::Input` |
| `input::InputDiscountNodeMetafield` | `schema::input::input::discount_node::Metafield` |
| `input::InputDiscountNode` | `schema::input::input::DiscountNode` |
| `output::FunctionRunResult` | `schema::FunctionRunResult` |
| `output::DiscountApplicationStrategy::FIRST` | `schema::DiscountApplicationStrategy::First` |
If referencing generated types from a file other than `main.rs` where they are defined, you'll need to import the schema. For example in `run.rs` you would need to add:
```rust
use crate::schema;
```
## Viewing the generated types
To preview the types generated by the `typegen` macro, use the [`cargo doc`](https://doc.rust-lang.org/cargo/commands/cargo-doc.html) command.
```bash
cargo doc --open
```
You can also use the [cargo-expand](https://github.com/dtolnay/cargo-expand) crate to view the generated source, or use the [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) VSCode extension to get [IntelliSense](https://code.visualstudio.com/docs/editor/intellisense) for Rust and the generated types.
---
License Apache-2.0
[Shopify Functions]: https://shopify.dev/api/functions
[`generate_types`]: https://docs.rs/shopify_function/latest/shopify_function/macro.typegen.html
[input query]: https://shopify.dev/api/functions/input-output#input
[`shopify_function`]: https://docs.rs/shopify_function/latest/shopify_function/attr.shopify_function.html
[`run_function_with_input`]: https://docs.rs/shopify_function/latest/shopify_function/fn.run_function_with_input.html
[example_with_targets]: https://github.com/Shopify/shopify-function-rust/tree/main/example_with_targets