2020-09-14 00:37:15 +00:00
|
|
|
mod cache_control;
|
2020-09-23 00:04:00 +00:00
|
|
|
mod export_sdl;
|
2021-04-07 08:23:28 +00:00
|
|
|
mod stringify_exec_doc;
|
2020-09-14 00:37:15 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
|
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
};
|
2020-10-15 06:38:10 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
pub use cache_control::CacheControl;
|
2022-05-05 12:09:35 +00:00
|
|
|
pub use export_sdl::SDLExportOptions;
|
2022-04-19 04:25:11 +00:00
|
|
|
use indexmap::{map::IndexMap, set::IndexSet};
|
2020-10-15 06:38:10 +00:00
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
pub use crate::model::__DirectiveLocation;
|
2021-11-09 10:52:10 +00:00
|
|
|
use crate::{
|
2022-04-13 03:00:24 +00:00
|
|
|
parser::types::{BaseType as ParsedBaseType, Field, Type as ParsedType, VariableDefinition},
|
|
|
|
schema::IntrospectionMode,
|
2022-04-19 04:25:11 +00:00
|
|
|
Any, Context, InputType, OutputType, Positioned, ServerResult, SubscriptionType, Value,
|
|
|
|
VisitorContext,
|
2022-04-13 03:00:24 +00:00
|
|
|
};
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
fn strip_brackets(type_name: &str) -> Option<&str> {
|
2021-05-07 02:14:05 +00:00
|
|
|
type_name
|
|
|
|
.strip_prefix('[')
|
|
|
|
.map(|rest| &rest[..rest.len() - 1])
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 06:05:46 +00:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub enum MetaTypeName<'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
|
|
|
}
|
|
|
|
|
2022-05-06 01:28:57 +00:00
|
|
|
impl<'a> Display for MetaTypeName<'a> {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2020-04-05 08:00:26 +00:00
|
|
|
match self {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaTypeName::Named(name) => write!(f, "{}", name),
|
|
|
|
MetaTypeName::NonNull(name) => write!(f, "{}!", name),
|
|
|
|
MetaTypeName::List(name) => write!(f, "[{}]", name),
|
2020-04-05 08:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
impl<'a> MetaTypeName<'a> {
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn create(type_name: &str) -> MetaTypeName {
|
2020-09-06 05:38:31 +00:00
|
|
|
if let Some(type_name) = type_name.strip_suffix('!') {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaTypeName::NonNull(type_name)
|
2020-09-06 05:38:31 +00:00
|
|
|
} else if let Some(type_name) = strip_brackets(type_name) {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaTypeName::List(type_name)
|
2020-03-08 12:35:36 +00:00
|
|
|
} else {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaTypeName::Named(type_name)
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 10:05:52 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-04-06 10:30:38 +00:00
|
|
|
pub fn concrete_typename(type_name: &str) -> &str {
|
2020-05-15 02:08:37 +00:00
|
|
|
match MetaTypeName::create(type_name) {
|
|
|
|
MetaTypeName::List(type_name) => Self::concrete_typename(type_name),
|
|
|
|
MetaTypeName::NonNull(type_name) => Self::concrete_typename(type_name),
|
|
|
|
MetaTypeName::Named(type_name) => type_name,
|
2020-03-09 10:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 12:35:25 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-10 12:35:25 +00:00
|
|
|
pub fn is_non_null(&self) -> bool {
|
2020-09-06 05:38:31 +00:00
|
|
|
matches!(self, MetaTypeName::NonNull(_))
|
2020-03-10 12:35:25 +00:00
|
|
|
}
|
2020-04-05 08:00:26 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2022-01-11 01:35:09 +00:00
|
|
|
#[must_use]
|
2020-04-05 08:00:26 +00:00
|
|
|
pub fn unwrap_non_null(&self) -> Self {
|
|
|
|
match self {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaTypeName::NonNull(ty) => MetaTypeName::create(ty),
|
2020-04-05 08:14:22 +00:00
|
|
|
_ => *self,
|
2020-04-05 08:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn is_subtype(&self, sub: &MetaTypeName<'_>) -> bool {
|
2020-04-05 08:00:26 +00:00
|
|
|
match (self, sub) {
|
2020-05-15 02:08:37 +00:00
|
|
|
(MetaTypeName::NonNull(super_type), MetaTypeName::NonNull(sub_type))
|
|
|
|
| (MetaTypeName::Named(super_type), MetaTypeName::NonNull(sub_type)) => {
|
|
|
|
MetaTypeName::create(super_type).is_subtype(&MetaTypeName::create(sub_type))
|
2020-04-05 08:00:26 +00:00
|
|
|
}
|
2020-05-15 02:08:37 +00:00
|
|
|
(MetaTypeName::Named(super_type), MetaTypeName::Named(sub_type)) => {
|
|
|
|
super_type == sub_type
|
|
|
|
}
|
|
|
|
(MetaTypeName::List(super_type), MetaTypeName::List(sub_type)) => {
|
|
|
|
MetaTypeName::create(super_type).is_subtype(&MetaTypeName::create(sub_type))
|
2020-04-05 08:00:26 +00:00
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 06:59:37 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-12-18 06:59:37 +00:00
|
|
|
pub fn is_list(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
MetaTypeName::List(_) => true,
|
|
|
|
MetaTypeName::NonNull(ty) => MetaTypeName::create(ty).is_list(),
|
|
|
|
MetaTypeName::Named(name) => name.ends_with(']'),
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
#[derive(Clone)]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub struct MetaInputValue {
|
2020-03-03 03:48:00 +00:00
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
|
|
|
pub ty: String,
|
2020-05-26 10:34:43 +00:00
|
|
|
pub default_value: Option<String>,
|
2020-12-11 08:03:28 +00:00
|
|
|
pub visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
pub inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
pub tags: &'static [&'static str],
|
2021-04-07 08:23:28 +00:00
|
|
|
pub is_secret: bool,
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-02 11:25:21 +00:00
|
|
|
|
2020-12-18 06:59:37 +00:00
|
|
|
type ComputeComplexityFn = fn(
|
|
|
|
&VisitorContext<'_>,
|
|
|
|
&[Positioned<VariableDefinition>],
|
|
|
|
&Field,
|
|
|
|
usize,
|
|
|
|
) -> ServerResult<usize>;
|
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
#[derive(Clone)]
|
2020-12-18 06:59:37 +00:00
|
|
|
pub enum ComplexityType {
|
|
|
|
Const(usize),
|
|
|
|
Fn(ComputeComplexityFn),
|
|
|
|
}
|
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-02-27 03:59:58 +00:00
|
|
|
pub enum Deprecation {
|
|
|
|
NoDeprecated,
|
|
|
|
Deprecated { reason: Option<&'static str> },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Deprecation {
|
|
|
|
fn default() -> Self {
|
|
|
|
Deprecation::NoDeprecated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deprecation {
|
|
|
|
#[inline]
|
|
|
|
pub fn is_deprecated(&self) -> bool {
|
|
|
|
matches!(self, Deprecation::Deprecated { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn reason(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
Deprecation::NoDeprecated => None,
|
|
|
|
Deprecation::Deprecated { reason } => reason.as_deref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
#[derive(Clone)]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub struct MetaField {
|
2020-03-19 09:20:12 +00:00
|
|
|
pub name: String,
|
2020-03-03 03:48:00 +00:00
|
|
|
pub description: Option<&'static str>,
|
2022-02-28 07:26:33 +00:00
|
|
|
pub args: IndexMap<String, MetaInputValue>,
|
2020-03-03 03:48:00 +00:00
|
|
|
pub ty: String,
|
2021-02-27 03:59:58 +00:00
|
|
|
pub deprecation: Deprecation,
|
2020-03-22 08:45:59 +00:00
|
|
|
pub cache_control: CacheControl,
|
2020-04-09 14:03:09 +00:00
|
|
|
pub external: bool,
|
|
|
|
pub requires: Option<&'static str>,
|
|
|
|
pub provides: Option<&'static str>,
|
2020-12-11 08:03:28 +00:00
|
|
|
pub visible: Option<MetaVisibleFn>,
|
2022-08-17 01:29:43 +00:00
|
|
|
pub shareable: bool,
|
2022-08-18 09:40:04 +00:00
|
|
|
pub inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
pub tags: &'static [&'static str],
|
2022-08-19 21:47:56 +00:00
|
|
|
pub override_from: Option<&'static str>,
|
2020-12-18 06:59:37 +00:00
|
|
|
pub compute_complexity: Option<ComplexityType>,
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
#[derive(Clone)]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub struct MetaEnumValue {
|
2020-03-03 03:48:00 +00:00
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
2021-02-27 03:59:58 +00:00
|
|
|
pub deprecation: Deprecation,
|
2020-12-11 08:03:28 +00:00
|
|
|
pub visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
pub inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
pub tags: &'static [&'static str],
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2020-12-11 08:03:28 +00:00
|
|
|
type MetaVisibleFn = fn(&Context<'_>) -> bool;
|
|
|
|
|
2022-03-30 12:54:49 +00:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub enum MetaTypeId {
|
|
|
|
Scalar,
|
|
|
|
Object,
|
|
|
|
Interface,
|
|
|
|
Union,
|
|
|
|
Enum,
|
|
|
|
InputObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for MetaTypeId {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str(match self {
|
|
|
|
MetaTypeId::Scalar => "Scalar",
|
|
|
|
MetaTypeId::Object => "Object",
|
|
|
|
MetaTypeId::Interface => "Interface",
|
|
|
|
MetaTypeId::Union => "Union",
|
|
|
|
MetaTypeId::Enum => "Enum",
|
|
|
|
MetaTypeId::InputObject => "InputObject",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
#[derive(Clone)]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub enum MetaType {
|
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-12-11 08:03:28 +00:00
|
|
|
visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &'static [&'static str],
|
2021-10-28 07:21:42 +00:00
|
|
|
specified_by_url: Option<&'static str>,
|
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-05-16 02:05:48 +00:00
|
|
|
fields: IndexMap<String, MetaField>,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: CacheControl,
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: bool,
|
2022-08-17 02:36:00 +00:00
|
|
|
shareable: bool,
|
2020-04-09 14:03:09 +00:00
|
|
|
keys: Option<Vec<String>>,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &'static [&'static str],
|
2021-10-28 08:56:08 +00:00
|
|
|
is_subscription: bool,
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: &'static str,
|
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-05-16 02:05:48 +00:00
|
|
|
fields: IndexMap<String, MetaField>,
|
|
|
|
possible_types: IndexSet<String>,
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: bool,
|
|
|
|
keys: Option<Vec<String>>,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &'static [&'static str],
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: &'static str,
|
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-05-16 02:05:48 +00:00
|
|
|
possible_types: IndexSet<String>,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &'static [&'static str],
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: &'static str,
|
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-05-16 02:05:48 +00:00
|
|
|
enum_values: IndexMap<&'static str, MetaEnumValue>,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &'static [&'static str],
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: &'static str,
|
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-05-16 02:05:48 +00:00
|
|
|
input_fields: IndexMap<String, MetaInputValue>,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: Option<MetaVisibleFn>,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: bool,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &'static [&'static str],
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: &'static str,
|
2022-02-17 08:55:32 +00:00
|
|
|
oneof: bool,
|
2020-03-03 03:48:00 +00:00
|
|
|
},
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
impl MetaType {
|
2022-03-30 12:54:49 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn type_id(&self) -> MetaTypeId {
|
|
|
|
match self {
|
|
|
|
MetaType::Scalar { .. } => MetaTypeId::Scalar,
|
|
|
|
MetaType::Object { .. } => MetaTypeId::Object,
|
|
|
|
MetaType::Interface { .. } => MetaTypeId::Interface,
|
|
|
|
MetaType::Union { .. } => MetaTypeId::Union,
|
|
|
|
MetaType::Enum { .. } => MetaTypeId::Enum,
|
|
|
|
MetaType::InputObject { .. } => MetaTypeId::InputObject,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn field_by_name(&self, name: &str) -> Option<&MetaField> {
|
2020-03-08 12:35:36 +00:00
|
|
|
self.fields().and_then(|fields| fields.get(name))
|
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-05-16 02:05:48 +00:00
|
|
|
pub fn fields(&self) -> Option<&IndexMap<String, MetaField>> {
|
2020-03-08 12:35:36 +00:00
|
|
|
match self {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Object { fields, .. } => Some(&fields),
|
|
|
|
MetaType::Interface { fields, .. } => Some(&fields),
|
2020-03-08 12:35:36 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-12-11 08:03:28 +00:00
|
|
|
pub fn is_visible(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
let visible = match self {
|
|
|
|
MetaType::Scalar { visible, .. } => visible,
|
|
|
|
MetaType::Object { visible, .. } => visible,
|
|
|
|
MetaType::Interface { visible, .. } => visible,
|
|
|
|
MetaType::Union { visible, .. } => visible,
|
|
|
|
MetaType::Enum { visible, .. } => visible,
|
|
|
|
MetaType::InputObject { visible, .. } => visible,
|
|
|
|
};
|
2021-11-11 07:22:40 +00:00
|
|
|
is_visible(ctx, visible)
|
2020-12-11 08:03:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-08 12:35:36 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
match self {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Scalar { name, .. } => &name,
|
|
|
|
MetaType::Object { name, .. } => name,
|
|
|
|
MetaType::Interface { name, .. } => name,
|
|
|
|
MetaType::Union { name, .. } => name,
|
|
|
|
MetaType::Enum { name, .. } => name,
|
|
|
|
MetaType::InputObject { name, .. } => name,
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 04:08:50 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-09 04:08:50 +00:00
|
|
|
pub fn is_composite(&self) -> bool {
|
2021-02-13 01:28:05 +00:00
|
|
|
matches!(
|
|
|
|
self,
|
|
|
|
MetaType::Object { .. } | MetaType::Interface { .. } | MetaType::Union { .. }
|
|
|
|
)
|
2020-03-09 04:08:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-04-05 08:00:26 +00:00
|
|
|
pub fn is_abstract(&self) -> bool {
|
2021-01-04 00:18:28 +00:00
|
|
|
matches!(self, MetaType::Interface { .. } | MetaType::Union { .. })
|
2020-04-05 08:00:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-09 04:08:50 +00:00
|
|
|
pub fn is_leaf(&self) -> bool {
|
2021-01-04 00:18:28 +00:00
|
|
|
matches!(self, MetaType::Enum { .. } | MetaType::Scalar { .. })
|
2020-03-09 04:08:50 +00:00
|
|
|
}
|
2020-03-10 06:14:09 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-10 06:14:09 +00:00
|
|
|
pub fn is_input(&self) -> bool {
|
2021-02-13 01:28:05 +00:00
|
|
|
matches!(
|
|
|
|
self,
|
|
|
|
MetaType::Enum { .. } | MetaType::Scalar { .. } | MetaType::InputObject { .. }
|
|
|
|
)
|
2020-03-10 06:14:09 +00:00
|
|
|
}
|
2020-03-10 10:07:47 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-03-10 10:07:47 +00:00
|
|
|
pub fn is_possible_type(&self, type_name: &str) -> bool {
|
|
|
|
match self {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Interface { possible_types, .. } => possible_types.contains(type_name),
|
|
|
|
MetaType::Union { possible_types, .. } => possible_types.contains(type_name),
|
|
|
|
MetaType::Object { name, .. } => name == type_name,
|
2020-03-10 10:07:47 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 08:00:26 +00:00
|
|
|
|
2021-02-27 03:59:58 +00:00
|
|
|
#[inline]
|
2020-05-16 02:05:48 +00:00
|
|
|
pub fn possible_types(&self) -> Option<&IndexSet<String>> {
|
2020-04-05 08:00:26 +00:00
|
|
|
match self {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Interface { possible_types, .. } => Some(possible_types),
|
|
|
|
MetaType::Union { possible_types, .. } => Some(possible_types),
|
2020-04-05 08:00:26 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn type_overlap(&self, ty: &MetaType) -> bool {
|
2021-01-04 00:18:28 +00:00
|
|
|
if std::ptr::eq(self, ty) {
|
2020-04-05 08:00:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
match (self.is_abstract(), ty.is_abstract()) {
|
|
|
|
(true, true) => self
|
|
|
|
.possible_types()
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.flatten()
|
|
|
|
.any(|type_name| ty.is_possible_type(type_name)),
|
|
|
|
(true, false) => self.is_possible_type(ty.name()),
|
|
|
|
(false, true) => ty.is_possible_type(self.name()),
|
|
|
|
(false, false) => false,
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 01:33:20 +00:00
|
|
|
|
|
|
|
pub fn rust_typename(&self) -> Option<&'static str> {
|
|
|
|
match self {
|
|
|
|
MetaType::Scalar { .. } => None,
|
|
|
|
MetaType::Object { rust_typename, .. } => Some(rust_typename),
|
|
|
|
MetaType::Interface { rust_typename, .. } => Some(rust_typename),
|
|
|
|
MetaType::Union { rust_typename, .. } => Some(rust_typename),
|
|
|
|
MetaType::Enum { rust_typename, .. } => Some(rust_typename),
|
|
|
|
MetaType::InputObject { rust_typename, .. } => Some(rust_typename),
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
pub struct MetaDirective {
|
2020-03-04 06:24:44 +00:00
|
|
|
pub name: &'static str,
|
|
|
|
pub description: Option<&'static str>,
|
2022-05-06 01:28:57 +00:00
|
|
|
pub locations: Vec<__DirectiveLocation>,
|
2022-02-28 07:26:33 +00:00
|
|
|
pub args: IndexMap<String, MetaInputValue>,
|
2021-11-18 12:14:56 +00:00
|
|
|
pub is_repeatable: bool,
|
2021-11-19 10:49:37 +00:00
|
|
|
pub visible: Option<MetaVisibleFn>,
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 16:04:12 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ApolloLinkConfig {
|
|
|
|
pub extend_schema: bool,
|
|
|
|
}
|
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
#[derive(Default)]
|
2020-03-04 06:24:44 +00:00
|
|
|
pub struct Registry {
|
2021-07-17 02:13:42 +00:00
|
|
|
pub types: BTreeMap<String, MetaType>,
|
2020-05-15 02:08:37 +00:00
|
|
|
pub directives: HashMap<String, MetaDirective>,
|
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>,
|
2022-04-13 03:00:24 +00:00
|
|
|
pub introspection_mode: IntrospectionMode,
|
2021-02-23 04:49:41 +00:00
|
|
|
pub enable_federation: bool,
|
2022-09-04 16:04:12 +00:00
|
|
|
pub enable_apollo_link: bool,
|
2021-03-20 09:22:18 +00:00
|
|
|
pub federation_subscription: bool,
|
2022-05-24 12:08:35 +00:00
|
|
|
pub ignore_name_conflicts: HashSet<String>,
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registry {
|
2022-03-30 12:54:49 +00:00
|
|
|
pub fn create_input_type<T, F>(&mut self, type_id: MetaTypeId, mut f: F) -> String
|
|
|
|
where
|
|
|
|
T: InputType + ?Sized,
|
|
|
|
F: FnMut(&mut Registry) -> MetaType,
|
|
|
|
{
|
|
|
|
self.create_type(
|
|
|
|
&mut f,
|
|
|
|
&*T::type_name(),
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
type_id,
|
|
|
|
);
|
2021-11-09 10:52:10 +00:00
|
|
|
T::qualified_type_name()
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:54:49 +00:00
|
|
|
pub fn create_output_type<T, F>(&mut self, type_id: MetaTypeId, mut f: F) -> String
|
|
|
|
where
|
|
|
|
T: OutputType + ?Sized,
|
|
|
|
F: FnMut(&mut Registry) -> MetaType,
|
|
|
|
{
|
|
|
|
self.create_type(
|
|
|
|
&mut f,
|
|
|
|
&*T::type_name(),
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
type_id,
|
|
|
|
);
|
2021-11-09 10:52:10 +00:00
|
|
|
T::qualified_type_name()
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:54:49 +00:00
|
|
|
pub fn create_subscription_type<T, F>(&mut self, mut f: F) -> String
|
|
|
|
where
|
2021-11-09 10:52:10 +00:00
|
|
|
T: SubscriptionType + ?Sized,
|
|
|
|
F: FnMut(&mut Registry) -> MetaType,
|
2022-03-30 12:54:49 +00:00
|
|
|
{
|
|
|
|
self.create_type(
|
|
|
|
&mut f,
|
|
|
|
&*T::type_name(),
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
MetaTypeId::Object,
|
|
|
|
);
|
2021-11-09 10:52:10 +00:00
|
|
|
T::qualified_type_name()
|
|
|
|
}
|
2021-11-08 01:33:20 +00:00
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
fn create_type<F: FnMut(&mut Registry) -> MetaType>(
|
|
|
|
&mut self,
|
|
|
|
f: &mut F,
|
|
|
|
name: &str,
|
|
|
|
rust_typename: &str,
|
2022-03-30 12:54:49 +00:00
|
|
|
type_id: MetaTypeId,
|
2021-11-09 10:52:10 +00:00
|
|
|
) {
|
|
|
|
match self.types.get(name) {
|
2021-11-08 01:33:20 +00:00
|
|
|
Some(ty) => {
|
|
|
|
if let Some(prev_typename) = ty.rust_typename() {
|
2022-03-30 12:54:49 +00:00
|
|
|
if prev_typename == "__fake_type__" {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-24 12:08:35 +00:00
|
|
|
if rust_typename != prev_typename && !self.ignore_name_conflicts.contains(name)
|
|
|
|
{
|
2021-11-08 01:33:20 +00:00
|
|
|
panic!(
|
|
|
|
"`{}` and `{}` have the same GraphQL name `{}`",
|
|
|
|
prev_typename, rust_typename, name,
|
|
|
|
);
|
|
|
|
}
|
2022-03-30 12:54:49 +00:00
|
|
|
|
|
|
|
if ty.type_id() != type_id {
|
|
|
|
panic!(
|
|
|
|
"Register `{}` as `{}`, but it is already registered as `{}`",
|
|
|
|
name,
|
|
|
|
type_id,
|
|
|
|
ty.type_id()
|
|
|
|
);
|
|
|
|
}
|
2021-11-08 01:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2022-04-19 04:25:11 +00:00
|
|
|
// Inserting a fake type before calling the function allows recursive types to
|
|
|
|
// exist.
|
2021-11-08 01:33:20 +00:00
|
|
|
self.types.insert(
|
2021-11-09 10:52:10 +00:00
|
|
|
name.to_string(),
|
2021-11-08 01:33:20 +00:00
|
|
|
MetaType::Object {
|
|
|
|
name: "".to_string(),
|
|
|
|
description: None,
|
|
|
|
fields: Default::default(),
|
|
|
|
cache_control: Default::default(),
|
|
|
|
extends: false,
|
2022-08-17 02:36:00 +00:00
|
|
|
shareable: false,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2021-11-08 01:33:20 +00:00
|
|
|
keys: None,
|
|
|
|
visible: None,
|
|
|
|
is_subscription: false,
|
|
|
|
rust_typename: "__fake_type__",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let ty = f(self);
|
2022-08-12 06:05:46 +00:00
|
|
|
*self.types.get_mut(name).unwrap() = ty;
|
2021-11-08 01:33:20 +00:00
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
2021-11-09 10:52:10 +00:00
|
|
|
}
|
2021-11-08 01:33:20 +00:00
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
pub fn create_fake_output_type<T: OutputType>(&mut self) -> MetaType {
|
|
|
|
T::create_type_info(self);
|
|
|
|
self.types
|
|
|
|
.get(&*T::type_name())
|
|
|
|
.cloned()
|
|
|
|
.expect("You definitely encountered a bug!")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_fake_input_type<T: InputType>(&mut self) -> MetaType {
|
|
|
|
T::create_type_info(self);
|
|
|
|
self.types
|
|
|
|
.get(&*T::type_name())
|
|
|
|
.cloned()
|
|
|
|
.expect("You definitely encountered a bug!")
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
pub fn create_fake_subscription_type<T: SubscriptionType>(&mut self) -> MetaType {
|
2021-04-08 03:27:06 +00:00
|
|
|
T::create_type_info(self);
|
|
|
|
self.types
|
2021-06-21 01:38:18 +00:00
|
|
|
.get(&*T::type_name())
|
|
|
|
.cloned()
|
2021-04-08 03:27:06 +00:00
|
|
|
.expect("You definitely encountered a bug!")
|
2020-10-20 03:49:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn add_directive(&mut self, directive: MetaDirective) {
|
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-04-09 14:03:09 +00:00
|
|
|
pub fn add_keys(&mut self, ty: &str, keys: &str) {
|
|
|
|
let all_keys = match self.types.get_mut(ty) {
|
2020-05-15 02:08:37 +00:00
|
|
|
Some(MetaType::Object { keys: all_keys, .. }) => all_keys,
|
|
|
|
Some(MetaType::Interface { keys: all_keys, .. }) => all_keys,
|
2020-04-09 14:03:09 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
if let Some(all_keys) = all_keys {
|
|
|
|
all_keys.push(keys.to_string());
|
|
|
|
} else {
|
|
|
|
*all_keys = Some(vec![keys.to_string()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn concrete_type_by_name(&self, type_name: &str) -> Option<&MetaType> {
|
|
|
|
self.types.get(MetaTypeName::concrete_typename(type_name))
|
2020-03-08 12:35:36 +00:00
|
|
|
}
|
2020-03-14 03:46:20 +00:00
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
pub fn concrete_type_by_parsed_type(&self, query_type: &ParsedType) -> Option<&MetaType> {
|
2020-09-06 05:38:31 +00:00
|
|
|
match &query_type.base {
|
|
|
|
ParsedBaseType::Named(name) => self.types.get(name.as_str()),
|
|
|
|
ParsedBaseType::List(ty) => self.concrete_type_by_parsed_type(ty),
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 14:03:09 +00:00
|
|
|
|
2020-06-02 00:49:27 +00:00
|
|
|
pub(crate) fn has_entities(&self) -> bool {
|
2020-04-09 14:03:09 +00:00
|
|
|
self.types.values().any(|ty| match ty {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Object {
|
2020-04-09 14:03:09 +00:00
|
|
|
keys: Some(keys), ..
|
2021-06-21 01:38:18 +00:00
|
|
|
}
|
|
|
|
| MetaType::Interface {
|
2020-04-09 14:03:09 +00:00
|
|
|
keys: Some(keys), ..
|
|
|
|
} => !keys.is_empty(),
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:01:18 +00:00
|
|
|
/// Each type annotated with @key should be added to the _Entity union.
|
|
|
|
/// If no types are annotated with the key directive, then the _Entity union
|
|
|
|
/// and Query._entities field should be removed from the schema.
|
|
|
|
///
|
|
|
|
/// [Reference](https://www.apollographql.com/docs/federation/federation-spec/#resolve-requests-for-entities).
|
|
|
|
fn create_entity_type_and_root_field(&mut self) {
|
2021-11-13 01:08:55 +00:00
|
|
|
let possible_types: IndexSet<String> = self
|
2020-04-09 14:03:09 +00:00
|
|
|
.types
|
|
|
|
.values()
|
|
|
|
.filter_map(|ty| match ty {
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Object {
|
2020-04-09 14:03:09 +00:00
|
|
|
name,
|
|
|
|
keys: Some(keys),
|
|
|
|
..
|
|
|
|
} if !keys.is_empty() => Some(name.clone()),
|
2020-05-15 02:08:37 +00:00
|
|
|
MetaType::Interface {
|
2020-04-09 14:03:09 +00:00
|
|
|
name,
|
|
|
|
keys: Some(keys),
|
|
|
|
..
|
|
|
|
} if !keys.is_empty() => Some(name.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2022-06-10 10:55:12 +00:00
|
|
|
if let MetaType::Object { fields, .. } = self.types.get_mut(&self.query_type).unwrap() {
|
|
|
|
fields.insert(
|
|
|
|
"_service".to_string(),
|
|
|
|
MetaField {
|
|
|
|
name: "_service".to_string(),
|
|
|
|
description: None,
|
|
|
|
args: Default::default(),
|
|
|
|
ty: "_Service!".to_string(),
|
|
|
|
deprecation: Default::default(),
|
|
|
|
cache_control: Default::default(),
|
|
|
|
external: false,
|
|
|
|
requires: None,
|
|
|
|
provides: None,
|
2022-08-17 01:29:43 +00:00
|
|
|
shareable: false,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2022-08-19 21:47:56 +00:00
|
|
|
override_from: None,
|
2022-06-10 10:55:12 +00:00
|
|
|
visible: None,
|
|
|
|
compute_complexity: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:01:18 +00:00
|
|
|
if !possible_types.is_empty() {
|
|
|
|
self.types.insert(
|
|
|
|
"_Entity".to_string(),
|
|
|
|
MetaType::Union {
|
|
|
|
name: "_Entity".to_string(),
|
|
|
|
description: None,
|
|
|
|
possible_types,
|
|
|
|
visible: None,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2021-12-22 17:01:18 +00:00
|
|
|
rust_typename: "async_graphql::federation::Entity",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-06-10 10:55:12 +00:00
|
|
|
if let MetaType::Object { fields, .. } = self.types.get_mut(&self.query_type).unwrap() {
|
2021-12-22 17:01:18 +00:00
|
|
|
fields.insert(
|
|
|
|
"_entities".to_string(),
|
|
|
|
MetaField {
|
|
|
|
name: "_entities".to_string(),
|
|
|
|
description: None,
|
|
|
|
args: {
|
|
|
|
let mut args = IndexMap::new();
|
|
|
|
args.insert(
|
2022-02-28 07:26:33 +00:00
|
|
|
"representations".to_string(),
|
2021-12-22 17:01:18 +00:00
|
|
|
MetaInputValue {
|
|
|
|
name: "representations",
|
|
|
|
description: None,
|
|
|
|
ty: "[_Any!]!".to_string(),
|
|
|
|
default_value: None,
|
|
|
|
visible: None,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2021-12-22 17:01:18 +00:00
|
|
|
is_secret: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
args
|
|
|
|
},
|
|
|
|
ty: "[_Entity]!".to_string(),
|
|
|
|
deprecation: Default::default(),
|
|
|
|
cache_control: Default::default(),
|
|
|
|
external: false,
|
|
|
|
requires: None,
|
|
|
|
provides: None,
|
2022-08-17 01:29:43 +00:00
|
|
|
shareable: false,
|
2021-12-22 17:01:18 +00:00
|
|
|
visible: None,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2022-08-19 21:47:56 +00:00
|
|
|
override_from: None,
|
2021-12-22 17:01:18 +00:00
|
|
|
compute_complexity: None,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 14:03:09 +00:00
|
|
|
}
|
2020-09-23 00:04:00 +00:00
|
|
|
|
|
|
|
pub(crate) fn create_federation_types(&mut self) {
|
2021-11-09 10:52:10 +00:00
|
|
|
<Any as InputType>::create_type_info(self);
|
2020-09-23 00:04:00 +00:00
|
|
|
|
|
|
|
self.types.insert(
|
|
|
|
"_Service".to_string(),
|
|
|
|
MetaType::Object {
|
|
|
|
name: "_Service".to_string(),
|
|
|
|
description: None,
|
|
|
|
fields: {
|
|
|
|
let mut fields = IndexMap::new();
|
|
|
|
fields.insert(
|
|
|
|
"sdl".to_string(),
|
|
|
|
MetaField {
|
|
|
|
name: "sdl".to_string(),
|
|
|
|
description: None,
|
|
|
|
args: Default::default(),
|
|
|
|
ty: "String".to_string(),
|
2021-02-27 03:59:58 +00:00
|
|
|
deprecation: Default::default(),
|
2020-09-23 00:04:00 +00:00
|
|
|
cache_control: Default::default(),
|
|
|
|
external: false,
|
|
|
|
requires: None,
|
|
|
|
provides: None,
|
2022-08-17 01:29:43 +00:00
|
|
|
shareable: false,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: None,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2022-08-19 21:47:56 +00:00
|
|
|
override_from: None,
|
2020-12-18 06:59:37 +00:00
|
|
|
compute_complexity: None,
|
2020-09-23 00:04:00 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
fields
|
|
|
|
},
|
|
|
|
cache_control: Default::default(),
|
|
|
|
extends: false,
|
2022-08-17 02:36:00 +00:00
|
|
|
shareable: false,
|
2020-09-23 00:04:00 +00:00
|
|
|
keys: None,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: None,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: false,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: Default::default(),
|
2021-10-28 08:56:08 +00:00
|
|
|
is_subscription: false,
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: "async_graphql::federation::Service",
|
2020-09-23 00:04:00 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-12-22 17:01:18 +00:00
|
|
|
self.create_entity_type_and_root_field();
|
2020-09-23 00:04:00 +00:00
|
|
|
}
|
2020-10-15 11:36:54 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2020-10-27 02:06:10 +00:00
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
pub fn set_description(&mut self, name: &str, desc: &'static str) {
|
|
|
|
match self.types.get_mut(name) {
|
2020-10-27 02:06:10 +00:00
|
|
|
Some(MetaType::Scalar { description, .. }) => *description = Some(desc),
|
|
|
|
Some(MetaType::Object { description, .. }) => *description = Some(desc),
|
|
|
|
Some(MetaType::Interface { description, .. }) => *description = Some(desc),
|
|
|
|
Some(MetaType::Union { description, .. }) => *description = Some(desc),
|
|
|
|
Some(MetaType::Enum { description, .. }) => *description = Some(desc),
|
|
|
|
Some(MetaType::InputObject { description, .. }) => *description = Some(desc),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 01:38:18 +00:00
|
|
|
|
|
|
|
pub fn remove_unused_types(&mut self) {
|
2021-07-12 03:14:26 +00:00
|
|
|
let mut used_types = BTreeSet::new();
|
|
|
|
let mut unused_types = BTreeSet::new();
|
2021-06-21 01:38:18 +00:00
|
|
|
|
|
|
|
fn traverse_field<'a>(
|
2021-07-17 02:13:42 +00:00
|
|
|
types: &'a BTreeMap<String, MetaType>,
|
2021-07-12 03:14:26 +00:00
|
|
|
used_types: &mut BTreeSet<&'a str>,
|
2021-06-21 01:38:18 +00:00
|
|
|
field: &'a MetaField,
|
|
|
|
) {
|
|
|
|
traverse_type(
|
|
|
|
types,
|
|
|
|
used_types,
|
|
|
|
MetaTypeName::concrete_typename(&field.ty),
|
|
|
|
);
|
|
|
|
for arg in field.args.values() {
|
|
|
|
traverse_input_value(types, used_types, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_input_value<'a>(
|
2021-07-17 02:13:42 +00:00
|
|
|
types: &'a BTreeMap<String, MetaType>,
|
2021-07-12 03:14:26 +00:00
|
|
|
used_types: &mut BTreeSet<&'a str>,
|
2021-06-21 01:38:18 +00:00
|
|
|
input_value: &'a MetaInputValue,
|
|
|
|
) {
|
|
|
|
traverse_type(
|
|
|
|
types,
|
|
|
|
used_types,
|
|
|
|
MetaTypeName::concrete_typename(&input_value.ty),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_type<'a>(
|
2021-07-17 02:13:42 +00:00
|
|
|
types: &'a BTreeMap<String, MetaType>,
|
2021-07-12 03:14:26 +00:00
|
|
|
used_types: &mut BTreeSet<&'a str>,
|
2021-06-21 01:38:18 +00:00
|
|
|
type_name: &'a str,
|
|
|
|
) {
|
|
|
|
if used_types.contains(type_name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ty) = types.get(type_name) {
|
|
|
|
used_types.insert(type_name);
|
|
|
|
match ty {
|
|
|
|
MetaType::Object { fields, .. } => {
|
|
|
|
for field in fields.values() {
|
|
|
|
traverse_field(types, used_types, field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaType::Interface {
|
|
|
|
fields,
|
|
|
|
possible_types,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
for field in fields.values() {
|
|
|
|
traverse_field(types, used_types, field);
|
|
|
|
}
|
|
|
|
for type_name in possible_types.iter() {
|
|
|
|
traverse_type(types, used_types, type_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaType::Union { possible_types, .. } => {
|
|
|
|
for type_name in possible_types.iter() {
|
|
|
|
traverse_type(types, used_types, type_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaType::InputObject { input_fields, .. } => {
|
|
|
|
for field in input_fields.values() {
|
|
|
|
traverse_input_value(types, used_types, field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
for directive in self.directives.values() {
|
|
|
|
for arg in directive.args.values() {
|
|
|
|
traverse_input_value(&self.types, &mut used_types, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 01:38:18 +00:00
|
|
|
for type_name in Some(&self.query_type)
|
|
|
|
.into_iter()
|
|
|
|
.chain(self.mutation_type.iter())
|
|
|
|
.chain(self.subscription_type.iter())
|
|
|
|
{
|
|
|
|
traverse_type(&self.types, &mut used_types, type_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ty in self.types.values().filter(|ty| match ty {
|
|
|
|
MetaType::Object {
|
|
|
|
keys: Some(keys), ..
|
|
|
|
}
|
|
|
|
| MetaType::Interface {
|
|
|
|
keys: Some(keys), ..
|
|
|
|
} => !keys.is_empty(),
|
|
|
|
_ => false,
|
|
|
|
}) {
|
|
|
|
traverse_type(&self.types, &mut used_types, ty.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
for ty in self.types.values() {
|
|
|
|
let name = ty.name();
|
|
|
|
if !is_system_type(name) && !used_types.contains(name) {
|
|
|
|
unused_types.insert(name.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for type_name in unused_types {
|
|
|
|
self.types.remove(&type_name);
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 07:22:40 +00:00
|
|
|
|
|
|
|
pub fn find_visible_types(&self, ctx: &Context<'_>) -> HashSet<&str> {
|
|
|
|
let mut visible_types = HashSet::new();
|
|
|
|
|
|
|
|
fn traverse_field<'a>(
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
types: &'a BTreeMap<String, MetaType>,
|
|
|
|
visible_types: &mut HashSet<&'a str>,
|
|
|
|
field: &'a MetaField,
|
|
|
|
) {
|
|
|
|
if !is_visible(ctx, &field.visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
traverse_type(
|
|
|
|
ctx,
|
|
|
|
types,
|
|
|
|
visible_types,
|
|
|
|
MetaTypeName::concrete_typename(&field.ty),
|
|
|
|
);
|
|
|
|
for arg in field.args.values() {
|
|
|
|
traverse_input_value(ctx, types, visible_types, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_input_value<'a>(
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
types: &'a BTreeMap<String, MetaType>,
|
|
|
|
visible_types: &mut HashSet<&'a str>,
|
|
|
|
input_value: &'a MetaInputValue,
|
|
|
|
) {
|
|
|
|
if !is_visible(ctx, &input_value.visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
traverse_type(
|
|
|
|
ctx,
|
|
|
|
types,
|
|
|
|
visible_types,
|
|
|
|
MetaTypeName::concrete_typename(&input_value.ty),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_type<'a>(
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
types: &'a BTreeMap<String, MetaType>,
|
|
|
|
visible_types: &mut HashSet<&'a str>,
|
|
|
|
type_name: &'a str,
|
|
|
|
) {
|
|
|
|
if visible_types.contains(type_name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ty) = types.get(type_name) {
|
|
|
|
if !ty.is_visible(ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visible_types.insert(type_name);
|
|
|
|
match ty {
|
|
|
|
MetaType::Object { fields, .. } => {
|
|
|
|
for field in fields.values() {
|
|
|
|
traverse_field(ctx, types, visible_types, field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaType::Interface {
|
|
|
|
fields,
|
|
|
|
possible_types,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
for field in fields.values() {
|
|
|
|
traverse_field(ctx, types, visible_types, field);
|
|
|
|
}
|
|
|
|
for type_name in possible_types.iter() {
|
|
|
|
traverse_type(ctx, types, visible_types, type_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaType::Union { possible_types, .. } => {
|
|
|
|
for type_name in possible_types.iter() {
|
|
|
|
traverse_type(ctx, types, visible_types, type_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaType::InputObject { input_fields, .. } => {
|
|
|
|
for field in input_fields.values() {
|
|
|
|
traverse_input_value(ctx, types, visible_types, field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
for directive in self.directives.values() {
|
|
|
|
if is_visible(ctx, &directive.visible) {
|
|
|
|
for arg in directive.args.values() {
|
|
|
|
traverse_input_value(ctx, &self.types, &mut visible_types, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 07:22:40 +00:00
|
|
|
for type_name in Some(&self.query_type)
|
|
|
|
.into_iter()
|
|
|
|
.chain(self.mutation_type.iter())
|
|
|
|
.chain(self.subscription_type.iter())
|
|
|
|
{
|
|
|
|
traverse_type(ctx, &self.types, &mut visible_types, type_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ty in self.types.values().filter(|ty| match ty {
|
|
|
|
MetaType::Object {
|
|
|
|
keys: Some(keys), ..
|
|
|
|
}
|
|
|
|
| MetaType::Interface {
|
|
|
|
keys: Some(keys), ..
|
|
|
|
} => !keys.is_empty(),
|
|
|
|
_ => false,
|
|
|
|
}) {
|
|
|
|
traverse_type(ctx, &self.types, &mut visible_types, ty.name());
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:39:36 +00:00
|
|
|
for ty in self.types.values() {
|
|
|
|
if let MetaType::Interface { possible_types, .. } = ty {
|
|
|
|
if ty.is_visible(ctx) && !visible_types.contains(ty.name()) {
|
|
|
|
for type_name in possible_types.iter() {
|
|
|
|
if visible_types.contains(type_name.as_str()) {
|
|
|
|
traverse_type(ctx, &self.types, &mut visible_types, ty.name());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 07:22:40 +00:00
|
|
|
self.types
|
|
|
|
.values()
|
|
|
|
.filter_map(|ty| {
|
|
|
|
let name = ty.name();
|
|
|
|
if is_system_type(name) || visible_types.contains(name) {
|
|
|
|
Some(name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn is_visible(ctx: &Context<'_>, visible: &Option<MetaVisibleFn>) -> bool {
|
|
|
|
match visible {
|
|
|
|
Some(f) => f(ctx),
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_system_type(name: &str) -> bool {
|
|
|
|
if name.starts_with("__") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
name == "Boolean" || name == "Int" || name == "Float" || name == "String" || name == "ID"
|
2020-05-18 03:24:27 +00:00
|
|
|
}
|