async-graphql/src/schema.rs

178 lines
5.8 KiB
Rust
Raw Normal View History

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};
use crate::model::__DirectiveLocation;
2020-03-17 09:26:59 +00:00
use crate::query::QueryBuilder;
use crate::registry::{Directive, InputValue, Registry};
2020-03-03 11:15:18 +00:00
use crate::types::QueryRoot;
2020-03-19 09:20:12 +00:00
use crate::{ObjectType, SubscribeBuilder, SubscriptionType, Type};
2020-03-05 00:39:56 +00:00
use std::any::Any;
2020-03-05 07:50:57 +00:00
use std::collections::HashMap;
2020-03-01 10:54:34 +00:00
2020-03-09 10:05:52 +00:00
/// GraphQL schema
2020-03-17 09:26:59 +00:00
pub struct Schema<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-19 09:20:12 +00:00
impl<Query: ObjectType, Mutation: ObjectType, Subscription: SubscriptionType>
2020-03-17 09:26:59 +00:00
Schema<Query, Mutation, Subscription>
{
2020-03-20 03:56:08 +00:00
/// Create a schema
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-17 09:26:59 +00:00
pub fn new(query: Query, mutation: Mutation, subscription: Subscription) -> Self {
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
};
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
}
});
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
}
});
// 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-01 10:54:34 +00:00
Self {
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-05 00:39:56 +00:00
}
}
2020-03-25 07:07:16 +00:00
/// Disable introspection query
pub fn disable_introspection(mut self) -> Self {
self.query.disable_introspection = true;
self
}
/// Set limit complexity, Default no limit.
pub fn limit_complexity(mut self, complexity: usize) -> Self {
self.complexity = Some(complexity);
self
}
/// Set limit complexity, Default no limit.
pub fn limit_depth(mut self, depth: usize) -> Self {
self.depth = Some(depth);
self
}
2020-03-26 03:34:28 +00:00
/// Add an extension
pub fn extension<F: Fn() -> E + Send + Sync + 'static, E: Extension>(
mut self,
extension_factory: F,
) -> Self {
self.extensions
.push(Box::new(move || Box::new(extension_factory())));
self
}
2020-03-09 10:05:52 +00:00
/// Add a global data that can be accessed in the `Context`.
2020-03-05 00:39:56 +00:00
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
2020-03-08 01:21:29 +00:00
self.data.insert(data);
2020-03-05 00:39:56 +00:00
self
}
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-26 03:34:28 +00:00
extensions: self.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-05 00:39:56 +00:00
data: &self.data,
2020-03-01 10:54:34 +00:00
}
}
2020-03-03 11:15:18 +00:00
2020-03-20 03:56:08 +00:00
/// Start a subscribe and return `SubscribeBuilder`.
2020-03-17 09:26:59 +00:00
pub fn subscribe<'a>(&'a self, source: &'a str) -> SubscribeBuilder<'a, Subscription> {
SubscribeBuilder {
2020-03-26 03:34:28 +00:00
extensions: Default::default(),
2020-03-17 09:26:59 +00:00
subscription: &self.subscription,
registry: &self.registry,
source,
operation_name: None,
variables: None,
2020-03-14 03:46:20 +00:00
}
2020-03-01 10:54:34 +00:00
}
}