2020-03-08 12:35:36 +00:00
|
|
|
use crate::{model, GQLType, Value};
|
2020-03-06 15:58:43 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
fn parse_non_null(type_name: &str) -> Option<&str> {
|
|
|
|
if type_name.ends_with("!") {
|
|
|
|
Some(&type_name[..type_name.len() - 1])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_list(type_name: &str) -> Option<&str> {
|
|
|
|
if type_name.starts_with("[") {
|
|
|
|
Some(&type_name[1..type_name.len() - 1])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 04:08:50 +00:00
|
|
|
pub enum TypeName<'a> {
|
2020-03-08 12:35:36 +00:00
|
|
|
List(&'a str),
|
|
|
|
NonNull(&'a str),
|
2020-03-09 04:08:50 +00:00
|
|
|
Name(&'a str),
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 04:08:50 +00:00
|
|
|
impl<'a> TypeName<'a> {
|
|
|
|
pub fn create(type_name: &str) -> TypeName {
|
2020-03-08 12:35:36 +00:00
|
|
|
if let Some(type_name) = parse_non_null(type_name) {
|
2020-03-09 04:08:50 +00:00
|
|
|
TypeName::NonNull(type_name)
|
2020-03-08 12:35:36 +00:00
|
|
|
} else if let Some(type_name) = parse_list(type_name) {
|
2020-03-09 04:08:50 +00:00
|
|
|
TypeName::List(type_name)
|
2020-03-08 12:35:36 +00:00
|
|
|
} else {
|
2020-03-09 04:08:50 +00:00
|
|
|
TypeName::Name(type_name)
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
pub struct InputValue {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
|
|
|
pub ty: String,
|
|
|
|
pub default_value: Option<&'static str>,
|
|
|
|
}
|
2020-03-02 11:25:21 +00:00
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
pub struct Field {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
2020-03-08 12:35:36 +00:00
|
|
|
pub args: HashMap<&'static str, InputValue>,
|
2020-03-03 03:48:00 +00:00
|
|
|
pub ty: String,
|
|
|
|
pub deprecation: Option<&'static str>,
|
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
pub struct EnumValue {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
|
|
|
pub deprecation: Option<&'static str>,
|
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
pub enum Type {
|
2020-03-03 11:15:18 +00:00
|
|
|
Scalar {
|
|
|
|
name: String,
|
|
|
|
description: Option<&'static str>,
|
2020-03-08 12:35:36 +00:00
|
|
|
is_valid: fn(value: &Value) -> bool,
|
2020-03-03 11:15:18 +00:00
|
|
|
},
|
2020-03-03 03:48:00 +00:00
|
|
|
Object {
|
2020-03-03 11:15:18 +00:00
|
|
|
name: &'static str,
|
|
|
|
description: Option<&'static str>,
|
2020-03-08 12:35:36 +00:00
|
|
|
fields: HashMap<&'static str, Field>,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
Interface {
|
2020-03-03 11:15:18 +00:00
|
|
|
name: &'static str,
|
|
|
|
description: Option<&'static str>,
|
2020-03-08 12:35:36 +00:00
|
|
|
fields: HashMap<&'static str, Field>,
|
2020-03-06 15:58:43 +00:00
|
|
|
possible_types: Vec<String>,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
Union {
|
2020-03-03 11:15:18 +00:00
|
|
|
name: &'static str,
|
|
|
|
description: Option<&'static str>,
|
2020-03-08 01:21:29 +00:00
|
|
|
possible_types: Vec<String>,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
Enum {
|
2020-03-03 11:15:18 +00:00
|
|
|
name: &'static str,
|
|
|
|
description: Option<&'static str>,
|
2020-03-08 12:35:36 +00:00
|
|
|
enum_values: HashMap<&'static str, EnumValue>,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
InputObject {
|
2020-03-03 11:15:18 +00:00
|
|
|
name: &'static str,
|
|
|
|
description: Option<&'static str>,
|
2020-03-03 03:48:00 +00:00
|
|
|
input_fields: Vec<InputValue>,
|
|
|
|
},
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
impl Type {
|
|
|
|
pub fn field_by_name(&self, name: &str) -> Option<&Field> {
|
|
|
|
self.fields().and_then(|fields| fields.get(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fields(&self) -> Option<&HashMap<&'static str, Field>> {
|
|
|
|
match self {
|
|
|
|
Type::Object { fields, .. } => Some(&fields),
|
|
|
|
Type::Interface { fields, .. } => Some(&fields),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Type::Scalar { name, .. } => &name,
|
|
|
|
Type::Object { name, .. } => name,
|
|
|
|
Type::Interface { name, .. } => name,
|
|
|
|
Type::Union { name, .. } => name,
|
|
|
|
Type::Enum { name, .. } => name,
|
|
|
|
Type::InputObject { name, .. } => name,
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 04:08:50 +00:00
|
|
|
|
|
|
|
pub fn is_composite(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Type::Object { .. } => true,
|
|
|
|
Type::Interface { .. } => true,
|
|
|
|
Type::Union { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_leaf(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Type::Enum { .. } => true,
|
|
|
|
Type::Scalar { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 06:24:44 +00:00
|
|
|
pub struct Directive {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
|
|
|
pub locations: Vec<model::__DirectiveLocation>,
|
2020-03-08 12:35:36 +00:00
|
|
|
pub args: HashMap<&'static str, InputValue>,
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Registry {
|
|
|
|
pub types: HashMap<String, Type>,
|
2020-03-08 12:35:36 +00:00
|
|
|
pub directives: HashMap<String, Directive>,
|
2020-03-06 15:58:43 +00:00
|
|
|
pub implements: HashMap<String, HashSet<String>>,
|
2020-03-08 12:35:36 +00:00
|
|
|
pub query_type: String,
|
|
|
|
pub mutation_type: Option<String>,
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registry {
|
|
|
|
pub fn create_type<T: GQLType, F: FnMut(&mut Registry) -> Type>(&mut self, mut f: F) -> String {
|
|
|
|
let name = T::type_name();
|
|
|
|
if !self.types.contains_key(name.as_ref()) {
|
2020-03-05 13:34:31 +00:00
|
|
|
self.types.insert(
|
|
|
|
name.to_string(),
|
2020-03-08 12:35:36 +00:00
|
|
|
Type::Object {
|
|
|
|
name: "",
|
2020-03-05 13:34:31 +00:00
|
|
|
description: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
fields: Default::default(),
|
2020-03-05 13:34:31 +00:00
|
|
|
},
|
|
|
|
);
|
2020-03-08 12:35:36 +00:00
|
|
|
let mut ty = f(self);
|
|
|
|
if let Type::Object { fields, .. } = &mut ty {
|
|
|
|
fields.insert(
|
|
|
|
"__typename",
|
|
|
|
Field {
|
2020-03-09 04:08:50 +00:00
|
|
|
name: "__typename",
|
2020-03-08 12:35:36 +00:00
|
|
|
description: None,
|
|
|
|
args: Default::default(),
|
|
|
|
ty: "String!".to_string(),
|
|
|
|
deprecation: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
self.types.insert(name.to_string(), ty);
|
|
|
|
}
|
|
|
|
T::qualified_type_name()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_directive(&mut self, directive: Directive) {
|
2020-03-08 12:35:36 +00:00
|
|
|
self.directives
|
|
|
|
.insert(directive.name.to_string(), directive);
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
pub fn add_implements(&mut self, ty: &str, interface: &str) {
|
|
|
|
self.implements
|
|
|
|
.entry(ty.to_string())
|
|
|
|
.and_modify(|interfaces| {
|
|
|
|
interfaces.insert(interface.to_string());
|
|
|
|
})
|
|
|
|
.or_insert({
|
|
|
|
let mut interfaces = HashSet::new();
|
|
|
|
interfaces.insert(interface.to_string());
|
|
|
|
interfaces
|
|
|
|
});
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
|
|
|
|
pub fn get_basic_type(&self, type_name: &str) -> Option<&Type> {
|
2020-03-09 04:08:50 +00:00
|
|
|
match TypeName::create(type_name) {
|
|
|
|
TypeName::Name(type_name) => self.types.get(type_name),
|
|
|
|
TypeName::List(type_name) => self.get_basic_type(type_name),
|
|
|
|
TypeName::NonNull(type_name) => self.get_basic_type(type_name),
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|