async-graphql/src/registry.rs

92 lines
2.2 KiB
Rust
Raw Normal View History

use crate::{model, GQLType};
use std::collections::HashMap;
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>,
pub args: Vec<InputValue>,
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-03 03:48:00 +00:00
Object {
2020-03-03 11:15:18 +00:00
name: &'static str,
description: Option<&'static str>,
2020-03-03 03:48:00 +00:00
fields: Vec<Field>,
},
Interface {
2020-03-03 11:15:18 +00:00
name: &'static str,
description: Option<&'static str>,
2020-03-03 03:48:00 +00:00
fields: Vec<Field>,
possible_types: Vec<usize>,
},
Union {
2020-03-03 11:15:18 +00:00
name: &'static str,
description: Option<&'static str>,
2020-03-03 03:48:00 +00:00
possible_types: Vec<usize>,
},
Enum {
2020-03-03 11:15:18 +00:00
name: &'static str,
description: Option<&'static str>,
2020-03-03 03:48:00 +00:00
enum_values: Vec<EnumValue>,
},
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>,
},
}
pub struct Directive {
pub name: &'static str,
pub description: Option<&'static str>,
pub locations: Vec<model::__DirectiveLocation>,
pub args: Vec<InputValue>,
}
#[derive(Default)]
pub struct Registry {
pub types: HashMap<String, Type>,
pub directives: Vec<Directive>,
}
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(),
Type::Scalar {
name: String::new(),
description: None,
},
);
let ty = f(self);
self.types.insert(name.to_string(), ty);
}
T::qualified_type_name()
}
pub fn add_directive(&mut self, directive: Directive) {
self.directives.push(directive);
}
2020-03-02 00:24:49 +00:00
}