From 1ce8e47d6725126359021ae7f69575556818a2a3 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 15 Jan 2024 15:50:06 +0100 Subject: [PATCH] Switch transaction query arg from id to transactionHash --- .../torii/graphql/src/object/transaction.rs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/torii/graphql/src/object/transaction.rs b/crates/torii/graphql/src/object/transaction.rs index 17cf87bc33..bb55a2446f 100644 --- a/crates/torii/graphql/src/object/transaction.rs +++ b/crates/torii/graphql/src/object/transaction.rs @@ -1,7 +1,14 @@ +use async_graphql::dynamic::{Field, FieldFuture, InputValue, TypeRef}; +use async_graphql::Value; +use convert_case::{Case, Casing}; +use sqlx::{Pool, Row, Sqlite}; + use super::{ObjectTrait, TypeMapping}; use crate::constants::{TRANSACTION_NAMES, TRANSACTION_TABLE, TRANSACTION_TYPE_NAME}; use crate::mapping::TRANSACTION_MAPPING; - +use crate::query::data::fetch_single_row; +use crate::query::value_mapping_from_row; +use crate::utils::extract; pub struct TransactionObject; impl ObjectTrait for TransactionObject { @@ -20,4 +27,31 @@ impl ObjectTrait for TransactionObject { fn table_name(&self) -> Option<&str> { Some(TRANSACTION_TABLE) } + + fn resolve_one(&self) -> Option { + let type_mapping = self.type_mapping().clone(); + let table_name = self.table_name().unwrap().to_string(); + + Some( + Field::new(self.name().0, TypeRef::named_nn(self.type_name()), move |ctx| { + let type_mapping = type_mapping.clone(); + let table_name = table_name.to_string(); + + FieldFuture::new(async move { + let mut conn = ctx.data::>()?.acquire().await?; + let hash = + extract::(ctx.args.as_index_map(), &COLUMN.to_case(Case::Camel))?; + let data = fetch_single_row(&mut conn, &table_name, COLUMN, &hash).await?; + let model = value_mapping_from_row(&data, &type_mapping, false)?; + Ok(Some(Value::Object(model))) + }) + }) + .argument(InputValue::new( + COLUMN.to_case(Case::Camel), + TypeRef::named_nn(TypeRef::STRING), + )), + ) + } } + +const COLUMN: &str = "transaction_hash";