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
use std::collections::HashMap;
use arbitrary::Result;
use crate::{
description::Description,
directive::{Directive, DirectiveLocation},
input_value::InputValueDef,
name::Name,
DocumentBuilder,
};
#[derive(Debug, Clone)]
pub struct InputObjectTypeDef {
pub(crate) name: Name,
pub(crate) description: Option<Description>,
pub(crate) fields: Vec<InputValueDef>,
pub(crate) directives: HashMap<Name, Directive>,
pub(crate) extend: bool,
}
impl From<InputObjectTypeDef> for apollo_encoder::InputObjectDefinition {
fn from(input_object_def: InputObjectTypeDef) -> Self {
let mut new_input_object_def = Self::new(input_object_def.name.into());
if let Some(description) = input_object_def.description {
new_input_object_def.description(description.into());
}
if input_object_def.extend {
new_input_object_def.extend();
}
input_object_def
.directives
.into_iter()
.for_each(|(_, directive)| new_input_object_def.directive(directive.into()));
input_object_def
.fields
.into_iter()
.for_each(|field| new_input_object_def.field(field.into()));
new_input_object_def
}
}
#[cfg(feature = "parser-impl")]
impl TryFrom<apollo_parser::ast::InputObjectTypeDefinition> for InputObjectTypeDef {
type Error = crate::FromError;
fn try_from(
input_object: apollo_parser::ast::InputObjectTypeDefinition,
) -> Result<Self, Self::Error> {
Ok(Self {
name: input_object
.name()
.expect("object type definition must have a name")
.into(),
description: input_object.description().map(Description::from),
directives: input_object
.directives()
.map(Directive::convert_directives)
.transpose()?
.unwrap_or_default(),
extend: false,
fields: input_object
.input_fields_definition()
.map(|input_fields| {
input_fields
.input_value_definitions()
.map(InputValueDef::try_from)
.collect::<Result<_, _>>()
})
.transpose()?
.unwrap_or_default(),
})
}
}
#[cfg(feature = "parser-impl")]
impl TryFrom<apollo_parser::ast::InputObjectTypeExtension> for InputObjectTypeDef {
type Error = crate::FromError;
fn try_from(
input_object: apollo_parser::ast::InputObjectTypeExtension,
) -> Result<Self, Self::Error> {
Ok(Self {
name: input_object
.name()
.expect("object type definition must have a name")
.into(),
directives: input_object
.directives()
.map(Directive::convert_directives)
.transpose()?
.unwrap_or_default(),
extend: true,
fields: input_object
.input_fields_definition()
.map(|input_fields| {
input_fields
.input_value_definitions()
.map(InputValueDef::try_from)
.collect::<Result<Vec<_>, crate::FromError>>()
})
.transpose()?
.unwrap_or_default(),
description: None,
})
}
}
impl<'a> DocumentBuilder<'a> {
pub fn input_object_type_definition(&mut self) -> Result<InputObjectTypeDef> {
let extend = !self.input_object_type_defs.is_empty() && self.u.arbitrary().unwrap_or(false);
let name = if extend {
let available_input_objects: Vec<&Name> = self
.input_object_type_defs
.iter()
.filter_map(|input_object| {
if input_object.extend {
None
} else {
Some(&input_object.name)
}
})
.collect();
(*self.u.choose(&available_input_objects)?).clone()
} else {
self.type_name()?
};
let description = self
.u
.arbitrary()
.unwrap_or(false)
.then(|| self.description())
.transpose()?;
let fields = self.input_values_def()?;
Ok(InputObjectTypeDef {
description,
directives: self.directives(DirectiveLocation::InputObject)?,
name,
extend,
fields,
})
}
}