Skip to content

Commit 210c8f3

Browse files
authored
refactor: remove service impl trait (#500)
1 parent 0911515 commit 210c8f3

File tree

5 files changed

+364
-400
lines changed

5 files changed

+364
-400
lines changed

crates/service/src/routes/cost.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use std::str::FromStr;
5-
use std::sync::Arc;
65

76
use crate::database::cost_model::{self, CostModel};
87
use async_graphql::{Context, EmptyMutation, EmptySubscription, Object, Schema, SimpleObject};
@@ -127,7 +126,7 @@ impl Query {
127126
ctx: &Context<'_>,
128127
deployment_ids: Vec<DeploymentId>,
129128
) -> Result<Vec<GraphQlCostModel>, anyhow::Error> {
130-
let pool = &ctx.data_unchecked::<Arc<SubgraphServiceState>>().database;
129+
let pool = &ctx.data_unchecked::<SubgraphServiceState>().database;
131130
let cost_models = cost_model::cost_models(pool, &deployment_ids).await?;
132131
Ok(cost_models.into_iter().map(|m| m.into()).collect())
133132
}
@@ -137,7 +136,7 @@ impl Query {
137136
ctx: &Context<'_>,
138137
deployment_id: DeploymentId,
139138
) -> Result<Option<GraphQlCostModel>, anyhow::Error> {
140-
let pool = &ctx.data_unchecked::<Arc<SubgraphServiceState>>().database;
139+
let pool = &ctx.data_unchecked::<SubgraphServiceState>().database;
141140
cost_model::cost_model(pool, &deployment_id)
142141
.await
143142
.map(|model_opt| model_opt.map(GraphQlCostModel::from))
@@ -151,7 +150,7 @@ pub async fn build_schema() -> CostSchema {
151150
}
152151

153152
pub async fn cost(
154-
State(state): State<Arc<SubgraphServiceState>>,
153+
State(state): State<SubgraphServiceState>,
155154
req: GraphQLRequest,
156155
) -> GraphQLResponse {
157156
state

crates/service/src/routes/request_handler.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use thegraph_core::DeploymentId;
1919
use tracing::trace;
2020

2121
use crate::service::{
22-
AttestationOutput, IndexerServiceError, IndexerServiceImpl, IndexerServiceResponse,
23-
IndexerServiceState, TapReceipt,
22+
AttestationOutput, IndexerServiceError, IndexerServiceResponse, IndexerServiceState, TapReceipt,
2423
};
2524

2625
lazy_static! {
@@ -45,16 +44,13 @@ lazy_static! {
4544

4645
}
4746

48-
pub async fn request_handler<I>(
47+
pub async fn request_handler(
4948
Path(manifest_id): Path<DeploymentId>,
5049
typed_header: TypedHeader<TapReceipt>,
51-
state: State<Arc<IndexerServiceState<I>>>,
50+
state: State<Arc<IndexerServiceState>>,
5251
headers: HeaderMap,
5352
body: String,
54-
) -> Result<impl IntoResponse, IndexerServiceError<I::Error>>
55-
where
56-
I: IndexerServiceImpl + Sync + Send + 'static,
57-
{
53+
) -> Result<impl IntoResponse, IndexerServiceError> {
5854
_request_handler(manifest_id, typed_header, state, headers, body)
5955
.await
6056
.inspect_err(|_| {
@@ -64,16 +60,13 @@ where
6460
})
6561
}
6662

67-
async fn _request_handler<I>(
63+
async fn _request_handler(
6864
manifest_id: DeploymentId,
6965
TypedHeader(receipt): TypedHeader<TapReceipt>,
70-
State(state): State<Arc<IndexerServiceState<I>>>,
66+
State(state): State<Arc<IndexerServiceState>>,
7167
headers: HeaderMap,
7268
req: String,
73-
) -> Result<impl IntoResponse, IndexerServiceError<I::Error>>
74-
where
75-
I: IndexerServiceImpl + Sync + Send + 'static,
76-
{
69+
) -> Result<impl IntoResponse, IndexerServiceError> {
7770
trace!("Handling request for deployment `{manifest_id}`");
7871

7972
let request: QueryBody =

crates/service/src/routes/status.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use std::collections::HashSet;
5-
use std::sync::Arc;
65

76
use async_graphql_axum::GraphQLRequest;
87
use axum::{extract::State, response::IntoResponse, Json};
@@ -58,7 +57,7 @@ impl IntoRequestParameters for WrappedGraphQLRequest {
5857

5958
// Custom middleware function to process the request before reaching the main handler
6059
pub async fn status(
61-
State(state): State<Arc<SubgraphServiceState>>,
60+
State(state): State<SubgraphServiceState>,
6261
request: GraphQLRequest,
6362
) -> Result<impl IntoResponse, SubgraphServiceError> {
6463
let request = request.into_inner();

crates/service/src/service.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use anyhow::anyhow;
99
use async_graphql::{EmptySubscription, Schema};
1010
use async_graphql_axum::GraphQL;
1111
use axum::{
12-
async_trait,
1312
routing::{post, post_service},
1413
Json, Router,
1514
};
@@ -28,7 +27,7 @@ use crate::{
2827
dips::{AgreementStore, InMemoryAgreementStore},
2928
},
3029
routes::dips::Price,
31-
service::indexer_service::{IndexerService, IndexerServiceOptions, IndexerServiceRelease},
30+
service::indexer_service::{IndexerServiceOptions, IndexerServiceRelease},
3231
};
3332
use clap::Parser;
3433
use tracing::error;
@@ -37,13 +36,12 @@ mod indexer_service;
3736
mod tap_receipt_header;
3837

3938
pub use indexer_service::{
40-
AttestationOutput, IndexerServiceError, IndexerServiceImpl, IndexerServiceResponse,
41-
IndexerServiceState,
39+
AttestationOutput, IndexerServiceError, IndexerServiceResponse, IndexerServiceState,
4240
};
4341
pub use tap_receipt_header::TapReceipt;
4442

4543
#[derive(Debug)]
46-
struct SubgraphServiceResponse {
44+
pub struct SubgraphServiceResponse {
4745
inner: String,
4846
attestable: bool,
4947
}
@@ -78,6 +76,7 @@ impl IndexerServiceResponse for SubgraphServiceResponse {
7876
}
7977
}
8078

79+
#[derive(Clone)]
8180
pub struct SubgraphServiceState {
8281
pub config: &'static Config,
8382
pub database: PgPool,
@@ -87,27 +86,22 @@ pub struct SubgraphServiceState {
8786
pub graph_node_query_base_url: &'static Url,
8887
}
8988

90-
struct SubgraphService {
91-
state: Arc<SubgraphServiceState>,
89+
pub struct SubgraphService {
90+
state: SubgraphServiceState,
9291
}
9392

9493
impl SubgraphService {
95-
fn new(state: Arc<SubgraphServiceState>) -> Self {
94+
fn new(state: SubgraphServiceState) -> Self {
9695
Self { state }
9796
}
9897
}
9998

100-
#[async_trait]
101-
impl IndexerServiceImpl for SubgraphService {
102-
type Error = SubgraphServiceError;
103-
type Response = SubgraphServiceResponse;
104-
type State = SubgraphServiceState;
105-
106-
async fn process_request<Request: DeserializeOwned + Send + std::fmt::Debug + Serialize>(
99+
impl SubgraphService {
100+
pub async fn process_request<Request: DeserializeOwned + Send + std::fmt::Debug + Serialize>(
107101
&self,
108102
deployment: DeploymentId,
109103
request: Request,
110-
) -> Result<Self::Response, Self::Error> {
104+
) -> Result<SubgraphServiceResponse, SubgraphServiceError> {
111105
let deployment_url = self
112106
.state
113107
.graph_node_query_base_url
@@ -166,7 +160,7 @@ pub async fn run() -> anyhow::Result<()> {
166160
// Some of the subgraph service configuration goes into the so-called
167161
// "state", which will be passed to any request handler, middleware etc.
168162
// that is involved in serving requests
169-
let state = Arc::new(SubgraphServiceState {
163+
let state = SubgraphServiceState {
170164
config,
171165
database: database::connect(config.database.clone().get_formated_postgres_url().as_ref())
172166
.await,
@@ -178,7 +172,7 @@ pub async fn run() -> anyhow::Result<()> {
178172
.expect("Failed to init HTTP client for Graph Node"),
179173
graph_node_status_url: &config.graph_node.status_url,
180174
graph_node_query_base_url: &config.graph_node.query_url,
181-
});
175+
};
182176

183177
let agreement_store: Arc<dyn AgreementStore> = Arc::new(InMemoryAgreementStore::default());
184178
let prices: Vec<Price> = vec![];
@@ -215,7 +209,7 @@ pub async fn run() -> anyhow::Result<()> {
215209
router = router.route("/dips", post_service(GraphQL::new(schema)));
216210
}
217211

218-
IndexerService::run(IndexerServiceOptions {
212+
indexer_service::run(IndexerServiceOptions {
219213
release,
220214
config,
221215
url_namespace: "subgraphs",

0 commit comments

Comments
 (0)