Add SchemaBuilder::enable_federation #136

This commit is contained in:
Sunli 2020-06-02 08:49:27 +08:00
parent b38130575c
commit 616b7303ee
2 changed files with 15 additions and 9 deletions

View File

@ -565,7 +565,7 @@ impl Registry {
sdl
}
fn has_entities(&self) -> bool {
pub(crate) fn has_entities(&self) -> bool {
self.types.values().any(|ty| match ty {
MetaType::Object {
keys: Some(keys), ..
@ -607,10 +607,6 @@ impl Registry {
}
pub fn create_federation_types(&mut self) {
if !self.has_entities() {
return;
}
Any::create_type_info(self);
self.types.insert(

View File

@ -33,6 +33,7 @@ pub struct SchemaBuilder<Query, Mutation, Subscription> {
complexity: Option<usize>,
depth: Option<usize>,
extensions: Vec<Box<dyn Fn() -> BoxExtension + Send + Sync>>,
enable_federation: bool,
}
impl<Query: ObjectType, Mutation: ObjectType, Subscription: SubscriptionType>
@ -84,8 +85,19 @@ impl<Query: ObjectType, Mutation: ObjectType, Subscription: SubscriptionType>
self
}
/// Enable federation, which is automatically enabled if the Query has least one entity definition.
pub fn enable_federation(mut self) -> Self {
self.enable_federation = true;
self
}
/// Build schema.
pub fn finish(self) -> Schema<Query, Mutation, Subscription> {
pub fn finish(mut self) -> Schema<Query, Mutation, Subscription> {
// federation
if self.enable_federation || self.registry.has_entities() {
self.registry.create_federation_types();
}
Schema(Arc::new(SchemaInner {
validation_mode: self.validation_mode,
query: self.query,
@ -258,9 +270,6 @@ where
Subscription::create_type_info(&mut registry);
}
// federation
registry.create_federation_types();
SchemaBuilder {
validation_mode: ValidationMode::Strict,
query: QueryRoot {
@ -274,6 +283,7 @@ where
complexity: None,
depth: None,
extensions: Default::default(),
enable_federation: false,
}
}