hive_router_plan_executor/plugins/hooks/on_query_plan.rs
1use std::sync::Arc;
2
3use hive_router_query_planner::{
4 ast::operation::OperationDefinition,
5 planner::{plan_nodes::QueryPlan, Planner},
6 utils::cancellation::CancellationToken,
7};
8
9use crate::{
10 execution::plan::PlanExecutionOutput,
11 plugin_context::{PluginContext, RouterHttpRequest},
12 plugin_trait::{CacheHint, EndHookPayload, EndHookResult, StartHookPayload, StartHookResult},
13};
14
15pub struct OnQueryPlanStartHookPayload<'exec> {
16 /// The incoming HTTP request to the router for which the GraphQL execution is happening.
17 /// It includes all the details of the request such as headers, body, etc.
18 ///
19 /// Example:
20 /// ```
21 /// let my_header = payload.router_http_request.headers.get("my-header");
22 /// // do something with the header...
23 /// payload.proceed()
24 /// ```
25 pub router_http_request: &'exec RouterHttpRequest<'exec>,
26 /// The context object that can be used to share data across different plugin hooks for the same request.
27 /// It is unique per request and is dropped after the response is sent.
28 ///
29 /// [Learn more about the context data sharing in the docs](https://the-guild.dev/graphql/hive/docs/router/extensibility/plugin_system#context-data-sharing)
30 pub context: &'exec PluginContext,
31 /// The GraphQL Document AST that will be used for query planning.
32 pub filtered_operation_for_plan: &'exec OperationDefinition,
33 /// The cancellation token that can be used to check if the request has been cancelled by the client or not.
34 pub cancellation_token: &'exec CancellationToken,
35 /// The query planner instance that will be used to generate the query plan for the incoming GraphQL request.
36 pub planner: &'exec Planner,
37}
38
39impl<'exec> StartHookPayload<OnQueryPlanEndHookPayload, PlanExecutionOutput>
40 for OnQueryPlanStartHookPayload<'exec>
41{
42}
43
44pub type OnQueryPlanStartHookResult<'exec> = StartHookResult<
45 'exec,
46 OnQueryPlanStartHookPayload<'exec>,
47 OnQueryPlanEndHookPayload,
48 PlanExecutionOutput,
49>;
50
51pub struct OnQueryPlanEndHookPayload {
52 /// The generated query plan for the incoming GraphQL request.
53 pub query_plan: Arc<QueryPlan>,
54 /// The cache hint for the generated query plan.
55 /// - If this is `CacheHint::Hit`, it means the query planning process didn't happen because the result was retrieved from the cache.
56 /// - If this is `CacheHint::Miss`, it means the query planning process happened and the result was not retrieved from the cache.
57 pub cache_hint: CacheHint,
58}
59
60impl EndHookPayload<PlanExecutionOutput> for OnQueryPlanEndHookPayload {}
61
62pub type OnQueryPlanEndHookResult = EndHookResult<OnQueryPlanEndHookPayload, PlanExecutionOutput>;