2020-03-21 01:32:13 +00:00
|
|
|
use crate::validators::InputValueValidator;
|
2020-03-19 09:20:12 +00:00
|
|
|
use crate::{model, Value};
|
2020-03-14 03:46:20 +00:00
|
|
|
use graphql_parser::query::Type as ParsedType;
|
2020-03-06 15:58:43 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2020-03-21 01:32:13 +00:00
|
|
|
use std::sync::Arc;
|
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> {
|
2020-03-21 01:32:13 +00:00
|
|
|
if type_name.ends_with('!') {
|
2020-03-08 12:35:36 +00:00
|
|
|
Some(&type_name[..type_name.len() - 1])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_list(type_name: &str) -> Option<&str> {
|
2020-03-21 01:32:13 +00:00
|
|
|
if type_name.starts_with('[') {
|
2020-03-08 12:35:36 +00:00
|
|
|
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-10 06:14:09 +00:00
|
|
|
Named(&'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-10 06:14:09 +00:00
|
|
|
TypeName::Named(type_name)
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 10:05:52 +00:00
|
|
|
|
|
|
|
pub fn get_basic_typename(type_name: &str) -> &str {
|
|
|
|
match TypeName::create(type_name) {
|
|
|
|
TypeName::List(type_name) => Self::get_basic_typename(type_name),
|
|
|
|
TypeName::NonNull(type_name) => Self::get_basic_typename(type_name),
|
2020-03-10 06:14:09 +00:00
|
|
|
TypeName::Named(type_name) => type_name,
|
2020-03-09 10:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 12:35:25 +00:00
|
|
|
|
|
|
|
pub fn is_non_null(&self) -> bool {
|
|
|
|
if let TypeName::NonNull(_) = self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
#[derive(Clone)]
|
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-22 01:34:32 +00:00
|
|
|
pub validator: Option<Arc<dyn InputValueValidator>>,
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-02 11:25:21 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
#[derive(Clone)]
|
2020-03-03 03:48:00 +00:00
|
|
|
pub struct Field {
|
2020-03-19 09:20:12 +00:00
|
|
|
pub name: String,
|
2020-03-03 03:48:00 +00:00
|
|
|
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-22 08:45:59 +00:00
|
|
|
pub cache_control: CacheControl,
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
#[derive(Clone)]
|
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-22 08:45:59 +00:00
|
|
|
/// Cache control values
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use async_graphql::*;
|
|
|
|
///
|
|
|
|
/// struct QueryRoot;
|
|
|
|
///
|
|
|
|
/// #[Object(cache_control(max_age = 60))]
|
|
|
|
/// impl QueryRoot {
|
|
|
|
/// #[field(cache_control(max_age = 30))]
|
|
|
|
/// async fn value1(&self) -> i32 {
|
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[field(cache_control(private))]
|
|
|
|
/// async fn value2(&self) -> i32 {
|
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[async_std::main]
|
|
|
|
/// async fn main() {
|
|
|
|
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
2020-04-01 08:53:49 +00:00
|
|
|
/// assert_eq!(schema.query("{ value1 }").unwrap().cache_control(), CacheControl { public: true, max_age: 30 });
|
|
|
|
/// assert_eq!(schema.query("{ value2 }").unwrap().cache_control(), CacheControl { public: false, max_age: 60 });
|
|
|
|
/// assert_eq!(schema.query("{ value1 value2 }").unwrap().cache_control(), CacheControl { public: false, max_age: 30 });
|
2020-03-22 08:45:59 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
pub struct CacheControl {
|
|
|
|
/// Scope is public, default is true
|
|
|
|
pub public: bool,
|
|
|
|
|
|
|
|
/// Cache max age, default is 0.
|
|
|
|
pub max_age: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CacheControl {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
public: true,
|
|
|
|
max_age: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheControl {
|
|
|
|
/// Get 'Cache-Control' header value.
|
|
|
|
pub fn value(&self) -> Option<String> {
|
|
|
|
if self.max_age > 0 {
|
|
|
|
if !self.public {
|
|
|
|
Some(format!("max-age={}, private", self.max_age))
|
|
|
|
} else {
|
|
|
|
Some(format!("max-age={}", self.max_age))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheControl {
|
2020-03-24 10:54:22 +00:00
|
|
|
pub(crate) fn merge(&mut self, other: &CacheControl) {
|
|
|
|
self.public = self.public && other.public;
|
|
|
|
self.max_age = if self.max_age == 0 {
|
|
|
|
other.max_age
|
|
|
|
} else if other.max_age == 0 {
|
|
|
|
self.max_age
|
|
|
|
} else {
|
|
|
|
self.max_age.min(other.max_age)
|
|
|
|
};
|
2020-03-22 08:45:59 +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-19 09:20:12 +00:00
|
|
|
name: String,
|
2020-03-03 11:15:18 +00:00
|
|
|
description: Option<&'static str>,
|
2020-03-19 09:20:12 +00:00
|
|
|
fields: HashMap<String, Field>,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: CacheControl,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
Interface {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: String,
|
2020-03-03 11:15:18 +00:00
|
|
|
description: Option<&'static str>,
|
2020-03-19 09:20:12 +00:00
|
|
|
fields: HashMap<String, Field>,
|
2020-03-10 10:07:47 +00:00
|
|
|
possible_types: HashSet<String>,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
Union {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: String,
|
2020-03-03 11:15:18 +00:00
|
|
|
description: Option<&'static str>,
|
2020-03-10 10:07:47 +00:00
|
|
|
possible_types: HashSet<String>,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
|
|
|
Enum {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: String,
|
2020-03-03 11:15:18 +00:00
|
|
|
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-19 09:20:12 +00:00
|
|
|
name: String,
|
2020-03-03 11:15:18 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
pub fn fields(&self) -> Option<&HashMap<String, Field>> {
|
2020-03-08 12:35:36 +00:00
|
|
|
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-10 06:14:09 +00:00
|
|
|
|
|
|
|
pub fn is_input(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Type::Enum { .. } => true,
|
|
|
|
Type::Scalar { .. } => true,
|
|
|
|
Type::InputObject { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 10:07:47 +00:00
|
|
|
|
|
|
|
pub fn is_possible_type(&self, type_name: &str) -> bool {
|
|
|
|
match self {
|
|
|
|
Type::Interface { possible_types, .. } => possible_types.contains(type_name),
|
|
|
|
Type::Union { possible_types, .. } => possible_types.contains(type_name),
|
2020-04-03 14:19:15 +00:00
|
|
|
Type::Object { name, .. } => name == type_name,
|
2020-03-10 10:07:47 +00:00
|
|
|
_ => 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-17 09:26:59 +00:00
|
|
|
pub subscription_type: Option<String>,
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registry {
|
2020-03-19 09:20:12 +00:00
|
|
|
pub fn create_type<T: crate::Type, F: FnMut(&mut Registry) -> Type>(
|
|
|
|
&mut self,
|
|
|
|
mut f: F,
|
|
|
|
) -> String {
|
2020-03-04 06:24:44 +00:00
|
|
|
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 {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: "".to_string(),
|
2020-03-05 13:34:31 +00:00
|
|
|
description: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
fields: Default::default(),
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-03-05 13:34:31 +00:00
|
|
|
},
|
|
|
|
);
|
2020-04-02 12:37:01 +00:00
|
|
|
let ty = f(self);
|
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
|
|
|
|
2020-03-14 03:46:20 +00:00
|
|
|
pub fn basic_type_by_typename(&self, type_name: &str) -> Option<&Type> {
|
2020-03-09 10:05:52 +00:00
|
|
|
self.types.get(TypeName::get_basic_typename(type_name))
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
2020-03-14 03:46:20 +00:00
|
|
|
|
|
|
|
pub fn basic_type_by_parsed_type(&self, query_type: &ParsedType) -> Option<&Type> {
|
|
|
|
match query_type {
|
|
|
|
ParsedType::NonNullType(ty) => self.basic_type_by_parsed_type(ty),
|
|
|
|
ParsedType::ListType(ty) => self.basic_type_by_parsed_type(ty),
|
|
|
|
ParsedType::NamedType(name) => self.types.get(name.as_str()),
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|