Skip to content

Commit

Permalink
fix(torii): query models (#1752)
Browse files Browse the repository at this point in the history
* fix: query models

* fix: model for event message

* test: handle edge case

* fix: use correct type

* fmt
  • Loading branch information
Larkooo authored Apr 2, 2024
1 parent df5c3ea commit f9b8c02
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn model_union_field() -> Field {
let model_ids: Vec<(String, String)> = sqlx::query_as(
"SELECT id, name
FROM models
WHERE id = (
WHERE id IN (
SELECT model_id
FROM entity_model
WHERE entity_id = ?
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn model_union_field() -> Field {
let model_ids: Vec<(String, String)> = sqlx::query_as(
"SELECT id, name
FROM models
WHERE id = (
WHERE id IN (
SELECT model_id
FROM event_model
WHERE entity_id = ?
Expand Down
13 changes: 12 additions & 1 deletion crates/torii/graphql/src/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {

use crate::schema::build_schema;
use crate::tests::{
run_graphql_query, spinup_types_test, Connection, Entity, Record, Subrecord,
run_graphql_query, spinup_types_test, Connection, Entity, Record, RecordSibling, Subrecord,
};

async fn entities_query(schema: &Schema, arg: &str) -> Value {
Expand Down Expand Up @@ -62,6 +62,11 @@ mod tests {
random_u8
random_u128
}}
... on RecordSibling {{
__typename
record_id
random_u8
}}
... on Subrecord {{
__typename
record_id
Expand Down Expand Up @@ -224,10 +229,16 @@ mod tests {
let id = poseidon_hash_many(&[FieldElement::ZERO]);
let entity = entity_model_query(&schema, &id).await;
let models = entity.get("models").ok_or("no models found").unwrap();

// models should contain record & recordsibling
let record: Record = serde_json::from_value(models[0].clone()).unwrap();
assert_eq!(&record.__typename, "Record");
assert_eq!(record.record_id, 0);

let record_sibling: RecordSibling = serde_json::from_value(models[1].clone()).unwrap();
assert_eq!(&record_sibling.__typename, "RecordSibling");
assert_eq!(record_sibling.record_id, 0);

let id = poseidon_hash_many(&[FieldElement::ZERO, FieldElement::ONE]);
let entity = entity_model_query(&schema, &id).await;
let models = entity.get("models").ok_or("no models found").unwrap();
Expand Down

0 comments on commit f9b8c02

Please sign in to comment.