Skip to content

Commit

Permalink
Merge pull request #27 from graphops/aasseman/network_query_vars
Browse files Browse the repository at this point in the history
feat: network_query w/ gql and vars
  • Loading branch information
aasseman authored Aug 15, 2023
2 parents f6eb85d + 32dda65 commit ce19e8c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
36 changes: 30 additions & 6 deletions service/src/graph_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use reqwest::{header, Client, Url};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::query_processor::UnattestedQueryResult;

Expand All @@ -11,6 +13,13 @@ pub struct GraphNodeInstance {
base_url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct GraphQLQuery {
pub query: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<Value>,
}

impl GraphNodeInstance {
pub fn new(base_url: &str) -> GraphNodeInstance {
let client = reqwest::Client::builder()
Expand All @@ -23,15 +32,15 @@ impl GraphNodeInstance {
}
}

pub async fn subgraph_query(
pub async fn subgraph_query_raw(
&self,
endpoint: &str,
data: String,
body: String,
) -> Result<UnattestedQueryResult, reqwest::Error> {
let request = self
.client
.post(format!("{}/subgraphs/id/{}", self.base_url, endpoint))
.body(data)
.body(body)
.header(header::CONTENT_TYPE, "application/json");

let response = request.send().await?;
Expand All @@ -46,15 +55,15 @@ impl GraphNodeInstance {
})
}

pub async fn network_query(
pub async fn network_query_raw(
&self,
endpoint: Url,
data: String,
body: String,
) -> Result<UnattestedQueryResult, reqwest::Error> {
let request = self
.client
.post(endpoint)
.body(data.clone())
.body(body.clone())
.header(header::CONTENT_TYPE, "application/json");

let response = request.send().await?;
Expand All @@ -66,4 +75,19 @@ impl GraphNodeInstance {
attestable: false,
})
}

pub async fn network_query(
&self,
endpoint: Url,
query: String,
variables: Option<Value>,
) -> Result<UnattestedQueryResult, reqwest::Error> {
let body = GraphQLQuery { query, variables };

self.network_query_raw(
endpoint,
serde_json::to_string(&body).expect("serialize network GraphQL query"),
)
.await
}
}
6 changes: 3 additions & 3 deletions service/src/query_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl QueryProcessor {
) -> Result<Response<UnattestedQueryResult>, QueryError> {
let response = self
.graph_node
.subgraph_query(&query.subgraph_deployment_id.ipfs_hash(), query.query)
.subgraph_query_raw(&query.subgraph_deployment_id.ipfs_hash(), query.query)
.await?;

Ok(Response {
Expand All @@ -191,7 +191,7 @@ impl QueryProcessor {
) -> Result<Response<UnattestedQueryResult>, QueryError> {
let response = self
.graph_node
.network_query(self.network_subgraph.clone(), query)
.network_query_raw(self.network_subgraph.clone(), query)
.await?;

Ok(Response {
Expand Down Expand Up @@ -227,7 +227,7 @@ impl QueryProcessor {

let response = self
.graph_node
.subgraph_query(&subgraph_deployment_id.ipfs_hash(), query.clone())
.subgraph_query_raw(&subgraph_deployment_id.ipfs_hash(), query.clone())
.await?;

let attestation_signature = response.attestable.then(|| {
Expand Down

0 comments on commit ce19e8c

Please sign in to comment.