Skip to content

Commit 77cccf9

Browse files
committed
refactor: deserialize only once
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 3d12952 commit 77cccf9

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

common/src/indexer_service/http/indexer_service.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ pub enum AttestationOutput {
6666
#[async_trait]
6767
pub trait IndexerServiceImpl {
6868
type Error: std::error::Error;
69-
type Request: DeserializeOwned + Send + Debug + Serialize;
7069
type Response: IndexerServiceResponse + Sized;
7170
type State: Send + Sync;
7271

73-
async fn process_request(
72+
async fn process_request<Request: DeserializeOwned + Send + Debug + Serialize>(
7473
&self,
7574
manifest_id: DeploymentId,
76-
request: Self::Request,
77-
) -> Result<(Self::Request, Self::Response), Self::Error>;
75+
request: Request,
76+
) -> Result<(Request, Self::Response), Self::Error>;
7877
}
7978

8079
#[derive(Debug, Error)]

common/src/indexer_service/http/request_handler.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ where
8080
{
8181
trace!("Handling request for deployment `{manifest_id}`");
8282

83-
let request =
83+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
84+
pub struct QueryBody {
85+
pub query: String,
86+
pub variables: Option<Box<RawValue>>,
87+
}
88+
89+
let request: QueryBody =
8490
serde_json::from_slice(&body).map_err(|e| IndexerServiceError::InvalidRequest(e.into()))?;
8591

8692
let Some(receipt) = receipt.into_signed_receipt() else {
@@ -113,23 +119,15 @@ where
113119

114120
let allocation_id = receipt.message.allocation_id;
115121

116-
#[derive(Debug, serde::Deserialize)]
117-
pub struct QueryBody {
118-
pub query: String,
119-
pub variables: Option<Box<RawValue>>,
120-
}
121-
122-
let query_body: QueryBody =
123-
serde_json::from_slice(&body).map_err(|e| IndexerServiceError::InvalidRequest(e.into()))?;
124-
let variables = query_body
122+
let variables = request
125123
.variables
126124
.as_ref()
127125
.map(ToString::to_string)
128126
.unwrap_or_default();
129127
let mut ctx = Context::new();
130128
ctx.insert(AgoraQuery {
131129
deployment_id: manifest_id,
132-
query: query_body.query.clone(),
130+
query: request.query.clone(),
133131
variables,
134132
});
135133

service/src/service.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use indexer_common::indexer_service::http::{
1212
};
1313
use indexer_config::Config as MainConfig;
1414
use reqwest::Url;
15+
use serde::{de::DeserializeOwned, Serialize};
1516
use serde_json::{json, Value};
1617
use sqlx::PgPool;
1718
use thegraph_core::DeploymentId;
@@ -82,15 +83,14 @@ impl SubgraphService {
8283
#[async_trait]
8384
impl IndexerServiceImpl for SubgraphService {
8485
type Error = SubgraphServiceError;
85-
type Request = serde_json::Value;
8686
type Response = SubgraphServiceResponse;
8787
type State = SubgraphServiceState;
8888

89-
async fn process_request(
89+
async fn process_request<Request: DeserializeOwned + Send + std::fmt::Debug + Serialize>(
9090
&self,
9191
deployment: DeploymentId,
92-
request: Self::Request,
93-
) -> Result<(Self::Request, Self::Response), Self::Error> {
92+
request: Request,
93+
) -> Result<(Request, Self::Response), Self::Error> {
9494
let deployment_url = Url::parse(&self.state.graph_node_query_base_url)
9595
.expect("Invalid `graph_node.query_url` in config")
9696
.join(&format!("subgraphs/id/{deployment}"))

0 commit comments

Comments
 (0)