Skip to content

Commit

Permalink
chore(torii): graphql make component keys filterable (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Sep 4, 2023
1 parent 0e031cc commit e095113
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
23 changes: 11 additions & 12 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ impl State for Sql {
);

for member in component.clone().members {
if member.key {
continue;
}

component_table_query.push_str(&format!(
"external_{} {}, ",
member.name,
Expand Down Expand Up @@ -253,15 +249,18 @@ impl State for Sql {
entity_id, keys_str, component_names
);

let member_results = sqlx::query(
"SELECT * FROM component_members WHERE key == FALSE AND component_id = ? ORDER BY id \
ASC",
)
.bind(component.to_lowercase())
.fetch_all(&self.pool)
.await?;
let member_names_result =
sqlx::query("SELECT * FROM component_members WHERE component_id = ? ORDER BY id ASC")
.bind(component.to_lowercase())
.fetch_all(&self.pool)
.await?;

// keys are part of component members, so combine keys and component values array
let mut member_values: Vec<FieldElement> = Vec::new();
member_values.extend(keys);
member_values.extend(values);

let (names_str, values_str) = format_values(member_results, values)?;
let (names_str, values_str) = format_values(member_names_result, member_values)?;
let insert_components = format!(
"INSERT OR REPLACE INTO external_{} (entity_id {}) VALUES ('{}' {})",
component.to_lowercase(),
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/component_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub async fn type_mapping_query(
type AS ty,
key,
created_at
FROM component_members WHERE key == FALSE AND component_id = ?
FROM component_members WHERE component_id = ?
"#,
)
.bind(component_id)
Expand Down
1 change: 1 addition & 0 deletions crates/torii/graphql/src/tests/components_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod tests {
("where: { xLT: 42 }", 0),
("where: { xLTE: 42 }", 1),
("where: { x: 1337, yGTE: 1234 }", 0),
(r#"where: { player: "0x2" }"#, 1), // player is a key
]);

for (filter, expected_total) in where_filters {
Expand Down

0 comments on commit e095113

Please sign in to comment.