Skip to content

Commit 74f49f6

Browse files
authored
Merge pull request #56 from Shopify/jb/extern-enums
Add `extern_enums` and skip codegen for large enums
2 parents 8b4860f + 3bf99bb commit 74f49f6

File tree

9 files changed

+208
-43
lines changed

9 files changed

+208
-43
lines changed

example_with_targets/README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,27 @@ input_query = "b.graphql"
7676
export = "function_b"
7777
```
7878

79-
- `target`: the API-specific handle for the target implemented by the Wasm function
80-
- `input_query`: the path to the target-specific input query file
81-
- `export` (optional): the name of the Wasm function export to run
82-
- default: the target handle as `snake_case`
79+
- `target`: The API-specific handle for the target implemented by the Wasm function.
80+
- `input_query`: The path to the target-specific input query file.
81+
- `export` (optional): The name of the Wasm function export to run.
82+
- default: The target handle as `snake_case`.
8383

8484
## `shopify_function_target` usage
8585

8686
### Arguments
8787

88-
- `query_path`: the path to the input query file for the target
89-
- `schema_path`: the path to the API schema file for the target
90-
- `target` (optional): the API-specific handle for the target if the function name does not match the target handle as `snake_case`
91-
- `module_name` (optional): the name of the generated module
92-
- default: the target handle as `snake_case`
88+
- `query_path`: A path to a GraphQL query, whose result will be used
89+
as the input for the function invocation. The query MUST be named "Input".
90+
- `schema_path`: A path to Shopify's GraphQL schema definition. Use the CLI
91+
to download a fresh copy.
92+
- `target` (optional): The API-specific handle for the target if the function name does not match the target handle as `snake_case`.
93+
- `module_name` (optional): The name of the generated module.
94+
- default: The target handle as `snake_case`
95+
- `extern_enums` (optional): A list of Enums for which an external type should be used.
96+
For those, code generation will be skipped. This is useful for large enums
97+
which can increase binary size, or for enums shared between multiple targets.
98+
Example: `extern_enums = ["LanguageCode"]`
99+
- default: `["LanguageCode", "CountryCode", "CurrencyCode"]`
93100

94101
### `src/lib.rs`
95102

shopify_function/src/enums.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub type CountryCode = String;
2+
pub type CurrencyCode = String;
3+
pub type LanguageCode = String;

shopify_function/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
1919
pub use shopify_function_macro::{generate_types, shopify_function, shopify_function_target};
2020

21+
#[doc(hidden)]
22+
pub mod enums;
2123
/// Only used for struct generation.
2224
#[doc(hidden)]
2325
pub mod scalars;
2426

2527
pub mod prelude {
28+
pub use crate::enums::*;
2629
pub use crate::scalars::*;
2730
pub use shopify_function_macro::{generate_types, shopify_function, shopify_function_target};
2831
}

shopify_function/tests/fixtures/input.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ query Input {
22
id
33
num
44
name
5+
country
56
}

shopify_function/tests/fixtures/schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Input {
2828
id: ID!
2929
num: Int
3030
name: String
31+
country: CountryCode
3132
}
3233

3334
"""
@@ -51,3 +52,8 @@ The result of the function.
5152
input FunctionResult {
5253
name: String
5354
}
55+
56+
enum CountryCode {
57+
AC
58+
CA
59+
}

shopify_function/tests/fixtures/schema_with_targets.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Input {
2828
id: ID!
2929
num: Int
3030
name: String
31+
country: CountryCode,
3132
targetAResult: Int @restrictTarget(only: ["test.target-b"])
3233
}
3334

@@ -69,3 +70,8 @@ The result of API target B.
6970
input FunctionTargetBResult {
7071
name: String
7172
}
73+
74+
enum CountryCode {
75+
AC
76+
CA
77+
}

shopify_function/tests/shopify_function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use shopify_function::Result;
44
const FUNCTION_INPUT: &str = r#"{
55
"id": "gid://shopify/Order/1234567890",
66
"num": 123,
7-
"name": "test"
7+
"name": "test",
8+
"country": "CA"
89
}"#;
910
static mut FUNCTION_OUTPUT: Vec<u8> = vec![];
1011

shopify_function/tests/shopify_function_target.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use shopify_function::Result;
44
const TARGET_A_INPUT: &str = r#"{
55
"id": "gid://shopify/Order/1234567890",
66
"num": 123,
7-
"name": "test"
7+
"name": "test",
8+
"country": "CA"
89
}"#;
910
static mut TARGET_A_OUTPUT: Vec<u8> = vec![];
1011

@@ -24,8 +25,11 @@ fn test_target_a_export() {
2425
output_stream = unsafe { &mut TARGET_A_OUTPUT }
2526
)]
2627
fn target_a(
27-
_input: target_a::input::ResponseData,
28+
input: target_a::input::ResponseData,
2829
) -> Result<target_a::output::FunctionTargetAResult> {
30+
if input.country != Some("CA".to_string()) {
31+
panic!("Expected CountryCode to be the CA String")
32+
}
2933
Ok(target_a::output::FunctionTargetAResult { status: Some(200) })
3034
}
3135

@@ -49,7 +53,7 @@ fn test_mod_b_export() {
4953
query_path = "./tests/fixtures/b.graphql",
5054
schema_path = "./tests/fixtures/schema_with_targets.graphql",
5155
input_stream = std::io::Cursor::new(TARGET_B_INPUT.as_bytes().to_vec()),
52-
output_stream = unsafe { &mut TARGET_B_OUTPUT }
56+
output_stream = unsafe { &mut TARGET_B_OUTPUT },
5357
)]
5458
fn some_function(
5559
input: mod_b::input::ResponseData,
@@ -58,3 +62,26 @@ fn some_function(
5862
name: Some(format!("new name: {}", input.id)),
5963
})
6064
}
65+
66+
// Verify that the CountryCode enum is generated when `extern_enums = []`
67+
#[shopify_function_target(
68+
target = "test.target-a",
69+
module_name = "country_enum",
70+
query_path = "./tests/fixtures/input.graphql",
71+
schema_path = "./tests/fixtures/schema_with_targets.graphql",
72+
extern_enums = []
73+
)]
74+
fn _with_generated_country_code(
75+
input: country_enum::input::ResponseData,
76+
) -> Result<country_enum::output::FunctionTargetAResult> {
77+
use country_enum::*;
78+
79+
let status = match input.country {
80+
Some(input::CountryCode::CA) => 200,
81+
_ => 201,
82+
};
83+
84+
Ok(output::FunctionTargetAResult {
85+
status: Some(status),
86+
})
87+
}

0 commit comments

Comments
 (0)