From 0c02b88971474da1147ffd1ca8565fefbe2c9378 Mon Sep 17 00:00:00 2001 From: Alexis Asseman Date: Tue, 8 Aug 2023 16:09:56 -0700 Subject: [PATCH 1/3] fix: subgraph queries responses --- service/src/graph_node.rs | 13 +++++++++---- service/src/query_processor.rs | 14 ++++++++++---- service/src/server/routes/network.rs | 14 +------------- service/src/server/routes/subgraphs.rs | 16 ++-------------- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/service/src/graph_node.rs b/service/src/graph_node.rs index 439c5a85..28942b01 100644 --- a/service/src/graph_node.rs +++ b/service/src/graph_node.rs @@ -31,10 +31,15 @@ impl GraphNodeInstance { .body(data.clone()) .header(header::CONTENT_TYPE, "application/json"); - let response = request.send().await?.text().await?; + let response = request.send().await?; + let attestable = response + .headers() + .get("graph-attestable") + .map_or(false, |v| v == "true"); + Ok(UnattestedQueryResult { - graphql_response: response, - attestable: true, + graphQLResponse: response.text().await?, + attestable, }) } @@ -54,7 +59,7 @@ impl GraphNodeInstance { // actually parse the JSON for the graphQL schema let response_text = response.text().await?; Ok(UnattestedQueryResult { - graphql_response: response_text, + graphQLResponse: response_text, attestable: false, }) } diff --git a/service/src/query_processor.rs b/service/src/query_processor.rs index dcd8e139..632b10b1 100644 --- a/service/src/query_processor.rs +++ b/service/src/query_processor.rs @@ -80,20 +80,25 @@ impl ToString for SubgraphDeploymentID { self.hex() } } + +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Signature { v: i64, r: String, s: String, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[allow(non_snake_case)] // Need exact field names for JSON response pub struct QueryResult { - graphql_response: String, - attestation: Option, + pub graphQLResponse: String, + pub attestation: Option, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[allow(non_snake_case)] // Need exact field names for JSON response pub struct UnattestedQueryResult { - pub graphql_response: String, + pub graphQLResponse: String, pub attestable: bool, } @@ -109,6 +114,7 @@ pub struct Response { #[derive(Debug)] pub struct FreeQuery { pub subgraph_deployment_id: SubgraphDeploymentID, + // TODO: Use Arc instead of String? pub query: String, } diff --git a/service/src/server/routes/network.rs b/service/src/server/routes/network.rs index d8ca345f..73076e74 100644 --- a/service/src/server/routes/network.rs +++ b/service/src/server/routes/network.rs @@ -4,7 +4,6 @@ use axum::{ response::IntoResponse, Json, }; -use hyper::http::HeaderName; use crate::server::ServerOptions; @@ -43,18 +42,7 @@ pub async fn network_queries( .expect("Failed to execute free network subgraph query"); match request.status { - 200 => { - let response_body = request.result.graphql_response; - ( - StatusCode::OK, - axum::response::AppendHeaders([( - HeaderName::from_static("graph-attestable"), - "false", - )]), - Json(response_body), - ) - .into_response() - } + 200 => (StatusCode::OK, Json(request.result)).into_response(), _ => bad_request_response("Bad response from Graph node"), } } diff --git a/service/src/server/routes/subgraphs.rs b/service/src/server/routes/subgraphs.rs index e5e2f0c9..3809800c 100644 --- a/service/src/server/routes/subgraphs.rs +++ b/service/src/server/routes/subgraphs.rs @@ -1,6 +1,6 @@ use axum::{ extract::Extension, - http::{self, HeaderName, Request, StatusCode}, + http::{self, Request, StatusCode}, response::IntoResponse, Json, }; @@ -69,19 +69,7 @@ pub async fn subgraph_queries( .expect("Failed to execute free query"); match res.status { - 200 => { - let response_body = res.result.graphql_response; - let attestable = res.result.attestable; - ( - StatusCode::OK, - axum::response::AppendHeaders([( - HeaderName::from_static("graph-attestable"), - if attestable { "true" } else { "false" }, - )]), - Json(response_body), - ) - .into_response() - } + 200 => (StatusCode::OK, Json(res.result)).into_response(), _ => bad_request_response("Bad response from Graph node"), } } else { From 80cdf6c1eedcd0262203c8c4c9d1a85be568f570 Mon Sep 17 00:00:00 2001 From: Alexis Asseman Date: Wed, 9 Aug 2023 13:16:30 -0700 Subject: [PATCH 2/3] refactor: snake case QueryResult using serde rename --- service/src/graph_node.rs | 4 ++-- service/src/query_processor.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/service/src/graph_node.rs b/service/src/graph_node.rs index 28942b01..2bac3aaf 100644 --- a/service/src/graph_node.rs +++ b/service/src/graph_node.rs @@ -38,7 +38,7 @@ impl GraphNodeInstance { .map_or(false, |v| v == "true"); Ok(UnattestedQueryResult { - graphQLResponse: response.text().await?, + graphql_response: response.text().await?, attestable, }) } @@ -59,7 +59,7 @@ impl GraphNodeInstance { // actually parse the JSON for the graphQL schema let response_text = response.text().await?; Ok(UnattestedQueryResult { - graphQLResponse: response_text, + graphql_response: response_text, attestable: false, }) } diff --git a/service/src/query_processor.rs b/service/src/query_processor.rs index 632b10b1..68a540c0 100644 --- a/service/src/query_processor.rs +++ b/service/src/query_processor.rs @@ -89,16 +89,16 @@ pub struct Signature { } #[derive(Debug, Clone, Serialize, Deserialize)] -#[allow(non_snake_case)] // Need exact field names for JSON response pub struct QueryResult { - pub graphQLResponse: String, + #[serde(rename = "graphQLResponse")] + pub graphql_response: String, pub attestation: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] -#[allow(non_snake_case)] // Need exact field names for JSON response pub struct UnattestedQueryResult { - pub graphQLResponse: String, + #[serde(rename = "graphQLResponse")] + pub graphql_response: String, pub attestable: bool, } From 8b9b531ea6b59ef34563dc18ca4f9590727890a1 Mon Sep 17 00:00:00 2001 From: Alexis Asseman Date: Wed, 9 Aug 2023 14:25:01 -0700 Subject: [PATCH 3/3] docs: remove TODO about Arc in FreeQuery --- service/src/query_processor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/service/src/query_processor.rs b/service/src/query_processor.rs index 68a540c0..1543e0b8 100644 --- a/service/src/query_processor.rs +++ b/service/src/query_processor.rs @@ -114,7 +114,6 @@ pub struct Response { #[derive(Debug)] pub struct FreeQuery { pub subgraph_deployment_id: SubgraphDeploymentID, - // TODO: Use Arc instead of String? pub query: String, }