Skip to content

Commit 24b558c

Browse files
refactor: use graphql-client for status monitor (#486)
1 parent 552d16b commit 24b558c

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

crates/common/src/subgraph_client/monitor.rs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,30 @@
33

44
use std::time::Duration;
55

6+
use deployment_status_query::Health;
7+
use graphql_client::GraphQLQuery;
68
use reqwest::Url;
79
use serde::Deserialize;
8-
use serde_json::json;
910
use thegraph_core::DeploymentId;
10-
use thegraph_graphql_http::{
11-
http::request::IntoRequestParameters,
12-
http_client::{ReqwestExt, ResponseResult},
13-
};
1411
use tokio::sync::watch::Receiver;
1512

1613
use crate::watcher::new_watcher;
1714

18-
use super::Query;
19-
20-
#[derive(Deserialize)]
21-
#[serde(rename_all = "camelCase")]
22-
struct DeploymentStatusResponse {
23-
indexing_statuses: Vec<DeploymentStatus>,
24-
}
15+
#[derive(GraphQLQuery)]
16+
#[graphql(
17+
schema_path = "../graphql/indexing_status.schema.graphql",
18+
query_path = "../graphql/subgraph_deployment_status.graphql",
19+
response_derives = "Debug",
20+
variables_derives = "Clone"
21+
)]
22+
pub struct DeploymentStatusQuery;
2523

2624
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
2725
pub struct DeploymentStatus {
2826
pub synced: bool,
2927
pub health: String,
3028
}
3129

32-
async fn query<T: for<'de> Deserialize<'de>>(
33-
url: Url,
34-
query: impl IntoRequestParameters + Send,
35-
) -> Result<ResponseResult<T>, anyhow::Error> {
36-
Ok(reqwest::Client::new().post(url).send_graphql(query).await?)
37-
}
38-
3930
pub async fn monitor_deployment_status(
4031
deployment: DeploymentId,
4132
status_url: Url,
@@ -50,28 +41,28 @@ pub async fn check_deployment_status(
5041
deployment: DeploymentId,
5142
status_url: Url,
5243
) -> Result<DeploymentStatus, anyhow::Error> {
53-
let body = Query::new_with_variables(
54-
r#"
55-
query indexingStatuses($ids: [String!]!) {
56-
indexingStatuses(subgraphs: $ids) {
57-
synced
58-
health
59-
}
60-
}
61-
"#,
62-
[("ids", json!([deployment.to_string()]))],
63-
);
64-
65-
let response = query::<DeploymentStatusResponse>(status_url, body).await?;
66-
67-
match response {
68-
Ok(deployment_status) => deployment_status
44+
let req_body = DeploymentStatusQuery::build_query(deployment_status_query::Variables {
45+
ids: vec![deployment.to_string()],
46+
});
47+
let client = reqwest::Client::new();
48+
let response = client.post(status_url).json(&req_body).send().await?;
49+
let graphql_response: graphql_client::Response<deployment_status_query::ResponseData> =
50+
response.json().await?;
51+
match graphql_response.data {
52+
Some(data) => data
6953
.indexing_statuses
7054
.first()
71-
.cloned()
55+
.map(|status| DeploymentStatus {
56+
synced: status.synced,
57+
health: match status.health {
58+
Health::healthy => "healthy".to_owned(),
59+
Health::unhealthy => "unhealthy".to_owned(),
60+
_ => "failed".to_owned(),
61+
},
62+
})
7263
.ok_or_else(|| anyhow::anyhow!("Deployment `{deployment}` not found")),
73-
Err(e) => Err(anyhow::anyhow!(
74-
"Failed to query status of deployment `{deployment}`: {e}"
64+
None => Err(anyhow::anyhow!(
65+
"Failed to query status of deployment `{deployment}`"
7566
)),
7667
}
7768
}
@@ -80,6 +71,7 @@ pub async fn check_deployment_status(
8071
mod tests {
8172
use std::str::FromStr;
8273

74+
use serde_json::json;
8375
use wiremock::matchers::{method, path};
8476
use wiremock::{Mock, MockServer, ResponseTemplate};
8577

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
query DeploymentStatusQuery($ids: [String!]!) {
2+
indexingStatuses(subgraphs: $ids) {
3+
synced
4+
health
5+
}
6+
}

0 commit comments

Comments
 (0)