hyperlane-plugin-graphql 23.3.1

A zero-external-parser GraphQL over HTTP plugin for the Hyperlane framework featuring a recursive-descent GraphQL query parser, depth and complexity DoS limits, XxHash3_64 query/mutation resolver registries, variable binding, and GraphQL-over-HTTP POST/GET dispatch.
Documentation
use super::*;

/// Parsed GraphQL field selection with optional alias, arguments, and nested sub-selections.
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct FieldSelection {
    /// Optional response key alias (`alias: name`).
    pub(crate) alias: Option<String>,
    /// Canonical schema field name.
    pub(crate) name: String,
    /// Resolved argument values.
    pub(crate) arguments: BTreeMap<String, Value>,
    /// Nested sub-field selections.
    pub(crate) sub_fields: Vec<FieldSelection>,
}

/// Parsed GraphQL operation document.
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct ParsedOperation {
    /// Operation kind (`Query` or `Mutation`).
    #[get(type(copy))]
    pub(crate) kind: OperationKind,
    /// Optional operation name.
    pub(crate) name: Option<String>,
    /// Root selection set fields.
    pub(crate) selections: Vec<FieldSelection>,
}

/// Arguments and variables passed to a [`GraphqlResolver`].
#[derive(Clone, Debug, Getter)]
pub struct GraphqlFieldArgs {
    /// Resolved field arguments from the selection.
    pub(crate) arguments: BTreeMap<String, Value>,
    /// Top-level variables supplied by the HTTP request.
    pub(crate) variables: Map<String, Value>,
}

/// Inbound GraphQL-over-HTTP JSON request envelope.
#[derive(Clone, Debug, Deserialize, Getter, Serialize)]
pub struct GraphqlRequest {
    /// GraphQL query document text.
    pub(crate) query: String,
    /// Optional operation name.
    #[serde(rename = "operationName", default)]
    pub(crate) operation_name: Option<String>,
    /// Optional variables JSON object.
    #[serde(default)]
    pub(crate) variables: Map<String, Value>,
}

/// One error entry in a GraphQL-spec response envelope.
#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
pub struct GraphqlError {
    /// Human-readable error message.
    pub(crate) message: String,
}

/// Spec-compliant GraphQL HTTP response envelope (`{"data": ..., "errors": [...]}`).
#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
pub struct GraphqlResponse {
    /// Resolved data object (present when execution completed).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) data: Option<Value>,
    /// Collected syntax or resolver errors.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub(crate) errors: Vec<GraphqlError>,
}

/// Configuration for [`GraphqlPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct GraphqlConfig {
    /// Exact HTTP endpoint path handled by [`GraphqlHook`] (default `"/graphql"`).
    pub(crate) endpoint: String,
    /// Maximum query nesting depth allowed (DoS cap).
    #[get(type(copy))]
    pub(crate) max_depth: usize,
    /// Maximum total selected fields allowed per operation (DoS cap).
    #[get(type(copy))]
    pub(crate) max_fields: usize,
    /// Maximum raw query text length in bytes.
    #[get(type(copy))]
    pub(crate) max_query_bytes: usize,
    /// Whether HTTP `GET /graphql?query=...` read-only queries are enabled.
    #[get(type(copy))]
    pub(crate) allow_get: bool,
}

/// Wrapper adapting any `Fn(&GraphqlFieldArgs) -> Result<Value, String>` closure into [`GraphqlResolver`].
pub struct FnGraphqlResolver<F> {
    /// Wrapped resolver closure.
    pub(crate) func: F,
}

/// Compiled GraphQL schema and execution engine backing [`GraphqlHook`].
pub struct GraphqlPlugin {
    /// Retained configuration.
    pub(crate) config: GraphqlConfig,
    /// `XxHash3_64` map of root `Query` resolvers.
    pub(crate) queries: HashMapXxHash3_64<String, Arc<dyn GraphqlResolver>>,
    /// `XxHash3_64` map of root `Mutation` resolvers.
    pub(crate) mutations: HashMapXxHash3_64<String, Arc<dyn GraphqlResolver>>,
}

/// Opt-in request middleware handling GraphQL-over-HTTP `POST` and `GET` requests.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-62)]
pub struct GraphqlHook;