Skip to main content

hyperlane_plugin_graphql/
struct.rs

1use super::*;
2
3/// Parsed GraphQL field selection with optional alias, arguments, and nested sub-selections.
4#[derive(Clone, Debug, Eq, Getter, PartialEq)]
5pub struct FieldSelection {
6    /// Optional response key alias (`alias: name`).
7    pub(crate) alias: Option<String>,
8    /// Canonical schema field name.
9    pub(crate) name: String,
10    /// Resolved argument values.
11    pub(crate) arguments: BTreeMap<String, Value>,
12    /// Nested sub-field selections.
13    pub(crate) sub_fields: Vec<FieldSelection>,
14}
15
16/// Parsed GraphQL operation document.
17#[derive(Clone, Debug, Eq, Getter, PartialEq)]
18pub struct ParsedOperation {
19    /// Operation kind (`Query` or `Mutation`).
20    #[get(type(copy))]
21    pub(crate) kind: OperationKind,
22    /// Optional operation name.
23    pub(crate) name: Option<String>,
24    /// Root selection set fields.
25    pub(crate) selections: Vec<FieldSelection>,
26}
27
28/// Arguments and variables passed to a [`GraphqlResolver`].
29#[derive(Clone, Debug, Getter)]
30pub struct GraphqlFieldArgs {
31    /// Resolved field arguments from the selection.
32    pub(crate) arguments: BTreeMap<String, Value>,
33    /// Top-level variables supplied by the HTTP request.
34    pub(crate) variables: Map<String, Value>,
35}
36
37/// Inbound GraphQL-over-HTTP JSON request envelope.
38#[derive(Clone, Debug, Deserialize, Getter, Serialize)]
39pub struct GraphqlRequest {
40    /// GraphQL query document text.
41    pub(crate) query: String,
42    /// Optional operation name.
43    #[serde(rename = "operationName", default)]
44    pub(crate) operation_name: Option<String>,
45    /// Optional variables JSON object.
46    #[serde(default)]
47    pub(crate) variables: Map<String, Value>,
48}
49
50/// One error entry in a GraphQL-spec response envelope.
51#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
52pub struct GraphqlError {
53    /// Human-readable error message.
54    pub(crate) message: String,
55}
56
57/// Spec-compliant GraphQL HTTP response envelope (`{"data": ..., "errors": [...]}`).
58#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
59pub struct GraphqlResponse {
60    /// Resolved data object (present when execution completed).
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub(crate) data: Option<Value>,
63    /// Collected syntax or resolver errors.
64    #[serde(default, skip_serializing_if = "Vec::is_empty")]
65    pub(crate) errors: Vec<GraphqlError>,
66}
67
68/// Configuration for [`GraphqlPlugin`].
69#[derive(Clone, Data, Debug, Eq, PartialEq)]
70pub struct GraphqlConfig {
71    /// Exact HTTP endpoint path handled by [`GraphqlHook`] (default `"/graphql"`).
72    pub(crate) endpoint: String,
73    /// Maximum query nesting depth allowed (DoS cap).
74    #[get(type(copy))]
75    pub(crate) max_depth: usize,
76    /// Maximum total selected fields allowed per operation (DoS cap).
77    #[get(type(copy))]
78    pub(crate) max_fields: usize,
79    /// Maximum raw query text length in bytes.
80    #[get(type(copy))]
81    pub(crate) max_query_bytes: usize,
82    /// Whether HTTP `GET /graphql?query=...` read-only queries are enabled.
83    #[get(type(copy))]
84    pub(crate) allow_get: bool,
85}
86
87/// Wrapper adapting any `Fn(&GraphqlFieldArgs) -> Result<Value, String>` closure into [`GraphqlResolver`].
88pub struct FnGraphqlResolver<F> {
89    /// Wrapped resolver closure.
90    pub(crate) func: F,
91}
92
93/// Compiled GraphQL schema and execution engine backing [`GraphqlHook`].
94pub struct GraphqlPlugin {
95    /// Retained configuration.
96    pub(crate) config: GraphqlConfig,
97    /// `XxHash3_64` map of root `Query` resolvers.
98    pub(crate) queries: HashMapXxHash3_64<String, Arc<dyn GraphqlResolver>>,
99    /// `XxHash3_64` map of root `Mutation` resolvers.
100    pub(crate) mutations: HashMapXxHash3_64<String, Arc<dyn GraphqlResolver>>,
101}
102
103/// Opt-in request middleware handling GraphQL-over-HTTP `POST` and `GET` requests.
104#[derive(Clone, Copy, Debug, Default)]
105#[hyperlane::request_middleware(-62)]
106pub struct GraphqlHook;