Skip to content

Commit 06c26b7

Browse files
committed
refactor: only support a query URL in SubgraphClient
1 parent 1bd7650 commit 06c26b7

File tree

5 files changed

+39
-70
lines changed

5 files changed

+39
-70
lines changed

common/src/allocations/monitor.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ pub fn indexer_allocations(
189189

190190
#[cfg(test)]
191191
mod test {
192+
use reqwest::Url;
192193
use serde_json::json;
193194
use wiremock::{
194195
matchers::{body_string_contains, method, path},
@@ -202,17 +203,14 @@ mod test {
202203
async fn setup_mock_network_subgraph() -> (&'static SubgraphClient, MockServer) {
203204
// Set up a mock network subgraph
204205
let mock_server = MockServer::start().await;
205-
let network_subgraph_endpoint = SubgraphClient::local_deployment_endpoint(
206+
let network_subgraph_endpoint = Url::parse(&format!(
207+
"{}/subgraphs/id/{}",
206208
&mock_server.uri(),
207-
&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT,
208-
)
209-
.unwrap();
210-
let network_subgraph = SubgraphClient::new(
211-
Some(&mock_server.uri()),
212-
Some(&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT),
213-
network_subgraph_endpoint.as_ref(),
214-
)
209+
*test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT
210+
))
215211
.unwrap();
212+
let network_subgraph =
213+
SubgraphClient::new("network-subgraph", network_subgraph_endpoint.as_ref()).unwrap();
216214

217215
// Mock result for current epoch requests
218216
mock_server

common/src/attestations/dispute_manager.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub fn dispute_manager(
8181

8282
#[cfg(test)]
8383
mod test {
84+
use reqwest::Url;
8485
use serde_json::json;
8586
use wiremock::{
8687
matchers::{method, path},
@@ -97,17 +98,14 @@ mod test {
9798
async fn setup_mock_network_subgraph() -> (&'static SubgraphClient, MockServer) {
9899
// Set up a mock network subgraph
99100
let mock_server = MockServer::start().await;
100-
let network_subgraph_endpoint = SubgraphClient::local_deployment_endpoint(
101+
let network_subgraph_endpoint = Url::parse(&format!(
102+
"{}/subgraphs/id/{}",
101103
&mock_server.uri(),
102-
&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT,
103-
)
104-
.unwrap();
105-
let network_subgraph = SubgraphClient::new(
106-
Some(&mock_server.uri()),
107-
Some(&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT),
108-
network_subgraph_endpoint.as_ref(),
109-
)
104+
*test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT
105+
))
110106
.unwrap();
107+
let network_subgraph =
108+
SubgraphClient::new("network-subgraph", network_subgraph_endpoint.as_ref()).unwrap();
111109

112110
// Mock result for current epoch requests
113111
mock_server

common/src/escrow_accounts.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub fn escrow_accounts(
115115

116116
#[cfg(test)]
117117
mod tests {
118+
use reqwest::Url;
118119
use wiremock::matchers::{method, path};
119120
use wiremock::{Mock, MockServer, ResponseTemplate};
120121

@@ -126,18 +127,14 @@ mod tests {
126127
async fn test_current_accounts() {
127128
// Set up a mock escrow subgraph
128129
let mock_server = MockServer::start().await;
129-
let escrow_subgraph_endpoint = SubgraphClient::local_deployment_endpoint(
130+
let escrow_subgraph_endpoint = Url::parse(&format!(
131+
"{}/subgraphs/id/{}",
130132
&mock_server.uri(),
131-
&test_vectors::ESCROW_SUBGRAPH_DEPLOYMENT,
132-
)
133+
*test_vectors::ESCROW_SUBGRAPH_DEPLOYMENT
134+
))
133135
.unwrap();
134136
let escrow_subgraph = Box::leak(Box::new(
135-
SubgraphClient::new(
136-
Some(&mock_server.uri()),
137-
Some(&test_vectors::ESCROW_SUBGRAPH_DEPLOYMENT),
138-
escrow_subgraph_endpoint.as_ref(),
139-
)
140-
.unwrap(),
137+
SubgraphClient::new("escrow-subgraph", escrow_subgraph_endpoint.as_ref()).unwrap(),
141138
));
142139

143140
let mock = Mock::given(method("POST"))

common/src/subgraph_client.rs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use graphql::http::Response;
88
use reqwest::{header, Client, Url};
99
use serde::de::Deserialize;
1010
use serde_json::Value;
11-
use toolshed::thegraph::DeploymentId;
1211

1312
/// Network subgraph query wrapper
1413
///
@@ -20,48 +19,32 @@ pub struct SubgraphClient {
2019
}
2120

2221
impl SubgraphClient {
23-
pub fn new(
24-
graph_node_query_endpoint: Option<&str>,
25-
deployment: Option<&DeploymentId>,
26-
subgraph_url: &str,
27-
) -> Result<Self, anyhow::Error> {
28-
// TODO: Check indexing status of the local subgraph deployment
29-
// if the deployment is healthy and synced, use local_subgraoh_endpoint
30-
let _local_subgraph_endpoint = match (graph_node_query_endpoint, deployment) {
31-
(Some(endpoint), Some(id)) => Some(Self::local_deployment_endpoint(endpoint, id)?),
32-
_ => None,
33-
};
34-
35-
let subgraph_url = Url::parse(subgraph_url)
36-
.map_err(|e| anyhow!("Could not parse subgraph url `{}`: {}", subgraph_url, e))?;
22+
pub fn new(name: &str, query_url: &str) -> Result<Self, anyhow::Error> {
23+
let query_url = Url::parse(query_url).map_err(|e| {
24+
anyhow!(
25+
"Could not parse `{}` subgraph query URL `{}`: {}",
26+
name,
27+
query_url,
28+
e
29+
)
30+
})?;
3731

3832
let client = reqwest::Client::builder()
3933
.user_agent("indexer-common")
4034
.build()
41-
.expect("Could not build a client for the Graph Node query endpoint");
35+
.map_err(|err| {
36+
anyhow!(
37+
"Could not build a client for `{name}` subgraph query URL `{query_url}`: {err}"
38+
)
39+
})
40+
.expect("Building subgraph client");
4241

4342
Ok(Self {
4443
client,
45-
subgraph_url: Arc::new(subgraph_url),
44+
subgraph_url: Arc::new(query_url),
4645
})
4746
}
4847

49-
pub fn local_deployment_endpoint(
50-
graph_node_query_endpoint: &str,
51-
deployment: &DeploymentId,
52-
) -> Result<Url, anyhow::Error> {
53-
Url::parse(graph_node_query_endpoint)
54-
.and_then(|u| u.join("/subgraphs/id/"))
55-
.and_then(|u| u.join(&deployment.to_string()))
56-
.map_err(|e| {
57-
anyhow!(
58-
"Could not parse Graph Node query endpoint for subgraph deployment `{}`: {}",
59-
deployment,
60-
e
61-
)
62-
})
63-
}
64-
6548
pub async fn query<T: for<'de> Deserialize<'de>>(
6649
&self,
6750
body: &Value,
@@ -117,12 +100,7 @@ mod test {
117100
}
118101

119102
fn network_subgraph_client() -> SubgraphClient {
120-
SubgraphClient::new(
121-
Some(GRAPH_NODE_STATUS_ENDPOINT),
122-
Some(&test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT),
123-
NETWORK_SUBGRAPH_URL,
124-
)
125-
.unwrap()
103+
SubgraphClient::new("network-subgraph", NETWORK_SUBGRAPH_URL).unwrap()
126104
}
127105

128106
#[tokio::test]

service/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ async fn main() -> Result<(), std::io::Error> {
7070
// no problem.
7171
let network_subgraph = Box::leak(Box::new(
7272
SubgraphClient::new(
73-
Some(&config.indexer_infrastructure.graph_node_query_endpoint),
74-
config.network_subgraph.network_subgraph_deployment.as_ref(),
73+
"network-subgraph",
7574
&config.network_subgraph.network_subgraph_endpoint,
7675
)
7776
.expect("Failed to set up network subgraph client"),
@@ -108,8 +107,7 @@ async fn main() -> Result<(), std::io::Error> {
108107

109108
let escrow_subgraph = Box::leak(Box::new(
110109
SubgraphClient::new(
111-
Some(&config.indexer_infrastructure.graph_node_query_endpoint),
112-
config.escrow_subgraph.escrow_subgraph_deployment.as_ref(),
110+
"escrow-subgraph",
113111
&config.escrow_subgraph.escrow_subgraph_endpoint,
114112
)
115113
.expect("Failed to set up escrow subgraph client"),

0 commit comments

Comments
 (0)