Shopify Functions Rust Crate

A crate to help developers build [Shopify Functions].

Dependencies

Usage

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

  1. cargo add shopify_function
  2. cargo add graphql_client@0.13.0
  3. Delete src/api.rs.
  4. In main.rs:

    1. Add imports for shopify_function.

      rust use shopify_function::prelude::*; use shopify_function::Result;

    2. Remove references to mod api.

    3. Add type generation, right under your imports.

      rust generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql");

    4. Remove the main function entirely.

    5. Attribute the function function with the shopify_function macro, and change its return type.

      ```rust

      [shopify_function]

      fn function(input: input::ResponseData) -> Result { ```

    6. 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 |

  5. 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.

bash 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