Skip to content

Commit 126105f

Browse files
committed
Use graphql_client_codegen with output query string
1 parent 4816796 commit 126105f

File tree

10 files changed

+144
-94
lines changed

10 files changed

+144
-94
lines changed

Cargo.lock

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ shopify_function = { path = "../shopify_function", version = "0.5.0" }
99
serde = { version = "1.0.13", features = ["derive"] }
1010
serde_json = "1.0"
1111
graphql_client = "0.13.0"
12+
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }

example_with_targets/.target_a.output.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

example_with_targets/.target_b.output.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

example_with_targets/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ shopify_function = { path = "../shopify_function", version = "0.5.0" }
99
serde = { version = "1.0.13", features = ["derive"] }
1010
serde_json = "1.0"
1111
graphql_client = "0.13.0"
12+
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }

example_with_targets/src/tests.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,39 @@ use shopify_function::{run_function_with_input, Result};
33

44
#[test]
55
fn test_a() -> Result<()> {
6-
let result = run_function_with_input(
7-
target_a,
8-
r#"
6+
let result = serde_json::to_string(
7+
&run_function_with_input(
8+
target_a,
9+
r#"
910
{
1011
"id": "gid://shopify/Order/1234567890",
1112
"num": 123,
1213
"name": "test"
1314
}
1415
"#,
16+
)
17+
.unwrap(),
1518
)?;
16-
let expected = crate::target_a::output::FunctionTargetAResult { status: Some(200) };
19+
let expected = r#"{"status":200}"#;
1720
assert_eq!(result, expected);
1821
Ok(())
1922
}
2023

2124
#[test]
2225
fn test_function_b() -> Result<()> {
23-
let result = run_function_with_input(
24-
function_b,
25-
r#"
26+
let result = serde_json::to_string(
27+
&run_function_with_input(
28+
function_b,
29+
r#"
2630
{
2731
"id": "gid://shopify/Order/1234567890",
2832
"aResult": 200
2933
}
3034
"#,
35+
)
36+
.unwrap(),
3137
)?;
32-
let expected = crate::mod_b::output::FunctionTargetBResult {
33-
name: Some("new name: \"gid://shopify/Order/1234567890\"".to_string()),
34-
};
35-
38+
let expected = r#"{"name":"new name: \"gid://shopify/Order/1234567890\""}"#;
3639
assert_eq!(result, expected);
3740
Ok(())
3841
}

shopify_function/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ shopify_function_macro = { version = "0.5.0", path = "../shopify_function_macro"
1414

1515
[dev-dependencies]
1616
graphql_client = "0.13.0"
17+
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }

shopify_function/README.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ A crate to help developers build [Shopify Functions].
44

55
## Dependencies
66

7-
* Make sure you have `graphql_client` in your dependencies
7+
- Make sure you have `graphql_client` in your dependencies
88

9-
```
10-
11-
```
9+
```
10+
11+
```
1212

1313
## Usage
1414

15-
* 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.
16-
* It will automatically generate an `.output.graphql` file for code generation purposes. This file can be added to your `.gitignore`.
17-
* 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.
18-
* 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.
15+
- 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.
16+
- 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.
17+
- 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.
1918

2019
See the [example] for details on usage, or use the following guide to convert an existing Rust-based function.
2120

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

30-
```rust
31-
use shopify_function::prelude::*;
32-
use shopify_function::Result;
33-
```
28+
1. Add imports for `shopify_function`.
3429

35-
1. Remove references to `mod api`.
36-
1. Add type generation, right under your imports.
30+
```rust
31+
use shopify_function::prelude::*;
32+
use shopify_function::Result;
33+
```
3734

38-
```rust
39-
generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql");
40-
```
35+
1. Remove references to `mod api`.
36+
1. Add type generation, right under your imports.
4137

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

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

50-
1. Update the types and fields utilized in the function to the new, auto-generated structs. For example:
51-
| Old | New |
52-
| --- | --- |
53-
| `input::Input` | `input::ResponseData` |
54-
| `input::Metafield` | `input::InputDiscountNodeMetafield` |
55-
| `input::DiscountNode` | `input::InputDiscountNode` |
56-
| `FunctionResult` | `output::FunctionResult` |
57-
| `DiscountApplicationStrategy::First` | `output::DiscountApplicationStrategy::FIRST` |
45+
```rust
46+
#[shopify_function]
47+
fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
48+
```
49+
50+
1. Update the types and fields utilized in the function to the new, auto-generated structs. For example:
51+
| Old | New |
52+
| --- | --- |
53+
| `input::Input` | `input::ResponseData` |
54+
| `input::Metafield` | `input::InputDiscountNodeMetafield` |
55+
| `input::DiscountNode` | `input::InputDiscountNode` |
56+
| `FunctionResult` | `output::FunctionResult` |
57+
| `DiscountApplicationStrategy::First` | `output::DiscountApplicationStrategy::FIRST` |
5858

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

@@ -69,6 +69,7 @@ cargo doc --open
6969
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.
7070

7171
---
72+
7273
License Apache-2.0
7374

7475
[Shopify Functions]: https://shopify.dev/api/functions

shopify_function_macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ syn = { version = "1.0", features = ["full"] }
1414
quote = "1.0"
1515
proc-macro2 = "1.0.43"
1616
convert_case = "0.6.0"
17+
graphql_client_codegen = { git = "https://github.com/dphm/graphql-client.git", branch = "query-string" }

0 commit comments

Comments
 (0)