2020-03-05 00:39:56 +00:00
|
|
|
use crate::context::Data;
|
2020-03-26 03:34:28 +00:00
|
|
|
use crate::extensions::{BoxExtension, Extension};
|
2020-03-04 06:24:44 +00:00
|
|
|
use crate::model::__DirectiveLocation;
|
2020-03-17 09:26:59 +00:00
|
|
|
use crate::query::QueryBuilder;
|
2020-03-04 06:24:44 +00:00
|
|
|
use crate::registry::{Directive, InputValue, Registry};
|
2020-03-29 12:02:52 +00:00
|
|
|
use crate::subscription::{create_connection, SubscriptionStub, SubscriptionTransport};
|
2020-03-03 11:15:18 +00:00
|
|
|
use crate::types::QueryRoot;
|
2020-03-29 12:02:52 +00:00
|
|
|
use crate::validation::check_rules;
|
|
|
|
use crate::{
|
|
|
|
ContextSelectionSet, ObjectType, QueryError, QueryParseError, Result, SubscriptionStream,
|
|
|
|
SubscriptionType, Type, Variables,
|
|
|
|
};
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::channel::mpsc;
|
|
|
|
use futures::lock::Mutex;
|
|
|
|
use futures::SinkExt;
|
|
|
|
use graphql_parser::parse_query;
|
|
|
|
use graphql_parser::query::{
|
|
|
|
Definition, Field, FragmentDefinition, OperationDefinition, Selection,
|
|
|
|
};
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use slab::Slab;
|
|
|
|
use std::any::{Any, TypeId};
|
2020-03-05 07:50:57 +00:00
|
|
|
use std::collections::HashMap;
|
2020-03-29 12:02:52 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::Arc;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
type MsgSender = mpsc::Sender<Arc<dyn Any + Sync + Send>>;
|
|
|
|
|
|
|
|
pub(crate) static SUBSCRIPTION_SENDERS: Lazy<Mutex<Slab<MsgSender>>> = Lazy::new(Default::default);
|
|
|
|
|
|
|
|
pub(crate) struct SchemaInner<Query, Mutation, Subscription> {
|
2020-03-25 07:07:16 +00:00
|
|
|
pub(crate) query: QueryRoot<Query>,
|
|
|
|
pub(crate) mutation: Mutation,
|
2020-03-17 09:26:59 +00:00
|
|
|
pub(crate) subscription: Subscription,
|
|
|
|
pub(crate) registry: Registry,
|
|
|
|
pub(crate) data: Data,
|
2020-03-25 07:07:16 +00:00
|
|
|
pub(crate) complexity: Option<usize>,
|
|
|
|
pub(crate) depth: Option<usize>,
|
2020-03-26 03:34:28 +00:00
|
|
|
pub(crate) extensions: Vec<Box<dyn Fn() -> BoxExtension + Send + Sync>>,
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Schema builder
|
|
|
|
pub struct SchemaBuilder<Query, Mutation, Subscription>(SchemaInner<Query, Mutation, Subscription>);
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl<Query: ObjectType, Mutation: ObjectType, Subscription: SubscriptionType>
|
2020-03-29 12:02:52 +00:00
|
|
|
SchemaBuilder<Query, Mutation, Subscription>
|
2020-03-17 09:26:59 +00:00
|
|
|
{
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Disable introspection query
|
|
|
|
pub fn disable_introspection(mut self) -> Self {
|
|
|
|
self.0.query.disable_introspection = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set limit complexity, Default no limit.
|
|
|
|
pub fn limit_complexity(mut self, complexity: usize) -> Self {
|
|
|
|
self.0.complexity = Some(complexity);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set limit complexity, Default no limit.
|
|
|
|
pub fn limit_depth(mut self, depth: usize) -> Self {
|
|
|
|
self.0.depth = Some(depth);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an extension
|
|
|
|
pub fn extension<F: Fn() -> E + Send + Sync + 'static, E: Extension>(
|
|
|
|
mut self,
|
|
|
|
extension_factory: F,
|
|
|
|
) -> Self {
|
|
|
|
self.0
|
|
|
|
.extensions
|
|
|
|
.push(Box::new(move || Box::new(extension_factory())));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a global data that can be accessed in the `Context`.
|
|
|
|
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
|
|
|
|
self.0.data.insert(data);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build schema.
|
|
|
|
pub fn finish(self) -> Schema<Query, Mutation, Subscription> {
|
|
|
|
Schema(Arc::new(self.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GraphQL schema
|
|
|
|
pub struct Schema<Query, Mutation, Subscription>(
|
|
|
|
pub(crate) Arc<SchemaInner<Query, Mutation, Subscription>>,
|
|
|
|
);
|
|
|
|
|
|
|
|
impl<Query, Mutation, Subscription> Clone for Schema<Query, Mutation, Subscription> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Schema(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Query, Mutation, Subscription> Schema<Query, Mutation, Subscription>
|
|
|
|
where
|
|
|
|
Query: ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: ObjectType + Send + Sync + 'static,
|
|
|
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
/// Create a schema builder
|
2020-03-09 10:05:52 +00:00
|
|
|
///
|
|
|
|
/// The root object for the query and Mutation needs to be specified.
|
2020-03-19 09:20:12 +00:00
|
|
|
/// If there is no mutation, you can use `EmptyMutation`.
|
|
|
|
/// If there is no subscription, you can use `EmptySubscription`.
|
2020-03-29 12:02:52 +00:00
|
|
|
pub fn build(
|
|
|
|
query: Query,
|
|
|
|
mutation: Mutation,
|
|
|
|
subscription: Subscription,
|
|
|
|
) -> SchemaBuilder<Query, Mutation, Subscription> {
|
2020-03-08 12:35:36 +00:00
|
|
|
let mut registry = Registry {
|
|
|
|
types: Default::default(),
|
|
|
|
directives: Default::default(),
|
|
|
|
implements: Default::default(),
|
|
|
|
query_type: Query::type_name().to_string(),
|
|
|
|
mutation_type: if Mutation::is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Mutation::type_name().to_string())
|
|
|
|
},
|
2020-03-17 09:26:59 +00:00
|
|
|
subscription_type: if Subscription::is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Subscription::type_name().to_string())
|
|
|
|
},
|
2020-03-08 12:35:36 +00:00
|
|
|
};
|
2020-03-04 06:24:44 +00:00
|
|
|
|
|
|
|
registry.add_directive(Directive {
|
|
|
|
name: "include",
|
|
|
|
description: Some("Directs the executor to include this field or fragment only when the `if` argument is true."),
|
2020-03-05 13:34:31 +00:00
|
|
|
locations: vec![
|
|
|
|
__DirectiveLocation::FIELD,
|
|
|
|
__DirectiveLocation::FRAGMENT_SPREAD,
|
|
|
|
__DirectiveLocation::INLINE_FRAGMENT
|
|
|
|
],
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("if", InputValue {
|
|
|
|
name: "if",
|
|
|
|
description: Some("Included when true."),
|
|
|
|
ty: "Boolean!".to_string(),
|
2020-03-21 01:32:13 +00:00
|
|
|
default_value: None,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
|
|
|
args
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
registry.add_directive(Directive {
|
|
|
|
name: "skip",
|
|
|
|
description: Some("Directs the executor to skip this field or fragment when the `if` argument is true."),
|
2020-03-05 13:34:31 +00:00
|
|
|
locations: vec![
|
|
|
|
__DirectiveLocation::FIELD,
|
|
|
|
__DirectiveLocation::FRAGMENT_SPREAD,
|
|
|
|
__DirectiveLocation::INLINE_FRAGMENT
|
|
|
|
],
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert("if", InputValue {
|
|
|
|
name: "if",
|
|
|
|
description: Some("Skipped when true."),
|
|
|
|
ty: "Boolean!".to_string(),
|
2020-03-21 01:32:13 +00:00
|
|
|
default_value: None,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
|
|
|
args
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// register scalars
|
|
|
|
bool::create_type_info(&mut registry);
|
|
|
|
i32::create_type_info(&mut registry);
|
|
|
|
f32::create_type_info(&mut registry);
|
|
|
|
String::create_type_info(&mut registry);
|
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
QueryRoot::<Query>::create_type_info(&mut registry);
|
2020-03-05 09:06:14 +00:00
|
|
|
if !Mutation::is_empty() {
|
|
|
|
Mutation::create_type_info(&mut registry);
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
if !Subscription::is_empty() {
|
|
|
|
Subscription::create_type_info(&mut registry);
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
SchemaBuilder(SchemaInner {
|
2020-03-25 07:07:16 +00:00
|
|
|
query: QueryRoot {
|
|
|
|
inner: query,
|
|
|
|
disable_introspection: false,
|
|
|
|
},
|
2020-03-01 10:54:34 +00:00
|
|
|
mutation,
|
2020-03-17 09:26:59 +00:00
|
|
|
subscription,
|
2020-03-05 00:39:56 +00:00
|
|
|
registry,
|
|
|
|
data: Default::default(),
|
2020-03-25 07:07:16 +00:00
|
|
|
complexity: None,
|
|
|
|
depth: None,
|
2020-03-26 03:34:28 +00:00
|
|
|
extensions: Default::default(),
|
2020-03-29 12:02:52 +00:00
|
|
|
})
|
2020-03-25 07:07:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Create a schema
|
|
|
|
pub fn new(
|
|
|
|
query: Query,
|
|
|
|
mutation: Mutation,
|
|
|
|
subscription: Subscription,
|
|
|
|
) -> Schema<Query, Mutation, Subscription> {
|
|
|
|
Self::build(query, mutation, subscription).finish()
|
2020-03-05 00:39:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// Start a query and return `QueryBuilder`.
|
2020-03-25 07:07:16 +00:00
|
|
|
pub fn query<'a>(&'a self, source: &'a str) -> QueryBuilder<'a, Query, Mutation, Subscription> {
|
2020-03-05 00:39:56 +00:00
|
|
|
QueryBuilder {
|
2020-03-29 12:02:52 +00:00
|
|
|
extensions: self.0.extensions.iter().map(|factory| factory()).collect(),
|
2020-03-25 07:07:16 +00:00
|
|
|
schema: self,
|
2020-03-17 09:26:59 +00:00
|
|
|
source,
|
2020-03-01 10:54:34 +00:00
|
|
|
operation_name: None,
|
|
|
|
variables: None,
|
2020-03-29 12:02:52 +00:00
|
|
|
data: &self.0.data,
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 11:15:18 +00:00
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Create subscription stub, typically called inside the `SubscriptionTransport::handle_request` method/
|
|
|
|
pub fn create_subscription_stub(
|
|
|
|
&self,
|
|
|
|
source: &str,
|
|
|
|
operation_name: Option<&str>,
|
|
|
|
variables: Variables,
|
|
|
|
) -> Result<SubscriptionStub<Query, Mutation, Subscription>>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
let document = parse_query(source).map_err(|err| QueryParseError(err.to_string()))?;
|
|
|
|
check_rules(&self.0.registry, &document)?;
|
|
|
|
|
|
|
|
let mut fragments = HashMap::new();
|
|
|
|
let mut subscription = None;
|
|
|
|
|
|
|
|
for definition in document.definitions {
|
|
|
|
match definition {
|
|
|
|
Definition::Operation(OperationDefinition::Subscription(s)) => {
|
|
|
|
if s.name.as_deref() == operation_name {
|
|
|
|
subscription = Some(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Definition::Fragment(fragment) => {
|
|
|
|
fragments.insert(fragment.name.clone(), fragment);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let subscription = subscription.ok_or(if let Some(name) = operation_name {
|
|
|
|
QueryError::UnknownOperationNamed {
|
|
|
|
name: name.to_string(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QueryError::MissingOperation
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut types = HashMap::new();
|
|
|
|
let resolve_id = AtomicUsize::default();
|
|
|
|
let ctx = ContextSelectionSet {
|
|
|
|
path_node: None,
|
|
|
|
extensions: &[],
|
|
|
|
item: &subscription.selection_set,
|
|
|
|
resolve_id: &resolve_id,
|
|
|
|
variables: &variables,
|
|
|
|
variable_definitions: Some(&subscription.variable_definitions),
|
|
|
|
registry: &self.0.registry,
|
|
|
|
data: &Default::default(),
|
|
|
|
fragments: &fragments,
|
|
|
|
};
|
|
|
|
create_subscription_types::<Subscription>(&ctx, &fragments, &mut types)?;
|
|
|
|
Ok(SubscriptionStub {
|
|
|
|
schema: self.clone(),
|
|
|
|
types,
|
|
|
|
variables,
|
|
|
|
variable_definitions: subscription.variable_definitions,
|
|
|
|
fragments,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create subscription connection, returns `Sink` and `Stream`.
|
|
|
|
pub async fn subscription_connection<T: SubscriptionTransport>(
|
|
|
|
&self,
|
|
|
|
transport: T,
|
|
|
|
) -> (
|
|
|
|
mpsc::Sender<Bytes>,
|
|
|
|
SubscriptionStream<Query, Mutation, Subscription, T>,
|
|
|
|
) {
|
|
|
|
create_connection(self, transport).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_subscription_types<T: SubscriptionType>(
|
|
|
|
ctx: &ContextSelectionSet<'_>,
|
|
|
|
fragments: &HashMap<String, FragmentDefinition>,
|
|
|
|
types: &mut HashMap<TypeId, Field>,
|
|
|
|
) -> Result<()> {
|
|
|
|
for selection in &ctx.items {
|
|
|
|
match selection {
|
|
|
|
Selection::Field(field) => {
|
|
|
|
if ctx.is_skip(&field.directives)? {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
T::create_type(field, types)?;
|
|
|
|
}
|
|
|
|
Selection::FragmentSpread(fragment_spread) => {
|
|
|
|
if ctx.is_skip(&fragment_spread.directives)? {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(fragment) = fragments.get(&fragment_spread.fragment_name) {
|
|
|
|
create_subscription_types::<T>(
|
|
|
|
&ctx.with_selection_set(&fragment.selection_set),
|
|
|
|
fragments,
|
|
|
|
types,
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
return Err(QueryError::UnknownFragment {
|
|
|
|
name: fragment_spread.fragment_name.clone(),
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Selection::InlineFragment(inline_fragment) => {
|
|
|
|
if ctx.is_skip(&inline_fragment.directives)? {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
create_subscription_types::<T>(
|
|
|
|
&ctx.with_selection_set(&inline_fragment.selection_set),
|
|
|
|
fragments,
|
|
|
|
types,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Publish a message that will be pushed to all subscribed clients.
|
|
|
|
pub async fn publish<T: Any + Send + Sync + Sized>(msg: T) {
|
|
|
|
let mut senders = SUBSCRIPTION_SENDERS.lock().await;
|
|
|
|
let msg = Arc::new(msg);
|
|
|
|
let mut remove = Vec::new();
|
|
|
|
for (id, sender) in senders.iter_mut() {
|
|
|
|
if sender.send(msg.clone()).await.is_err() {
|
|
|
|
remove.push(id);
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-29 12:02:52 +00:00
|
|
|
for id in remove {
|
|
|
|
senders.remove(id);
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|