#shopify #function #input #unit-testing #input-output #api #macro

shopify_function

Crate to write Shopify Functions in Rust

14 releases (7 breaking)

0.8.1 Oct 8, 2024
0.8.0 May 8, 2024
0.7.1 May 8, 2024
0.7.0 Mar 8, 2024
0.2.3 Nov 8, 2022

#159 in Rust patterns

Download history 561/week @ 2024-07-30 663/week @ 2024-08-06 504/week @ 2024-08-13 518/week @ 2024-08-20 553/week @ 2024-08-27 590/week @ 2024-09-03 770/week @ 2024-09-10 642/week @ 2024-09-17 812/week @ 2024-09-24 698/week @ 2024-10-01 1207/week @ 2024-10-08 3783/week @ 2024-10-15 5893/week @ 2024-10-22 6883/week @ 2024-10-29 5285/week @ 2024-11-05 8178/week @ 2024-11-12

27,131 downloads per month

MIT license

10KB
107 lines

Shopify Functions Rust Crate

A crate to help developers build Shopify Functions.

Dependencies

  • Make sure you have graphql_client in your dependencies

    cargo add graphql_client@0.14.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.
  • The 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.
  • 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

  1. cargo add shopify_function

  2. cargo add graphql_client@0.14.0

  3. Delete src/api.rs.

  4. In main.rs:

    1. Add imports for shopify_function.

      use shopify_function::prelude::*;
      use shopify_function::Result;
      
    2. Remove references to mod api.

    3. Add type generation, right under your imports.

      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.

      #[shopify_function]
      fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
      
    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.

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

~2.4–3.5MB
~72K SLoC