diff --git a/service/src/main.rs b/service/src/main.rs index ccf69b4c..6c0dfd8c 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -62,14 +62,14 @@ async fn main() -> Result<(), std::io::Error> { // Make an instance of network subgraph at either // graph_node_query_endpoint/subgraphs/id/network_subgraph_deployment // or network_subgraph_endpoint - let network_subgraph = NetworkSubgraph::new( + let network_subgraph: &'static NetworkSubgraph = Box::leak(Box::new(NetworkSubgraph::new( Some(&config.indexer_infrastructure.graph_node_query_endpoint), config .network_subgraph .network_subgraph_deployment .as_deref(), &config.network_subgraph.network_subgraph_endpoint, - ); + ))); let indexer_allocations = indexer_allocations( &network_subgraph, @@ -79,7 +79,7 @@ async fn main() -> Result<(), std::io::Error> { ); let attestation_signers = attestation_signers( - indexer_allocations, + indexer_allocations.clone(), config.ethereum.mnemonic.clone(), // TODO: Chain ID should be a config U256::from(1), @@ -136,7 +136,7 @@ async fn main() -> Result<(), std::io::Error> { config.indexer_infrastructure.graph_node_status_endpoint, indexer_management_db, public_key(&config.ethereum.mnemonic).expect("Failed to initiate with operator wallet"), - network_subgraph, + &network_subgraph, config.network_subgraph.network_subgraph_auth_token, config.network_subgraph.serve_network_subgraph, ); diff --git a/service/src/server/mod.rs b/service/src/server/mod.rs index b2241920..8d5cf9df 100644 --- a/service/src/server/mod.rs +++ b/service/src/server/mod.rs @@ -38,7 +38,7 @@ pub struct ServerOptions { pub graph_node_status_endpoint: String, pub indexer_management_db: PgPool, pub operator_public_key: String, - pub network_subgraph: NetworkSubgraph, + pub network_subgraph: &'static NetworkSubgraph, pub network_subgraph_auth_token: Option, pub serve_network_subgraph: bool, } @@ -53,7 +53,7 @@ impl ServerOptions { graph_node_status_endpoint: String, indexer_management_db: PgPool, operator_public_key: String, - network_subgraph: NetworkSubgraph, + network_subgraph: &'static NetworkSubgraph, network_subgraph_auth_token: Option, serve_network_subgraph: bool, ) -> Self { diff --git a/service/src/server/routes/network.rs b/service/src/server/routes/network.rs index 7190efcc..02fb3e9f 100644 --- a/service/src/server/routes/network.rs +++ b/service/src/server/routes/network.rs @@ -2,16 +2,16 @@ // SPDX-License-Identifier: Apache-2.0 use axum::{ - extract::{Extension, FromRequest}, - http::{self, Request, StatusCode}, + extract::Extension, + http::{self, Request}, response::IntoResponse, Json, }; -use serde_json::Value; +use serde_json::{json, Value}; use crate::server::ServerOptions; -use super::{bad_request_response, response_body_to_query_string}; +use super::bad_request_response; pub async fn network_queries( Extension(server): Extension, @@ -33,31 +33,12 @@ pub async fn network_queries( return bad_request_response("Not enabled or authorized query"); } - // Serve query using query processor - // let req_body = req.into_body(); - // let query_string = match response_body_to_query_string(req_body).await { - // Ok(q) => q, - // Err(e) => return bad_request_response(&e.to_string()), - // }; - - match server.network_subgraph.query(&body).await { - Ok(result) => {} + match server.network_subgraph.query::(&body).await { + Ok(result) => Json(json!({ + "data": result.data, + "errors": result.errors.map(|errors| errors.into_iter().map(|e| json!({ "message": e.message })).collect::>()), + })) + .into_response(), Err(e) => bad_request_response(&e.to_string()), } - - //if response.status().is_success() { - // ( - // StatusCode::OK, - // Json( - // response - // .json::() - // .await - // // FIXME: Don't use expect here - // .expect("Failed to parse network subgraph query result"), - // ), - // ) - // .into_response() - //} else { - // bad_request_response("Bad response from Graph node") - //} }