Skip to content

Commit

Permalink
feat: event message field to modeldata
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jul 29, 2024
1 parent 3e0a5b1 commit a354b4c
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion crates/torii/graphql/src/object/model_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use super::inputs::where_input::{parse_where_argument, where_argument, WhereInpu
use super::inputs::InputObjectTrait;
use super::{BasicObject, ResolvableObject, TypeMapping, ValueMapping};
use crate::constants::{
ENTITY_ID_COLUMN, ENTITY_TABLE, EVENT_ID_COLUMN, ID_COLUMN, INTERNAL_ENTITY_ID_KEY,
ENTITY_ID_COLUMN, ENTITY_TABLE, EVENT_ID_COLUMN, EVENT_MESSAGE_TABLE, ID_COLUMN,
INTERNAL_ENTITY_ID_KEY,
};
use crate::mapping::ENTITY_TYPE_MAPPING;
use crate::query::data::{count_rows, fetch_multiple_rows, fetch_single_row};
Expand Down Expand Up @@ -77,6 +78,7 @@ impl BasicObject for ModelDataObject {
// root object requires entity_field association
let mut root = objects.pop().unwrap();
root = root.field(entity_field());
root = root.field(event_message_field());

objects.push(root);
objects
Expand Down Expand Up @@ -262,3 +264,23 @@ fn entity_field() -> Field {
})
})
}

fn event_message_field() -> Field {
Field::new("eventMessage", TypeRef::named("EventMessage"), |ctx| {
FieldFuture::new(async move {
match ctx.parent_value.try_to_value()? {
Value::Object(indexmap) => {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let entity_id = utils::extract::<String>(indexmap, INTERNAL_ENTITY_ID_KEY)?;
let data =
fetch_single_row(&mut conn, EVENT_MESSAGE_TABLE, ID_COLUMN, &entity_id)
.await?;
let event_message = value_mapping_from_row(&data, &ENTITY_TYPE_MAPPING, false)?;

Ok(Some(Value::Object(event_message)))
}
_ => Err("incorrect value, requires Value::Object".into()),
}
})
})
}

0 comments on commit a354b4c

Please sign in to comment.