hive_router_plan_executor/plugins/hooks/on_subgraph_execute.rs
1use crate::{
2 executors::common::{SubgraphExecutionRequest, SubgraphExecutorBoxedArc},
3 plugin_context::{PluginContext, RouterHttpRequest},
4 plugin_trait::{
5 EndHookPayload, EndHookResult, FromGraphQLErrorToResponse, StartHookPayload,
6 StartHookResult,
7 },
8 response::{graphql_error::GraphQLError, subgraph_response::SubgraphResponse},
9};
10
11pub struct OnSubgraphExecuteStartHookPayload<'exec> {
12 /// The incoming HTTP request to the router for which the GraphQL execution is happening.
13 /// It includes all the details of the request such as headers, body, etc.
14 ///
15 /// Example:
16 /// ```
17 /// let my_header = payload.router_http_request.headers.get("my-header");
18 /// // do something with the header...
19 /// payload.proceed()
20 /// ```
21 pub router_http_request: &'exec RouterHttpRequest<'exec>,
22 /// The context object that can be used to share data across different plugin hooks for the same request.
23 /// It is unique per request and is dropped after the response is sent.
24 ///
25 /// [Learn more about the context data sharing in the docs](https://the-guild.dev/graphql/hive/docs/router/extensibility/plugin_system#context-data-sharing)
26 pub context: &'exec PluginContext,
27
28 /// The name of the subgraph for which the execution is happening.
29 pub subgraph_name: &'exec str,
30 /// The executor instance that will be used to execute the query plan for the incoming GraphQL request.
31 pub executor: SubgraphExecutorBoxedArc,
32
33 /// The execution request object that contains all the details about the execution such as the query plan, variables, etc.
34 pub execution_request: SubgraphExecutionRequest<'exec>,
35}
36
37impl<'exec> StartHookPayload<OnSubgraphExecuteEndHookPayload<'exec>, SubgraphResponse<'exec>>
38 for OnSubgraphExecuteStartHookPayload<'exec>
39{
40}
41
42pub type OnSubgraphExecuteStartHookResult<'exec> = StartHookResult<
43 'exec,
44 OnSubgraphExecuteStartHookPayload<'exec>,
45 OnSubgraphExecuteEndHookPayload<'exec>,
46 SubgraphResponse<'exec>,
47>;
48
49pub struct OnSubgraphExecuteEndHookPayload<'exec> {
50 /// The execution result from the subgraph execution for the incoming GraphQL request.
51 /// Plugins can modify the execution result before it is sent back to the client.
52 pub execution_result: SubgraphResponse<'exec>,
53 /// The context object that can be used to share data across different plugin hooks for the same request.
54 /// It is unique per request and is dropped after the response is sent.
55 ///
56 /// [Learn more about the context data sharing in the docs](https://the-guild.dev/graphql/hive/docs/router/extensibility/plugin_system#context-data-sharing)
57 pub context: &'exec PluginContext,
58}
59
60impl<'exec> EndHookPayload<SubgraphResponse<'exec>> for OnSubgraphExecuteEndHookPayload<'exec> {}
61
62pub type OnSubgraphExecuteEndHookResult<'exec> =
63 EndHookResult<OnSubgraphExecuteEndHookPayload<'exec>, SubgraphResponse<'exec>>;
64
65impl FromGraphQLErrorToResponse for SubgraphResponse<'_> {
66 fn from_graphql_error_to_response(error: GraphQLError, _status_code: http::StatusCode) -> Self {
67 SubgraphResponse {
68 errors: Some(vec![error]),
69 ..Default::default()
70 }
71 }
72}