async-graphql/src/types/empty_subscription.rs
sunli 0769513c8b v1.5.0
Improve performance
2020-03-24 18:54:22 +08:00

61 lines
1.6 KiB
Rust

use crate::{
registry, ContextBase, ContextSelectionSet, JsonWriter, OutputValueType, QueryError, Result,
SubscriptionType, Type,
};
use graphql_parser::query::Field;
use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
/// Empty subscription
///
/// Only the parameters used to construct the Schema, representing an unconfigured subscription.
pub struct EmptySubscription;
impl Type for EmptySubscription {
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("EmptyMutation")
}
fn create_type_info(registry: &mut registry::Registry) -> String {
registry.create_type::<Self, _>(|_| registry::Type::Object {
name: "EmptySubscription".to_string(),
description: None,
fields: Default::default(),
cache_control: Default::default(),
})
}
}
#[async_trait::async_trait]
impl SubscriptionType for EmptySubscription {
fn is_empty() -> bool {
true
}
fn create_type(_field: &Field, _types: &mut HashMap<TypeId, Field>) -> Result<()> {
unreachable!()
}
async fn resolve(
&self,
_ctx: &ContextBase<'_, ()>,
_types: &HashMap<TypeId, Field, RandomState>,
_msg: &(dyn Any + Send + Sync),
) -> Result<Option<String>> {
unreachable!()
}
}
#[async_trait::async_trait]
impl OutputValueType for EmptySubscription {
async fn resolve(
_value: &Self,
_ctx: &ContextSelectionSet<'_>,
_w: &mut JsonWriter,
) -> Result<()> {
Err(QueryError::NotConfiguredSubscriptions.into())
}
}