async-graphql/src/types/empty_subscription.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2020-03-19 09:20:12 +00:00
use crate::{
registry, ContextBase, ContextSelectionSet, Error, OutputValueType, QueryError, Result,
2020-03-19 09:20:12 +00:00
SubscriptionType, Type,
};
use graphql_parser::query::Field;
use graphql_parser::Pos;
2020-03-25 03:39:28 +00:00
use serde_json::Value;
2020-03-17 09:26:59 +00:00
use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
2020-03-17 11:11:14 +00:00
/// Empty subscription
///
/// Only the parameters used to construct the Schema, representing an unconfigured subscription.
2020-03-19 09:20:12 +00:00
pub struct EmptySubscription;
2020-03-17 09:26:59 +00:00
2020-03-19 09:20:12 +00:00
impl Type for EmptySubscription {
2020-03-17 09:26:59 +00:00
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 {
2020-03-19 09:20:12 +00:00
name: "EmptySubscription".to_string(),
2020-03-17 09:26:59 +00:00
description: None,
fields: Default::default(),
2020-03-22 08:45:59 +00:00
cache_control: Default::default(),
2020-03-17 09:26:59 +00:00
})
}
}
#[async_trait::async_trait]
2020-03-19 09:20:12 +00:00
impl SubscriptionType for EmptySubscription {
fn is_empty() -> bool {
true
}
fn create_type(_field: &Field, _types: &mut HashMap<TypeId, Field>) -> Result<()> {
2020-03-19 09:20:12 +00:00
unreachable!()
2020-03-17 09:26:59 +00:00
}
async fn resolve(
&self,
_ctx: &ContextBase<'_, ()>,
_types: &HashMap<TypeId, Field, RandomState>,
_msg: &(dyn Any + Send + Sync),
2020-03-25 03:39:28 +00:00
) -> Result<Option<Value>> {
2020-03-19 09:20:12 +00:00
unreachable!()
}
}
#[async_trait::async_trait]
impl OutputValueType for EmptySubscription {
async fn resolve(
_value: &Self,
_ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
Err(Error::Query {
pos,
path: None,
err: QueryError::NotConfiguredSubscriptions,
})
2020-03-17 09:26:59 +00:00
}
}