Skip to content

Commit a12a4fb

Browse files
Merge pull request #148 from Shopify/shopify-function-crate-ga
Propagate the Functions crate to (important) Rust templates and samples
2 parents e79aa22 + e37aa36 commit a12a4fb

File tree

27 files changed

+367
-785
lines changed

27 files changed

+367
-785
lines changed

.graphqlrc.cjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Configures the GraphQL language server for all the function schemas in this repo.
3+
*/
4+
const fs = require('node:fs');
5+
6+
function getProjects(path) {
7+
const projects = {}
8+
9+
const extensions = fs.readdirSync(`./${path}`);
10+
for (const entry of extensions) {
11+
const extensionPath = `./${path}/${entry}`;
12+
const schema = `${extensionPath}/schema.graphql`;
13+
if(!fs.existsSync(schema)) {
14+
continue;
15+
}
16+
17+
const projectName = extensionPath.substring(2).replaceAll('/', '-');
18+
projects[projectName] = {
19+
schema,
20+
documents: `${extensionPath}/input.graphql`
21+
}
22+
}
23+
24+
return projects;
25+
}
26+
27+
const projects = {
28+
...getProjects("sample-apps/discounts-tutorial/extensions"),
29+
...getProjects("checkout/rust/delivery-customization"),
30+
...getProjects("checkout/rust/payment-customization"),
31+
...getProjects("discounts/rust/order-discounts"),
32+
...getProjects("discounts/rust/product-discounts"),
33+
...getProjects("discounts/rust/shipping-discounts"),
34+
}
35+
36+
module.exports = {
37+
projects
38+
};

.vscode/settings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"rust-analyzer.linkedProjects": [
3+
"checkout/rust/delivery-customization/default/Cargo.toml",
4+
"checkout/rust/payment-customization/default/Cargo.toml",
5+
"discounts/rust/order-discounts/default/Cargo.toml",
6+
"discounts/rust/order-discounts/fixed-amount/Cargo.toml",
7+
"discounts/rust/product-discounts/default/Cargo.toml",
8+
"discounts/rust/product-discounts/fixed-amount/Cargo.toml",
9+
"discounts/rust/shipping-discounts/default/Cargo.toml",
10+
"discounts/rust/shipping-discounts/fixed-amount/Cargo.toml",
11+
"sample-apps/discounts-tutorial/extensions/volume/Cargo.toml"
12+
]
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2+
Cargo.lock
23
.output.graphql

checkout/rust/delivery-customization/default/Cargo.toml.liquid

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ edition = "2021"
66
[dependencies]
77
serde = { version = "1.0.13", features = ["derive"] }
88
serde_json = "1.0"
9-
shopify_function = { version = "0.2.3" }
10-
graphql_client = { git = "https://github.com/graphql-rust/graphql-client", rev = "0776197ad7cfde2c658490e7c7e627a21ed622cb" }
9+
shopify_function = { version = "0.2.4" }
10+
graphql_client = { version = "0.12.0" }
1111

1212
[profile.release]
1313
lto = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2+
Cargo.lock
23
.output.graphql

checkout/rust/payment-customization/default/Cargo.toml.liquid

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ edition = "2021"
66
[dependencies]
77
serde = { version = "1.0.13", features = ["derive"] }
88
serde_json = "1.0"
9-
shopify_function = { version = "0.2.3" }
10-
graphql_client = { git = "https://github.com/graphql-rust/graphql-client", rev = "0776197ad7cfde2c658490e7c7e627a21ed622cb" }
9+
shopify_function = { version = "0.2.4" }
10+
graphql_client = { version = "0.12.0" }
1111

1212
[profile.release]
1313
lto = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
Cargo.lock
3+
.output.graphql

discounts/rust/order-discounts/default/Cargo.toml.liquid

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ edition = "2021"
55

66
[dependencies]
77
serde = { version = "1.0.13", features = ["derive"] }
8-
serde_with = "1.13.0"
98
serde_json = "1.0"
9+
shopify_function = { version = "0.2.4" }
10+
graphql_client = { version = "0.12.0" }
1011

1112
[profile.release]
1213
lto = true

discounts/rust/order-discounts/default/src/api.rs

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 27 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,44 @@
1+
use shopify_function::prelude::*;
2+
use shopify_function::Result;
3+
14
use serde::{Deserialize, Serialize};
25

3-
mod api;
4-
use api::*;
6+
generate_types!(
7+
query_path = "./input.graphql",
8+
schema_path = "./schema.graphql"
9+
);
10+
511

6-
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
7-
#[serde(rename_all = "camelCase")]
8-
pub struct Configuration {}
12+
#[derive(Serialize, Deserialize, Default, PartialEq)]
13+
#[serde(rename_all(deserialize = "camelCase"))]
14+
struct Configuration {
15+
16+
}
917

1018
impl Configuration {
1119
fn from_str(value: &str) -> Self {
1220
serde_json::from_str(value).expect("Unable to parse configuration value from metafield")
1321
}
1422
}
1523

16-
impl input::Input {
17-
pub fn configuration(&self) -> Configuration {
18-
match &self.discount_node.metafield {
19-
Some(input::Metafield { value }) => Configuration::from_str(value),
20-
None => Configuration::default(),
21-
}
22-
}
23-
}
24+
#[shopify_function]
25+
fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
26+
let no_discount = output::FunctionResult {
27+
discounts: vec![],
28+
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,
29+
};
2430

25-
fn main() -> Result<(), Box<dyn std::error::Error>> {
26-
let input: input::Input = serde_json::from_reader(std::io::BufReader::new(std::io::stdin()))?;
27-
let mut out = std::io::stdout();
28-
let mut serializer = serde_json::Serializer::new(&mut out);
29-
function(input)?.serialize(&mut serializer)?;
30-
Ok(())
31-
}
31+
let _config = match input.discount_node.metafield {
32+
Some(input::InputDiscountNodeMetafield { value }) =>
33+
Configuration::from_str(&value),
34+
None => return Ok(no_discount),
35+
};
3236

33-
fn function(input: input::Input) -> Result<FunctionResult, Box<dyn std::error::Error>> {
34-
let _config = input.configuration();
35-
Ok(FunctionResult {
37+
Ok(output::FunctionResult {
3638
discounts: vec![],
37-
discount_application_strategy: DiscountApplicationStrategy::First,
39+
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,
3840
})
3941
}
4042

4143
#[cfg(test)]
42-
mod tests {
43-
use super::*;
44-
45-
fn input(config: Option<Configuration>) -> input::Input {
46-
input::Input {
47-
discount_node: input::DiscountNode {
48-
metafield: Some(input::Metafield {
49-
value: serde_json::to_string(&config.unwrap_or_default()).unwrap()
50-
}),
51-
},
52-
}
53-
}
54-
55-
#[test]
56-
fn test_discount_with_no_configuration() {
57-
let input = input(None);
58-
let handle_result = serde_json::json!(function(input).unwrap());
59-
60-
let expected_handle_result = serde_json::json!({
61-
"discounts": [],
62-
"discountApplicationStrategy": "FIRST",
63-
});
64-
assert_eq!(handle_result, expected_handle_result);
65-
}
66-
67-
#[test]
68-
fn test_discount_with_configuration() {
69-
let input = input(Some(Configuration {}));
70-
let handle_result = serde_json::json!(function(input).unwrap());
71-
72-
let expected_handle_result = serde_json::json!({
73-
"discounts": [],
74-
"discountApplicationStrategy": "FIRST",
75-
});
76-
assert_eq!(handle_result, expected_handle_result);
77-
}
78-
79-
#[test]
80-
fn test_input_deserialization() {
81-
let input_json = r#"
82-
{
83-
"discountNode": { "metafield": { "value": "{}" } }
84-
}
85-
"#;
86-
87-
let expected_input = input(Some(Configuration {}));
88-
assert_eq!(expected_input, serde_json::from_str::<input::Input>(input_json).unwrap());
89-
}
90-
}
44+
mod tests;

0 commit comments

Comments
 (0)