Skip to content

Commit

Permalink
Pad query values (#1238)
Browse files Browse the repository at this point in the history
* Pad filter input

* add missing case

* rollback records contract

* Feat: Parse value to hex

* fmt
  • Loading branch information
gianalarcon authored Dec 8, 2023
1 parent 1388c14 commit b217aac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
17 changes: 13 additions & 4 deletions crates/torii/graphql/src/object/inputs/where_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@ fn parse_integer(
}

fn parse_string(input: ValueAccessor<'_>, type_name: &str) -> Result<FilterValue> {
input
.string()
.map(|i| FilterValue::String(i.to_string()))
.map_err(|_| GqlError::new(format!("Expected string on field {}", type_name)))
match input.string() {
Ok(i) => match i.starts_with("0x") {
true => Ok(FilterValue::String(format!("0x{:0>64}", i.strip_prefix("0x").unwrap()))), /* safe to unwrap since we know it starts with 0x */
false => match i.parse::<u128>() {
Ok(val) => Ok(FilterValue::String(format!("0x{:0>64x}", val))),
Err(_) => Err(GqlError::new(format!(
"Failed to parse integer value on field {}",
type_name
))),
},
},
Err(_) => Err(GqlError::new(format!("Expected string on field {}", type_name))),
}
}
10 changes: 5 additions & 5 deletions crates/torii/graphql/src/tests/models_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ mod tests {
let first_record = connection.edges.first().unwrap();
assert_eq!(first_record.node.type_u64, 3);

// NOTE: output leading zeros on hex strings are trimmed, however, we don't do this yet on
// input hex strings
let felt_str_0x5 = format!("0x{:064x}", 5);
// NOTE: Server side is gonna parse "0x5" and "5" to hexadecimal format
let felt_str_0x5 = "0x5";
let felt_int_5 = "5";

// where filter EQ on class_hash and contract_address
let records = records_model_query(
&schema,
&format!(
"(where: {{ type_class_hash: \"{}\", type_contract_address: \"{}\" }})",
felt_str_0x5, felt_str_0x5
felt_str_0x5, felt_int_5
),
)
.await;
Expand All @@ -156,7 +156,7 @@ mod tests {
// where filter LT on u256 (string)
let records = records_model_query(
&schema,
&format!("(where: {{ type_u256LT: \"{}\" }})", felt_str_0x5),
&format!("(where: {{ type_u256LT: \"{}\" }})", felt_int_5),
)
.await;
let connection: Connection<Record> = serde_json::from_value(records).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/tests/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "0.3.13"
version = "0.3.15"
dependencies = [
"dojo_plugin",
]
Expand Down

0 comments on commit b217aac

Please sign in to comment.