use crate::query_planner::state::supergraph_state::OperationKind;
use serde::ser::SerializeMap;
use sonic_rs::Value;
use super::super::api::plugin::RequestContextPluginRead;
use super::RequestContextDomain;
use super::RequestContextError;
pub(crate) const OPERATION_NAME_KEY: &str = "hive::operation::name";
pub(crate) const OPERATION_KIND_KEY: &str = "hive::operation::kind";
#[derive(Debug, Clone, Default)]
pub struct OperationContext {
pub name: Option<String>,
pub kind: Option<OperationKind>,
}
impl OperationContext {
pub fn update(&mut self, name: Option<String>, kind: Option<OperationKind>) {
self.name = name;
self.kind = kind;
}
}
pub struct RequestContextOperationRead<'a> {
context: &'a OperationContext,
}
impl RequestContextOperationRead<'_> {
pub fn name(&self) -> Option<&String> {
self.context.name.as_ref()
}
pub fn kind(&self) -> Option<&OperationKind> {
self.context.kind.as_ref()
}
}
impl<Hook> RequestContextPluginRead<Hook> {
pub fn operation(&self) -> RequestContextOperationRead<'_> {
RequestContextOperationRead {
context: &self.snapshot.operation,
}
}
}
impl RequestContextDomain for OperationContext {
const DOMAIN_PREFIX: &'static str = "hive::operation::";
fn set_key_value(&mut self, key: &str, _value: Value) -> Result<(), RequestContextError> {
match key {
OPERATION_NAME_KEY => self.forbidden_mutation(key),
OPERATION_KIND_KEY => self.forbidden_mutation(key),
_ => self.unknown_key(key),
}
}
super::impl_domain_serde!(
OPERATION_NAME_KEY => name,
OPERATION_KIND_KEY => kind,
);
}