Add Schema::names function.

This commit is contained in:
Sunli 2020-10-15 19:36:54 +08:00
parent f764edc7d1
commit c99e0b2e54
2 changed files with 55 additions and 4 deletions

View File

@ -451,4 +451,48 @@ impl Registry {
);
}
}
pub fn names(&self) -> Vec<String> {
let mut names = HashSet::new();
for d in self.directives.values() {
names.insert(d.name.to_string());
names.extend(d.args.values().map(|arg| arg.name.to_string()));
}
for ty in self.types.values() {
match ty {
MetaType::Scalar { name, .. } | MetaType::Union { name, .. } => {
names.insert(name.clone());
}
MetaType::Object { name, fields, .. }
| MetaType::Interface { name, fields, .. } => {
names.insert(name.clone());
names.extend(
fields
.values()
.map(|field| {
std::iter::once(field.name.clone())
.chain(field.args.values().map(|arg| arg.name.to_string()))
})
.flatten(),
);
}
MetaType::Enum {
name, enum_values, ..
} => {
names.insert(name.clone());
names.extend(enum_values.values().map(|value| value.name.to_string()));
}
MetaType::InputObject {
name, input_fields, ..
} => {
names.insert(name.clone());
names.extend(input_fields.values().map(|field| field.name.to_string()));
}
}
}
names.into_iter().collect()
}
}

View File

@ -14,7 +14,7 @@ use crate::model::__DirectiveLocation;
use crate::parser::parse_query;
use crate::parser::types::{DocumentOperations, OperationType};
use crate::registry::{MetaDirective, MetaInputValue, Registry};
use crate::resolver_utils::{resolve_container, resolve_container_serial, ContainerType};
use crate::resolver_utils::{resolve_container, resolve_container_serial};
use crate::subscription::collect_subscription_streams;
use crate::types::QueryRoot;
use crate::validation::{check_rules, CheckResult, ValidationMode};
@ -37,9 +37,7 @@ pub struct SchemaBuilder<Query, Mutation, Subscription> {
enable_federation: bool,
}
impl<Query: ContainerType, Mutation: ContainerType, Subscription: SubscriptionType>
SchemaBuilder<Query, Mutation, Subscription>
{
impl<Query, Mutation, Subscription> SchemaBuilder<Query, Mutation, Subscription> {
/// Manually register a type in the schema.
///
/// You can use this function to register schema types that are not directly referenced.
@ -322,6 +320,15 @@ where
Self::create_registry().export_sdl(false)
}
/// Get all names in this schema
///
/// Maybe you want to serialize a custom binary protocol. In order to minimize message size, a dictionary
/// is usually used to compress type names, field names, directive names, and parameter names. This function gets all the names
/// so you can create this dictionary.
pub fn names(&self) -> Vec<String> {
self.0.env.registry.names()
}
async fn prepare_request(
&self,
request: Request,