async-graphql/src/model/schema.rs

70 lines
2.4 KiB
Rust
Raw Normal View History

use crate::model::{__Directive, __Type};
use crate::{registry, Context, Object};
2020-03-03 11:15:18 +00:00
pub struct __Schema<'a> {
pub registry: &'a registry::Registry,
}
/// A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.
2020-10-14 09:08:57 +00:00
#[Object(internal, name = "__Schema")]
2020-03-05 06:23:55 +00:00
impl<'a> __Schema<'a> {
/// A list of all types supported by this server.
async fn types(&self, ctx: &Context<'_>) -> Vec<__Type<'a>> {
2020-10-15 06:38:10 +00:00
let mut types: Vec<_> = self
.registry
.types
2020-03-03 11:15:18 +00:00
.values()
.filter_map(|ty| {
if ty.is_visible(ctx) {
Some((ty.name(), __Type::new_simple(self.registry, ty)))
} else {
None
}
})
2020-10-15 06:38:10 +00:00
.collect();
types.sort_by(|a, b| a.0.cmp(b.0));
types.into_iter().map(|(_, ty)| ty).collect()
2020-03-03 11:15:18 +00:00
}
/// The type that query operations will be rooted at.
async fn query_type(&self) -> __Type<'a> {
2020-03-08 12:35:36 +00:00
__Type::new_simple(
self.registry,
&self.registry.types[&self.registry.query_type],
)
2020-03-03 11:15:18 +00:00
}
/// If this server supports mutation, the type that mutation operations will be rooted at.
async fn mutation_type(&self) -> Option<__Type<'a>> {
2020-03-08 12:35:36 +00:00
if let Some(ty) = &self.registry.mutation_type {
2020-03-05 09:06:14 +00:00
Some(__Type::new_simple(self.registry, &self.registry.types[ty]))
} else {
None
}
2020-03-05 00:39:56 +00:00
}
/// If this server support subscription, the type that subscription operations will be rooted at.
async fn subscription_type(&self) -> Option<__Type<'a>> {
2020-03-17 09:26:59 +00:00
if let Some(ty) = &self.registry.subscription_type {
Some(__Type::new_simple(self.registry, &self.registry.types[ty]))
} else {
None
}
}
/// A list of all directives supported by this server.
async fn directives(&self) -> Vec<__Directive<'a>> {
2020-10-15 06:38:10 +00:00
let mut directives: Vec<_> = self
.registry
.directives
2020-03-08 12:35:36 +00:00
.values()
.map(|directive| __Directive {
registry: &self.registry,
directive,
})
2020-10-15 06:38:10 +00:00
.collect();
directives.sort_by(|a, b| a.directive.name.cmp(b.directive.name));
directives
2020-03-03 11:15:18 +00:00
}
}