Skip to content

Commit

Permalink
chore(graph-gateway): use graphql-http crate client
Browse files Browse the repository at this point in the history
  • Loading branch information
LNSD committed Nov 14, 2023
1 parent 93fcb2c commit 4a616a7
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 174 deletions.
30 changes: 22 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions graph-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ faster-hex = "0.8.0"
futures = "0.3"
graph-subscriptions = { git = "https://github.com/edgeandnode/subscription-payments", rev = "334d18b" }
graphql = { git = "https://github.com/edgeandnode/toolshed", tag = "graphql-v0.1.0" }
graphql-http = { git = "https://github.com/edgeandnode/toolshed.git", tag = "graphql-http-v0.1.0", features = ["http-reqwest"] }
hex = "0.4"
indexer-selection = { path = "../indexer-selection" }
indoc = "2.0.3"
Expand Down
2 changes: 1 addition & 1 deletion graph-gateway/src/indexers_status.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod graphql;
pub mod cost_models;
pub mod indexing_statuses;
pub mod public_poi;
4 changes: 4 additions & 0 deletions graph-gateway/src/indexers_status/cost_models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub use query::*;

pub mod client;
mod query;
16 changes: 16 additions & 0 deletions graph-gateway/src/indexers_status/cost_models/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use graphql_http::http_client::ReqwestExt;
use toolshed::url::Url;

use crate::indexers_status::cost_models::query;

pub async fn send_cost_model_query(
client: reqwest::Client,
status_url: Url,
query: query::CostModelQuery,
) -> anyhow::Result<query::CostModelResponse> {
let res = client.post(status_url.0).send_graphql(query).await;
match res {
Ok(res) => Ok(res?),
Err(e) => Err(anyhow::anyhow!("Error sending cost model query: {}", e)),
}
}
43 changes: 43 additions & 0 deletions graph-gateway/src/indexers_status/cost_models/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use graphql_http::graphql::{Document, IntoDocument, IntoDocumentWithVariables};
use indoc::indoc;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use toolshed::thegraph::DeploymentId;

pub(super) const COST_MODEL_QUERY_DOCUMENT: &str = indoc! {
r#"query ($deployments: [String!]!) {
costModels(deployments: $deployments) {
deployment
model
variables
}
}"#
};

#[serde_as]
#[derive(Clone, Debug, Serialize)]
pub struct CostModelQuery {
#[serde_as(as = "Vec<DisplayFromStr>")]
pub deployments: Vec<DeploymentId>,
}

impl IntoDocumentWithVariables for CostModelQuery {
type Variables = Self;

fn into_document_with_variables(self) -> (Document, Self::Variables) {
(COST_MODEL_QUERY_DOCUMENT.into_document(), self)
}
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CostModelResponse {
pub cost_models: Vec<CostModelSourceResponse>,
}

#[derive(Deserialize)]
pub struct CostModelSourceResponse {
pub deployment: DeploymentId,
pub model: String,
pub variables: Option<String>,
}
41 changes: 0 additions & 41 deletions graph-gateway/src/indexers_status/graphql.rs

This file was deleted.

14 changes: 12 additions & 2 deletions graph-gateway/src/indexers_status/indexing_statuses/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use graphql_http::http_client::ReqwestExt;
use toolshed::url::Url;

use crate::indexers_status::graphql;
use crate::indexers_status::indexing_statuses::query;

pub async fn send_indexing_statuses_query(
client: reqwest::Client,
status_url: Url,
) -> anyhow::Result<query::IndexingStatusesResponse> {
graphql::send_graphql_query(&client, status_url, query::IndexingStatusesQuery).await
let res = client
.post(status_url.0)
.send_graphql(query::INDEXING_STATUSES_QUERY_DOCUMENT)
.await;
match res {
Ok(res) => Ok(res?),
Err(e) => Err(anyhow::anyhow!(
"Error sending indexing statuses query: {}",
e
)),
}
}
37 changes: 17 additions & 20 deletions graph-gateway/src/indexers_status/indexing_statuses/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@ use indoc::indoc;
use serde::{Deserialize, Deserializer};
use toolshed::thegraph::DeploymentId;

use crate::indexers_status::graphql::IntoGraphqlQuery;

#[derive(Clone, Debug)]
pub struct IndexingStatusesQuery;

impl IntoGraphqlQuery for IndexingStatusesQuery {
fn to_query(&self) -> String {
String::from(indoc! {
r#"{
indexingStatuses(subgraphs: []) {
subgraph
chains {
network
latestBlock { number hash }
earliestBlock { number hash }
}
pub(super) const INDEXING_STATUSES_QUERY_DOCUMENT: &str = indoc! {
r#"{
indexingStatuses(subgraphs: []) {
subgraph
chains {
network
latestBlock {
number
hash
}
}"#
})
}
}
earliestBlock {
number
hash
}
}
}
}"#
};

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
11 changes: 9 additions & 2 deletions graph-gateway/src/indexers_status/public_poi/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;

use alloy_primitives::BlockNumber;
use graphql_http::http_client::ReqwestExt;
use itertools::Itertools;
use toolshed::thegraph::DeploymentId;
use toolshed::url::Url;

use crate::indexers_status::graphql;
use crate::indexers_status::public_poi::query;
use crate::poi::ProofOfIndexing;

Expand All @@ -14,7 +14,14 @@ pub async fn send_public_poi_query(
status_url: Url,
query: query::PublicProofOfIndexingQuery,
) -> anyhow::Result<query::PublicProofOfIndexingResponse> {
graphql::send_graphql_query(&client, status_url, query).await
let res = client.post(status_url.0).send_graphql(query).await;
match res {
Ok(res) => Ok(res?),
Err(e) => Err(anyhow::anyhow!(
"Error sending public proof of indexing query: {}",
e
)),
}
}

pub async fn send_public_poi_queries_and_merge_results(
Expand Down
Loading

0 comments on commit 4a616a7

Please sign in to comment.