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