Skip to content

Commit 22279bf

Browse files
committed
Another plugin
1 parent 0ef2138 commit 22279bf

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
use dashmap::DashMap;
22

3-
use crate::{hooks::on_subgraph_execute::{OnSubgraphExecuteEndPayload, OnSubgraphExecuteStartPayload, SubgraphExecutorResponse, SubgraphResponse}, plugin_trait::{ControlFlow, RouterPlugin}};
3+
use crate::{executors::dedupe::SharedResponse, hooks::on_subgraph_http_request::{OnSubgraphHttpRequestPayload, OnSubgraphHttpResponsePayload}, plugin_trait::{ControlFlow, RouterPlugin}};
44

5-
struct SubgraphResponseCachePlugin {
6-
cache: DashMap<String, SubgraphResponse<'static>>,
5+
pub struct SubgraphResponseCachePlugin {
6+
cache: DashMap<String, SharedResponse>,
77
}
88

99
impl RouterPlugin for SubgraphResponseCachePlugin {
10-
fn on_subgraph_execute<'exec>(
11-
&self,
12-
payload: OnSubgraphExecuteStartPayload<'exec>,
13-
) -> ControlFlow<'exec, OnSubgraphExecuteEndPayload<'exec>> {
10+
fn on_subgraph_http_request<'exec>(
11+
&'static self,
12+
payload: OnSubgraphHttpRequestPayload<'exec>,
13+
) -> ControlFlow<'exec, OnSubgraphHttpResponsePayload<'exec>> {
1414
let key = format!(
15-
"subgraph_response_cache:{}:{}:{:?}",
16-
payload.subgraph_name, payload.execution_request.operation_name.unwrap_or(""), payload.execution_request.variables
15+
"subgraph_response_cache:{}:{:?}",
16+
payload.execution_request.query, payload.execution_request.variables
1717
);
1818
if let Some(cached_response) = self.cache.get(&key) {
19-
*payload.response = Some(SubgraphExecutorResponse::RawResponse(cached_response));
20-
// Return early with the cached response
19+
// Here payload.response is Option
20+
// So it is bypassing the actual subgraph request
21+
*payload.response = Some(cached_response.clone());
2122
return ControlFlow::Continue;
22-
} else {
23-
ControlFlow::OnEnd(Box::new(move |payload: OnSubgraphExecuteEndPayload| {
24-
let cacheable = payload.response.errors.is_none_or(|errors| errors.is_empty());
25-
if cacheable {
26-
self.cache.insert(key, *payload.response);
27-
}
28-
ControlFlow::Continue
29-
}))
3023
}
24+
ControlFlow::OnEnd(Box::new(move |payload: OnSubgraphHttpResponsePayload| {
25+
// Here payload.response is not Option
26+
self.cache.insert(key, payload.response.clone());
27+
ControlFlow::Continue
28+
}))
3129
}
3230
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub mod on_execute;
22
pub mod on_schema_reload;
3-
pub mod on_subgraph_execute;
3+
pub mod on_subgraph_http_request;
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
use std::collections::HashMap;
22

3-
use bytes::Bytes;
43
use hive_router_query_planner::ast::operation::SubgraphFetchOperation;
4+
use http::{HeaderMap, Uri};
55
use ntex::web::HttpRequest;
66

7-
use crate::{executors::dedupe::SharedResponse, response::{graphql_error::GraphQLError, value::Value}};
7+
use crate::
8+
executors::dedupe::SharedResponse
9+
;
810

11+
pub struct OnSubgraphHttpRequestPayload<'exec> {
12+
pub router_http_request: &'exec HttpRequest,
13+
pub subgraph_name: &'exec str,
14+
// At this point, there is no point of mutating this
15+
pub execution_request: &'exec SubgraphExecutionRequest<'exec>,
916

17+
pub endpoint: &'exec mut Uri,
18+
// By default, it is POST
19+
pub method: &'exec mut http::Method,
20+
pub headers: &'exec mut HeaderMap,
21+
pub request_body: &'exec mut Vec<u8>,
1022

11-
pub struct OnSubgraphExecuteStartPayload<'exec> {
12-
pub router_http_request: &'exec HttpRequest,
13-
pub subgraph_name: &'exec str,
14-
// The node that initiates this subgraph execution
15-
pub execution_request: &'exec mut SubgraphExecutionRequest<'exec>,
16-
// This will be tricky to implement with the current structure,
17-
// but I'm sure we'll figure it out
18-
pub response: &'exec mut Option<SubgraphResponse<'exec>>,
23+
// Early response
24+
pub response: &'exec mut Option<SharedResponse>,
1925
}
2026

2127
pub struct SubgraphExecutionRequest<'exec> {
2228
pub query: &'exec str,
2329
// We can add the original operation here too
2430
pub operation: &'exec SubgraphFetchOperation,
25-
31+
2632
pub dedupe: bool,
2733
pub operation_name: Option<&'exec str>,
2834
pub variables: Option<HashMap<&'exec str, &'exec sonic_rs::Value>>,
2935
pub extensions: Option<HashMap<String, sonic_rs::Value>>,
3036
pub representations: Option<Vec<u8>>,
3137
}
3238

33-
pub struct SubgraphResponse<'exec> {
34-
pub data: Value<'exec>,
35-
pub errors: Option<Vec<GraphQLError>>,
36-
pub extensions: Option<HashMap<String, Value<'exec>>>,
37-
}
38-
39-
pub struct OnSubgraphExecuteEndPayload<'exec> {
39+
pub struct OnSubgraphHttpResponsePayload<'exec> {
4040
pub router_http_request: &'exec HttpRequest,
4141
pub subgraph_name: &'exec str,
4242
// The node that initiates this subgraph execution
4343
pub execution_request: &'exec SubgraphExecutionRequest<'exec>,
4444
// This will be tricky to implement with the current structure,
4545
// but I'm sure we'll figure it out
46-
pub response: &'exec SubgraphResponse<'exec>,
46+
pub response: &'exec mut SharedResponse,
4747
}

lib/executor/src/plugins/plugin_trait.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ use ntex::web::HttpResponse;
22

33
use crate::hooks::on_execute::OnExecutePayload;
44
use crate::hooks::on_schema_reload::OnSchemaReloadPayload;
5-
use crate::hooks::on_subgraph_execute::OnSubgraphExecuteEndPayload;
6-
use crate::hooks::on_subgraph_execute::OnSubgraphExecuteStartPayload;
5+
use crate::hooks::on_subgraph_http_request::{OnSubgraphHttpRequestPayload, OnSubgraphHttpResponsePayload};
76

8-
pub enum ControlFlow<'a, TPayload> {
7+
pub enum ControlFlow<'exec, TPayload> {
98
Continue,
109
Break(HttpResponse),
11-
OnEnd(Box<dyn FnOnce(TPayload) -> ControlFlow<'a, ()> + Send + 'a>),
10+
OnEnd(Box<dyn FnOnce(TPayload) -> ControlFlow<'exec, ()> + 'exec>),
1211
}
1312

1413
pub trait RouterPlugin {
@@ -18,10 +17,10 @@ pub trait RouterPlugin {
1817
) -> ControlFlow<'exec, OnExecutePayload<'exec>> {
1918
ControlFlow::Continue
2019
}
21-
fn on_subgraph_execute<'exec>(
22-
&self,
23-
_payload: OnSubgraphExecuteStartPayload<'exec>,
24-
) -> ControlFlow<'exec, OnSubgraphExecuteEndPayload<'exec>> {
20+
fn on_subgraph_http_request<'exec>(
21+
&'static self,
22+
_payload: OnSubgraphHttpRequestPayload<'exec>,
23+
) -> ControlFlow<'exec, OnSubgraphHttpResponsePayload<'exec>> {
2524
ControlFlow::Continue
2625
}
2726
fn on_schema_reload(&self, _payload: OnSchemaReloadPayload) {}

0 commit comments

Comments
 (0)