Skip to content

Commit a2e8777

Browse files
Junha Yangjunha1
authored andcommitted
Add GqlBytes
It reduces the overhead of hex-en/decoding of strings in field implementations
1 parent 8221cd1 commit a2e8777

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

graphql-engine/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ struct MutationRoot {
5757

5858
#[async_graphql::Object]
5959
impl MutationRoot {
60-
async fn send_transaction(&self, tx_type: String, body: String) -> async_graphql::Result<String> {
61-
let tx = coordinator::Transaction::new(tx_type, hex::decode(body).map_err(|_| "Failed to parse body")?);
60+
async fn send_transaction(&self, tx_type: String, body: GqlBytes) -> async_graphql::Result<String> {
61+
let tx = coordinator::Transaction::new(tx_type, body.0);
6262
// NOTE: Check `queue_own_transaction()` won't cause a deadlock, especially when called by the async runtime.
6363
Ok(match self.client.queue_own_transaction(tx) {
6464
Ok(_) => "Done".to_owned(),

graphql-types/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,20 @@ impl ScalarType for GqlH256 {
8080
GqlValue::String(hex::encode(self.0.as_ref()))
8181
}
8282
}
83+
84+
pub struct GqlBytes(pub Vec<u8>);
85+
86+
#[Scalar]
87+
impl ScalarType for GqlBytes {
88+
fn parse(value: GqlValue) -> InputValueResult<Self> {
89+
if let GqlValue::String(s) = value {
90+
Ok(GqlBytes(hex::decode(&s).map_err(|_| InputValueError::custom("Invalid hex-encoded bytes".to_owned()))?))
91+
} else {
92+
Err(InputValueError::custom("String expected".to_owned()))
93+
}
94+
}
95+
96+
fn to_value(&self) -> GqlValue {
97+
GqlValue::String(hex::encode(&self.0))
98+
}
99+
}

0 commit comments

Comments
 (0)