44use super :: monitor:: { monitor_deployment_status, DeploymentStatus } ;
55use anyhow:: anyhow;
66use axum:: body:: Bytes ;
7- use eventuals:: Eventual ;
87use graphql_client:: GraphQLQuery ;
98use reqwest:: { header, Url } ;
109use serde_json:: { Map , Value } ;
@@ -13,6 +12,7 @@ use thegraph_graphql_http::{
1312 graphql:: { Document , IntoDocument } ,
1413 http:: request:: { IntoRequestParameters , RequestParameters } ,
1514} ;
15+ use tokio:: sync:: watch:: Receiver ;
1616use tracing:: warn;
1717
1818#[ derive( Clone ) ]
@@ -140,18 +140,24 @@ impl DeploymentDetails {
140140
141141struct DeploymentClient {
142142 pub http_client : reqwest:: Client ,
143- pub status : Option < Eventual < DeploymentStatus > > ,
143+ pub status : Option < Receiver < DeploymentStatus > > ,
144144 pub query_url : Url ,
145145}
146146
147147impl DeploymentClient {
148- pub fn new ( http_client : reqwest:: Client , details : DeploymentDetails ) -> Self {
148+ pub async fn new ( http_client : reqwest:: Client , details : DeploymentDetails ) -> Self {
149149 Self {
150150 http_client,
151- status : details
152- . deployment
153- . zip ( details. status_url )
154- . map ( |( deployment, url) | monitor_deployment_status ( deployment, url) ) ,
151+ status : match details. deployment . zip ( details. status_url ) {
152+ Some ( ( deployment, url) ) => Some (
153+ monitor_deployment_status ( deployment, url)
154+ . await
155+ . expect ( & format ! (
156+ "Failed to initialize monitoring for deployment {deployment}"
157+ ) ) ,
158+ ) ,
159+ None => None ,
160+ } ,
155161 query_url : details. query_url ,
156162 }
157163 }
@@ -161,7 +167,7 @@ impl DeploymentClient {
161167 variables : T :: Variables ,
162168 ) -> Result < ResponseResult < T :: ResponseData > , anyhow:: Error > {
163169 if let Some ( ref status) = self . status {
164- let deployment_status = status. value ( ) . await . expect ( "reading deployment status" ) ;
170+ let deployment_status = status. borrow ( ) ;
165171
166172 if !deployment_status. synced || & deployment_status. health != "healthy" {
167173 return Err ( anyhow ! (
@@ -198,7 +204,7 @@ impl DeploymentClient {
198204
199205 pub async fn query_raw ( & self , body : Bytes ) -> Result < reqwest:: Response , anyhow:: Error > {
200206 if let Some ( ref status) = self . status {
201- let deployment_status = status. value ( ) . await . expect ( "reading deployment status" ) ;
207+ let deployment_status = status. borrow ( ) ;
202208
203209 if !deployment_status. synced || & deployment_status. health != "healthy" {
204210 return Err ( anyhow ! (
@@ -226,14 +232,17 @@ pub struct SubgraphClient {
226232}
227233
228234impl SubgraphClient {
229- pub fn new (
235+ pub async fn new (
230236 http_client : reqwest:: Client ,
231237 local_deployment : Option < DeploymentDetails > ,
232238 remote_deployment : DeploymentDetails ,
233239 ) -> Self {
234240 Self {
235- local_client : local_deployment. map ( |d| DeploymentClient :: new ( http_client. clone ( ) , d) ) ,
236- remote_client : DeploymentClient :: new ( http_client, remote_deployment) ,
241+ local_client : match local_deployment {
242+ Some ( d) => Some ( DeploymentClient :: new ( http_client. clone ( ) , d) . await ) ,
243+ None => None ,
244+ } ,
245+ remote_client : DeploymentClient :: new ( http_client, remote_deployment) . await ,
237246 }
238247 }
239248
@@ -335,12 +344,13 @@ mod test {
335344 mock_server
336345 }
337346
338- fn network_subgraph_client ( ) -> SubgraphClient {
347+ async fn network_subgraph_client ( ) -> SubgraphClient {
339348 SubgraphClient :: new (
340349 reqwest:: Client :: new ( ) ,
341350 None ,
342351 DeploymentDetails :: for_query_url ( NETWORK_SUBGRAPH_URL ) . unwrap ( ) ,
343352 )
353+ . await
344354 }
345355
346356 #[ derive( GraphQLQuery ) ]
@@ -359,6 +369,7 @@ mod test {
359369
360370 // Check that the response is valid JSON
361371 let result = network_subgraph_client ( )
372+ . await
362373 . query :: < CurrentEpoch , _ > ( current_epoch:: Variables { } )
363374 . await
364375 . unwrap ( ) ;
@@ -447,6 +458,7 @@ mod test {
447458
448459 // Query the subgraph
449460 let data = client
461+ . await
450462 . query :: < UserQuery , _ > ( user_query:: Variables { } )
451463 . await
452464 . expect ( "Query should succeed" )
@@ -527,6 +539,7 @@ mod test {
527539
528540 // Query the subgraph
529541 let data = client
542+ . await
530543 . query :: < UserQuery , _ > ( user_query:: Variables { } )
531544 . await
532545 . expect ( "Query should succeed" )
@@ -607,6 +620,7 @@ mod test {
607620
608621 // Query the subgraph
609622 let data = client
623+ . await
610624 . query :: < UserQuery , _ > ( user_query:: Variables { } )
611625 . await
612626 . expect ( "Query should succeed" )
0 commit comments