1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use apollo_encoder::Type_;
use arbitrary::Result;
use once_cell::sync::Lazy;
use crate::{input_value::InputValue, name::Name, DocumentBuilder};
static BUILTIN_SCALAR_NAMES: Lazy<[Ty; 5]> = Lazy::new(|| {
[
Ty::Named(Name::new(String::from("Int"))),
Ty::Named(Name::new(String::from("Float"))),
Ty::Named(Name::new(String::from("String"))),
Ty::Named(Name::new(String::from("Boolean"))),
Ty::Named(Name::new(String::from("ID"))),
]
});
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Ty {
Named(Name),
List(Box<Ty>),
NonNull(Box<Ty>),
}
impl From<Ty> for Type_ {
fn from(val: Ty) -> Self {
match val {
Ty::Named(name) => Type_::NamedType { name: name.into() },
Ty::List(ty) => Type_::List {
ty: Box::new((*ty).into()),
},
Ty::NonNull(ty) => Type_::NonNull {
ty: Box::new((*ty).into()),
},
}
}
}
#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::Type> for Ty {
fn from(ty: apollo_parser::ast::Type) -> Self {
match ty {
apollo_parser::ast::Type::NamedType(named_ty) => named_ty.into(),
apollo_parser::ast::Type::ListType(list_type) => {
Self::List(Box::new(list_type.ty().unwrap().into()))
}
apollo_parser::ast::Type::NonNullType(non_null) => {
if let Some(named_ty) = non_null.named_type() {
Self::NonNull(Box::new(named_ty.into()))
} else if let Some(list_type) = non_null.list_type() {
Self::NonNull(Box::new(Self::List(Box::new(
list_type.ty().unwrap().into(),
))))
} else {
panic!("a non null type must have a type")
}
}
}
}
}
#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::NamedType> for Ty {
fn from(ty: apollo_parser::ast::NamedType) -> Self {
Self::Named(ty.name().unwrap().into())
}
}
impl Ty {
pub(crate) fn name(&self) -> &Name {
match self {
Ty::Named(name) => name,
Ty::List(list) => list.name(),
Ty::NonNull(non_null) => non_null.name(),
}
}
pub fn is_named(&self) -> bool {
matches!(self, Self::Named(..))
}
pub(crate) fn is_builtin(&self) -> bool {
BUILTIN_SCALAR_NAMES.contains(&Ty::Named(self.name().clone()))
}
}
impl<'a> DocumentBuilder<'a> {
pub fn ty(&mut self) -> Result<Ty> {
self.generate_ty(true)
}
pub fn choose_ty(&mut self, existing_types: &[Ty]) -> Result<Ty> {
self.choose_ty_given_nullable(existing_types, true)
}
pub fn choose_named_ty(&mut self, existing_types: &[Ty]) -> Result<Ty> {
let used_type_names: Vec<&Ty> = existing_types
.iter()
.chain(BUILTIN_SCALAR_NAMES.iter())
.collect();
Ok(self.u.choose(&used_type_names)?.to_owned().clone())
}
fn choose_ty_given_nullable(&mut self, existing_types: &[Ty], is_nullable: bool) -> Result<Ty> {
let ty: Ty = match self.u.int_in_range(0..=2usize)? {
0 => {
let used_type_names: Vec<&Ty> = existing_types
.iter()
.chain(BUILTIN_SCALAR_NAMES.iter())
.collect();
self.u.choose(&used_type_names)?.to_owned().clone()
}
1 => Ty::List(Box::new(
self.choose_ty_given_nullable(existing_types, true)?,
)),
2 => {
if is_nullable {
Ty::NonNull(Box::new(
self.choose_ty_given_nullable(existing_types, false)?,
))
} else {
self.choose_ty_given_nullable(existing_types, is_nullable)?
}
}
_ => unreachable!(),
};
Ok(ty)
}
fn generate_ty(&mut self, is_nullable: bool) -> Result<Ty> {
let ty = match self.u.int_in_range(0..=2usize)? {
0 => Ty::Named(self.name()?),
1 => Ty::List(Box::new(self.generate_ty(true)?)),
2 => {
if is_nullable {
Ty::NonNull(Box::new(self.generate_ty(false)?))
} else {
self.generate_ty(is_nullable)?
}
}
_ => unreachable!(),
};
Ok(ty)
}
pub(crate) fn list_existing_types(&self) -> Vec<Ty> {
self.object_type_defs
.iter()
.map(|o| Ty::Named(o.name.clone()))
.chain(
self.enum_type_defs
.iter()
.map(|e| Ty::Named(e.name.clone())),
)
.collect()
}
pub(crate) fn list_existing_object_types(&self) -> Vec<Ty> {
self.object_type_defs
.iter()
.map(|o| Ty::Named(o.name.clone()))
.collect()
}
#[allow(dead_code)]
pub(crate) fn generate_value_for_type(&mut self, _ty: &Ty) -> InputValue {
todo!()
}
}