2021-11-11 07:22:40 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use crate::{
|
|
|
|
model::{__Directive, __Type},
|
|
|
|
registry, Object,
|
|
|
|
};
|
2020-03-03 11:15:18 +00:00
|
|
|
|
|
|
|
pub struct __Schema<'a> {
|
2021-11-11 07:22:40 +00:00
|
|
|
registry: &'a registry::Registry,
|
|
|
|
visible_types: &'a HashSet<&'a str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> __Schema<'a> {
|
|
|
|
pub fn new(registry: &'a registry::Registry, visible_types: &'a HashSet<&'a str>) -> Self {
|
|
|
|
Self {
|
|
|
|
registry,
|
|
|
|
visible_types,
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// 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> {
|
2020-05-10 04:41:05 +00:00
|
|
|
/// A list of all types supported by this server.
|
2021-11-11 07:22:40 +00:00
|
|
|
async fn types(&self) -> Vec<__Type<'a>> {
|
2020-10-15 06:38:10 +00:00
|
|
|
let mut types: Vec<_> = self
|
2020-05-18 03:37:05 +00:00
|
|
|
.registry
|
2020-03-04 06:24:44 +00:00
|
|
|
.types
|
2020-03-03 11:15:18 +00:00
|
|
|
.values()
|
2020-12-11 08:03:28 +00:00
|
|
|
.filter_map(|ty| {
|
2021-11-11 07:22:40 +00:00
|
|
|
if self.visible_types.contains(ty.name()) {
|
|
|
|
Some((
|
|
|
|
ty.name(),
|
|
|
|
__Type::new_simple(self.registry, self.visible_types, ty),
|
|
|
|
))
|
2020-12-11 08:03:28 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-10-15 06:38:10 +00:00
|
|
|
.collect();
|
2020-05-18 03:37:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-10 04:41:05 +00:00
|
|
|
/// The type that query operations will be rooted at.
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-28 12:11:26 +00:00
|
|
|
async fn query_type(&self) -> __Type<'a> {
|
2020-03-08 12:35:36 +00:00
|
|
|
__Type::new_simple(
|
|
|
|
self.registry,
|
2021-11-11 07:22:40 +00:00
|
|
|
self.visible_types,
|
2020-03-08 12:35:36 +00:00
|
|
|
&self.registry.types[&self.registry.query_type],
|
|
|
|
)
|
2020-03-03 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// If this server supports mutation, the type that mutation operations will
|
|
|
|
/// be rooted at.
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-28 12:11:26 +00:00
|
|
|
async fn mutation_type(&self) -> Option<__Type<'a>> {
|
2021-11-11 07:22:40 +00:00
|
|
|
self.registry.mutation_type.as_ref().and_then(|ty| {
|
|
|
|
if self.visible_types.contains(ty.as_str()) {
|
|
|
|
Some(__Type::new_simple(
|
|
|
|
self.registry,
|
|
|
|
self.visible_types,
|
|
|
|
&self.registry.types[ty],
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-03-05 00:39:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// If this server support subscription, the type that subscription
|
|
|
|
/// operations will be rooted at.
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-28 12:11:26 +00:00
|
|
|
async fn subscription_type(&self) -> Option<__Type<'a>> {
|
2021-11-11 07:22:40 +00:00
|
|
|
self.registry.subscription_type.as_ref().and_then(|ty| {
|
|
|
|
if self.visible_types.contains(ty.as_str()) {
|
|
|
|
Some(__Type::new_simple(
|
|
|
|
self.registry,
|
|
|
|
self.visible_types,
|
|
|
|
&self.registry.types[ty],
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 04:41:05 +00:00
|
|
|
/// A list of all directives supported by this server.
|
2020-03-28 12:11:26 +00:00
|
|
|
async fn directives(&self) -> Vec<__Directive<'a>> {
|
2020-10-15 06:38:10 +00:00
|
|
|
let mut directives: Vec<_> = self
|
2020-05-18 03:37:05 +00:00
|
|
|
.registry
|
2020-03-04 06:24:44 +00:00
|
|
|
.directives
|
2020-03-08 12:35:36 +00:00
|
|
|
.values()
|
2020-03-04 06:24:44 +00:00
|
|
|
.map(|directive| __Directive {
|
2021-11-11 07:22:40 +00:00
|
|
|
registry: self.registry,
|
|
|
|
visible_types: self.visible_types,
|
2020-03-04 06:24:44 +00:00
|
|
|
directive,
|
|
|
|
})
|
2020-10-15 06:38:10 +00:00
|
|
|
.collect();
|
2020-05-18 03:37:05 +00:00
|
|
|
directives.sort_by(|a, b| a.directive.name.cmp(b.directive.name));
|
|
|
|
directives
|
2020-03-03 11:15:18 +00:00
|
|
|
}
|
|
|
|
}
|