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 Oct 31, 2023
1 parent 260b403 commit af1f37f
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 319 deletions.
344 changes: 193 additions & 151 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions graph-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ name = "graph-gateway"
version = "14.0.1"

[dependencies]
anyhow.workspace = true
alloy-primitives.workspace = true
alloy-sol-types = "0.3.2"
anyhow.workspace = true
axum = { version = "0.6.15", default-features = false, features = [
"json",
"tokio",
Expand All @@ -18,8 +18,9 @@ eventuals.workspace = true
faster-hex = "0.8.0"
futures = "0.3"
futures-util = "0.3"
graphql = { git = "https://github.com/edgeandnode/toolshed", tag = "graphql-v0.1.0" }
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", rev = "fcffcf8", 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
122 changes: 59 additions & 63 deletions graph-gateway/src/indexers_status/public_poi/query.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
use alloy_primitives::{BlockNumber, B256};
use graphql_http::graphql::{Document, IntoDocument, IntoDocumentWithVariables};
use indoc::indoc;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use toolshed::thegraph::DeploymentId;

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

pub const MAX_REQUESTS_PER_QUERY: usize = 10;

pub type ProofOfIndexing = B256;

#[derive(Clone, Debug)]
pub(super) const PUBLIC_PROOF_OF_INDEXING_QUERY_DOCUMENT: &str = indoc! {
r#"query ($requests: [PublicProofOfIndexingRequest!]!) {
publicProofsOfIndexing(requests: $requests) {
deployment
proofOfIndexing
block {
number
}
}
}"#
};

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicProofOfIndexingQuery {
pub requests: Vec<PublicProofOfIndexingRequest>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicProofOfIndexingRequest {
pub deployment: DeploymentId,
pub block_number: BlockNumber,
}

impl PublicProofOfIndexingRequest {
fn to_query_params(&self) -> String {
format!(
r#"{{ deployment: "{}", blockNumber: "{}" }}"#,
self.deployment, self.block_number
)
}
}
impl IntoDocumentWithVariables for PublicProofOfIndexingQuery {
type Variables = Self;

impl IntoGraphqlQuery for PublicProofOfIndexingQuery {
fn to_query(&self) -> String {
fn into_document_with_variables(self) -> (Document, Self::Variables) {
debug_assert!(!self.requests.is_empty(), "Must have at least one request");

let requests = self
.requests
.iter()
.map(|request| request.to_query_params())
.collect::<Vec<_>>()
.join(", ");

format!(
indoc! {
r#"{{
publicProofsOfIndexing(requests: [{}]) {{
deployment
proofOfIndexing
block {{ number }}
}}
}}"#
},
requests
(
PUBLIC_PROOF_OF_INDEXING_QUERY_DOCUMENT.into_document(),
self,
)
}
}
Expand Down Expand Up @@ -82,30 +73,13 @@ mod tests {
use super::*;

mod query {
use super::*;

#[test]
fn public_proof_of_indexing_request_to_query_params_format() {
//// Given
let request = PublicProofOfIndexingRequest {
deployment: "QmawxQJ5U1JvgosoFVDyAwutLWxrckqVmBTQxaMaKoj3Lw"
.parse()
.expect("Failed to parse deployment ID"),
block_number: 123,
};

//// When
let query = request.to_query_params();
use graphql_http::http::request::IntoRequestParameters;
use serde_json::json;

//// Then
assert_eq!(
query.as_str(),
"{ deployment: \"QmawxQJ5U1JvgosoFVDyAwutLWxrckqVmBTQxaMaKoj3Lw\", blockNumber: \"123\" }"
);
}
use super::*;

#[test]
fn create_status_public_pois_query() {
fn create_status_public_pois_request_params() {
//// Given
let query = PublicProofOfIndexingQuery {
requests: vec![
Expand All @@ -125,18 +99,40 @@ mod tests {
};

//// When
let query = query.to_query();
let query = query.into_request_parameters();

//// Then
// Assert the query document.
assert_eq!(
query.query.as_str(),
PUBLIC_PROOF_OF_INDEXING_QUERY_DOCUMENT
);

// Assert the query variables.
assert!(matches!(
query.variables.get("requests"),
Some(serde_json::Value::Array(_))
));

let var_requests = query
.variables
.get("requests")
.expect("Missing requests variables")
.as_array()
.expect("Invalid requests variables");
assert_eq!(
var_requests[0],
json!({
"deployment": "QmawxQJ5U1JvgosoFVDyAwutLWxrckqVmBTQxaMaKoj3Lw",
"blockNumber": 123
})
);
assert_eq!(
query.as_str(),
indoc! { r#"{
publicProofsOfIndexing(requests: [{ deployment: "QmawxQJ5U1JvgosoFVDyAwutLWxrckqVmBTQxaMaKoj3Lw", blockNumber: "123" }, { deployment: "QmeYTH2fK2wv96XvnCGH2eyKFE8kmRfo53zYVy5dKysZtH", blockNumber: "456" }]) {
deployment
proofOfIndexing
block { number }
}
}"# }
var_requests[1],
json!({
"deployment": "QmeYTH2fK2wv96XvnCGH2eyKFE8kmRfo53zYVy5dKysZtH",
"blockNumber": 456
})
);
}
}
Expand Down
Loading

0 comments on commit af1f37f

Please sign in to comment.