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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use bytes::Bytes;
use crate::executor::{
executors::{
common::SubgraphExecutionRequest,
http::{DeduplicationHint, SubgraphHttpResponse},
},
plugin_context::PluginContext,
plugin_trait::{
from_graphql_errors_to_bytes, EndHookPayload, FromGraphQLErrorToResponse,
FromGraphQLErrorsToResponse, StartHookPayload,
},
request_context::RequestContextPluginApi,
response::graphql_error::GraphQLError,
};
type RequestContextApi = RequestContextPluginApi<super::OnSubgraphHttp>;
pub struct OnSubgraphHttpRequestHookPayload<'exec> {
/// The name of the subgraph for which the HTTP request is being sent.
pub subgraph_name: &'exec str,
/// The endpoint of the subgraph for which the HTTP request is being sent.
pub endpoint: &'exec http::Uri,
/// The HTTP method of the request being sent to the subgraph.
pub method: http::Method,
/// The raw body of the HTTP request being sent to the subgraph.
pub body: Vec<u8>,
/// The original GraphQL request that is being executed and for which the HTTP request is being sent to the subgraph.
pub execution_request: SubgraphExecutionRequest<'exec>,
/// The flag indicating whether the request should be deduplicated or not.
/// If this is `true`, the router will check if there is an ongoing request with the same subgraph name and execution request,
/// and if there is, it will wait for the ongoing request to finish and return the same response instead of sending a new request to the subgraph.
///
/// [Learn more about request deduplication](https://the-guild.dev/graphql/hive/docs/router/guides/performance-tuning#request-deduplication)
pub deduplicate_request: bool,
/// 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> StartHookPayload<OnSubgraphHttpResponseHookPayload<'exec>, SubgraphHttpResponse>
for OnSubgraphHttpRequestHookPayload<'exec>
{
}
pub type OnSubgraphHttpRequestHookResult<'exec> = crate::executor::plugin_trait::StartHookResult<
'exec,
OnSubgraphHttpRequestHookPayload<'exec>,
OnSubgraphHttpResponseHookPayload<'exec>,
SubgraphHttpResponse,
>;
pub struct OnSubgraphHttpResponseHookPayload<'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 HTTP response received from the subgraph for the HTTP request sent by the router.
/// Plugins can modify the response before it is sent back to the client.
pub response: SubgraphHttpResponse,
/// The flag indicating whether the request was deduplicated or not.
/// - If this is `DeduplicationHint::Deduped`, it means the request was deduplicated and the response is the result of an ongoing request with the same subgraph name and execution request.
/// - If this is `DeduplicationHint::NotDeduped`, it means the request was not deduplicated and the response is the result of a new request sent to the subgraph.
pub deduplication_hint: DeduplicationHint,
}
impl<'exec> EndHookPayload<SubgraphHttpResponse> for OnSubgraphHttpResponseHookPayload<'exec> {}
pub type OnSubgraphHttpResponseHookResult<'exec> = crate::executor::plugin_trait::EndHookResult<
OnSubgraphHttpResponseHookPayload<'exec>,
SubgraphHttpResponse,
>;
impl FromGraphQLErrorToResponse for SubgraphHttpResponse {
fn from_graphql_error_to_response(error: GraphQLError, status: http::StatusCode) -> Self {
Self::from_graphql_errors_to_response(vec![error], status)
}
}
impl FromGraphQLErrorsToResponse for SubgraphHttpResponse {
fn from_graphql_errors_to_response(
errors: Vec<GraphQLError>,
status: http::StatusCode,
) -> Self {
let body_bytes = from_graphql_errors_to_bytes(errors);
SubgraphHttpResponse {
body: Bytes::from(body_bytes),
status,
..Default::default()
}
}
}