Skip to content

Commit 03cf66d

Browse files
committed
New example
1 parent 206136a commit 03cf66d

File tree

11 files changed

+158
-67
lines changed

11 files changed

+158
-67
lines changed

lib/executor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ pub mod variables;
1212

1313
pub use execution::plan::execute_query_plan;
1414
pub use executors::map::SubgraphExecutorMap;
15-
pub use plugins::response_cache::*;
15+
pub use plugins::*;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod response_cache;
2+
pub mod subgraph_response_cache;

lib/executor/src/plugins/response_cache.rs renamed to lib/executor/src/plugins/examples/response_cache.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use ntex::web::HttpResponse;
33
use redis::Commands;
44

55
use crate::{
6-
plugins::traits::{
7-
ControlFlow, OnExecutePayload, OnSchemaReloadPayload, RouterPlugin
8-
},
9-
utils::consts::TYPENAME_FIELD_NAME,
6+
hooks::{on_execute::OnExecutePayload, on_schema_reload::OnSchemaReloadPayload}, plugins::plugin_trait::{
7+
ControlFlow, RouterPlugin
8+
}, utils::consts::TYPENAME_FIELD_NAME
109
};
1110

1211
pub struct ResponseCachePlugin {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use dashmap::DashMap;
2+
3+
use crate::{hooks::on_subgraph_execute::{OnSubgraphExecuteEndPayload, OnSubgraphExecuteStartPayload, SubgraphExecutorResponse, SubgraphResponse}, plugin_trait::{ControlFlow, RouterPlugin}};
4+
5+
struct SubgraphResponseCachePlugin {
6+
cache: DashMap<String, SubgraphResponse<'static>>,
7+
}
8+
9+
impl RouterPlugin for SubgraphResponseCachePlugin {
10+
fn on_subgraph_execute<'exec>(
11+
&self,
12+
payload: OnSubgraphExecuteStartPayload<'exec>,
13+
) -> ControlFlow<'exec, OnSubgraphExecuteEndPayload<'exec>> {
14+
let key = format!(
15+
"subgraph_response_cache:{}:{}:{:?}",
16+
payload.subgraph_name, payload.execution_request.operation_name.unwrap_or(""), payload.execution_request.variables
17+
);
18+
if let Some(cached_response) = self.cache.get(&key) {
19+
*payload.response = Some(SubgraphExecutorResponse::RawResponse(cached_response));
20+
// Return early with the cached response
21+
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+
}))
30+
}
31+
}
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod on_execute;
2+
pub mod on_schema_reload;
3+
pub mod on_subgraph_execute;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::collections::HashMap;
2+
use std::sync::Arc;
3+
4+
use hive_router_query_planner::planner::plan_nodes::QueryPlan;
5+
use ntex::web::HttpRequest;
6+
7+
use crate::response::{value::Value};
8+
use crate::response::graphql_error::GraphQLError;
9+
10+
pub struct OnExecutePayload<'exec> {
11+
pub router_http_request: &'exec HttpRequest,
12+
pub query_plan: Arc<QueryPlan>,
13+
14+
pub data: &'exec mut Value<'exec>,
15+
pub errors: &'exec mut Vec<GraphQLError>,
16+
pub extensions: &'exec mut HashMap<String, sonic_rs::Value>,
17+
18+
pub skip_execution: bool,
19+
20+
pub variable_values: &'exec Option<HashMap<String, sonic_rs::Value>>,
21+
}
22+
23+
pub struct OnExecuteEndPayload<'exec> {
24+
pub router_http_request: &'exec HttpRequest,
25+
pub query_plan: Arc<QueryPlan>,
26+
27+
pub data: &'exec Value<'exec>,
28+
pub errors: &'exec Vec<GraphQLError>,
29+
pub extensions: &'exec mut HashMap<String, sonic_rs::Value>,
30+
31+
pub variable_values: &'exec Option<HashMap<String, sonic_rs::Value>>,
32+
}
33+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use hive_router_query_planner::consumer_schema::ConsumerSchema;
2+
3+
pub struct OnSchemaReloadPayload {
4+
pub old_schema: &'static ConsumerSchema,
5+
pub new_schema: &'static mut ConsumerSchema,
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::collections::HashMap;
2+
3+
use bytes::Bytes;
4+
use hive_router_query_planner::ast::operation::SubgraphFetchOperation;
5+
use ntex::web::HttpRequest;
6+
7+
use crate::{executors::dedupe::SharedResponse, response::{graphql_error::GraphQLError, value::Value}};
8+
9+
10+
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>>,
19+
}
20+
21+
pub struct SubgraphExecutionRequest<'exec> {
22+
pub query: &'exec str,
23+
// We can add the original operation here too
24+
pub operation: &'exec SubgraphFetchOperation,
25+
26+
pub dedupe: bool,
27+
pub operation_name: Option<&'exec str>,
28+
pub variables: Option<HashMap<&'exec str, &'exec sonic_rs::Value>>,
29+
pub extensions: Option<HashMap<String, sonic_rs::Value>>,
30+
pub representations: Option<Vec<u8>>,
31+
}
32+
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> {
40+
pub router_http_request: &'exec HttpRequest,
41+
pub subgraph_name: &'exec str,
42+
// The node that initiates this subgraph execution
43+
pub execution_request: &'exec SubgraphExecutionRequest<'exec>,
44+
// This will be tricky to implement with the current structure,
45+
// but I'm sure we'll figure it out
46+
pub response: &'exec SubgraphResponse<'exec>,
47+
}

lib/executor/src/plugins/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
pub mod response_cache;
2-
pub mod traits;
1+
pub mod examples;
2+
pub mod plugin_trait;
3+
pub mod hooks;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use ntex::web::HttpResponse;
2+
3+
use crate::hooks::on_execute::OnExecutePayload;
4+
use crate::hooks::on_schema_reload::OnSchemaReloadPayload;
5+
use crate::hooks::on_subgraph_execute::OnSubgraphExecuteEndPayload;
6+
use crate::hooks::on_subgraph_execute::OnSubgraphExecuteStartPayload;
7+
8+
pub enum ControlFlow<'a, TPayload> {
9+
Continue,
10+
Break(HttpResponse),
11+
OnEnd(Box<dyn FnOnce(TPayload) -> ControlFlow<'a, ()> + Send + 'a>),
12+
}
13+
14+
pub trait RouterPlugin {
15+
fn on_execute<'exec>(
16+
&self,
17+
_payload: OnExecutePayload<'exec>,
18+
) -> ControlFlow<'exec, OnExecutePayload<'exec>> {
19+
ControlFlow::Continue
20+
}
21+
fn on_subgraph_execute<'exec>(
22+
&self,
23+
_payload: OnSubgraphExecuteStartPayload<'exec>,
24+
) -> ControlFlow<'exec, OnSubgraphExecuteEndPayload<'exec>> {
25+
ControlFlow::Continue
26+
}
27+
fn on_schema_reload(&self, _payload: OnSchemaReloadPayload) {}
28+
}

0 commit comments

Comments
 (0)