Skip to content

Commit

Permalink
Use graphql_client_codegen with output query string
Browse files Browse the repository at this point in the history
  • Loading branch information
dphm committed Jan 12, 2024
1 parent 4816796 commit 126105f
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 94 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ shopify_function = { path = "../shopify_function", version = "0.5.0" }
serde = { version = "1.0.13", features = ["derive"] }
serde_json = "1.0"
graphql_client = "0.13.0"
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }
3 changes: 0 additions & 3 deletions example_with_targets/.target_a.output.graphql

This file was deleted.

3 changes: 0 additions & 3 deletions example_with_targets/.target_b.output.graphql

This file was deleted.

1 change: 1 addition & 0 deletions example_with_targets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ shopify_function = { path = "../shopify_function", version = "0.5.0" }
serde = { version = "1.0.13", features = ["derive"] }
serde_json = "1.0"
graphql_client = "0.13.0"
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }
25 changes: 14 additions & 11 deletions example_with_targets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@ use shopify_function::{run_function_with_input, Result};

#[test]
fn test_a() -> Result<()> {
let result = run_function_with_input(
target_a,
r#"
let result = serde_json::to_string(
&run_function_with_input(
target_a,
r#"
{
"id": "gid://shopify/Order/1234567890",
"num": 123,
"name": "test"
}
"#,
)
.unwrap(),
)?;
let expected = crate::target_a::output::FunctionTargetAResult { status: Some(200) };
let expected = r#"{"status":200}"#;
assert_eq!(result, expected);
Ok(())
}

#[test]
fn test_function_b() -> Result<()> {
let result = run_function_with_input(
function_b,
r#"
let result = serde_json::to_string(
&run_function_with_input(
function_b,
r#"
{
"id": "gid://shopify/Order/1234567890",
"aResult": 200
}
"#,
)
.unwrap(),
)?;
let expected = crate::mod_b::output::FunctionTargetBResult {
name: Some("new name: \"gid://shopify/Order/1234567890\"".to_string()),
};

let expected = r#"{"name":"new name: \"gid://shopify/Order/1234567890\""}"#;
assert_eq!(result, expected);
Ok(())
}
1 change: 1 addition & 0 deletions shopify_function/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ shopify_function_macro = { version = "0.5.0", path = "../shopify_function_macro"

[dev-dependencies]
graphql_client = "0.13.0"
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }
65 changes: 33 additions & 32 deletions shopify_function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ A crate to help developers build [Shopify Functions].

## Dependencies

* Make sure you have `graphql_client` in your dependencies
- Make sure you have `graphql_client` in your dependencies

```
cargo add [email protected]
```
```
cargo add [email protected]
```

## 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`.
* 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.
- 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.

Expand All @@ -25,36 +24,37 @@ See the [example] for details on usage, or use the following guide to convert an
1. `cargo add [email protected]`
1. Delete `src/api.rs`.
1. In `main.rs`:
1. Add imports for `shopify_function`.

```rust
use shopify_function::prelude::*;
use shopify_function::Result;
```
1. Add imports for `shopify_function`.

1. Remove references to `mod api`.
1. Add type generation, right under your imports.
```rust
use shopify_function::prelude::*;
use shopify_function::Result;
```

```rust
generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql");
```
1. Remove references to `mod api`.
1. Add type generation, right under your imports.

1. Remove the `main` function entirely.
1. Attribute the `function` function with the `shopify_function` macro, and change its return type.
```rust
generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql");
```

```rust
#[shopify_function]
fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
```
1. Remove the `main` function entirely.
1. Attribute the `function` function with the `shopify_function` macro, and change its return type.

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

1. 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` |

1. Add `.output.graphql` to your `.gitignore`.

Expand All @@ -69,6 +69,7 @@ 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
Expand Down
1 change: 1 addition & 0 deletions shopify_function_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ syn = { version = "1.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0.43"
convert_case = "0.6.0"
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }
Loading

0 comments on commit 126105f

Please sign in to comment.