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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::io;

use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionQuery {
    pub __schema: IntrospectionSchema,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionScalarType {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "specifiedByURL")]
    pub specified_by_url: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionInputValue {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "defaultValue")]
    pub default_value: Option<String>,
    #[serde(rename = "isDeprecated")]
    pub is_deprecated: Option<bool>,
    #[serde(rename = "deprecationReason")]
    pub deprecation_reason: Option<String>,
    #[serde(rename = "type")]
    pub type_ref: Option<IntrospectionInputTypeRef>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub struct IntrospectionField {
    pub name: String,
    pub description: Option<String>,
    pub args: Vec<IntrospectionInputValue>,
    #[serde(rename = "isDeprecated")]
    pub is_deprecated: Option<bool>,
    #[serde(rename = "deprecationReason")]
    pub deprecation_reason: Option<String>,
    #[serde(rename = "type")]
    pub type_ref: IntrospectionOutputTypeRef,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionObjectType {
    pub name: String,
    pub description: Option<String>,
    pub fields: Vec<IntrospectionField>,
    pub interfaces: Vec<IntrospectionNamedTypeRef>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionInterfaceType {
    pub name: String,
    pub description: Option<String>,
    pub fields: Vec<IntrospectionField>,
    pub interfaces: Option<Vec<IntrospectionNamedTypeRef>>,
    #[serde(rename = "possibleTypes")]
    pub possible_types: Vec<IntrospectionNamedTypeRef>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionUnionType {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "possibleTypes")]
    pub possible_types: Vec<IntrospectionNamedTypeRef>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionEnumValue {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "isDeprecated")]
    pub is_deprecated: Option<bool>,
    #[serde(rename = "deprecationReason")]
    pub deprecation_reason: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionEnumType {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "enumValues")]
    pub enum_values: Vec<IntrospectionEnumValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionInputObjectType {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "inputFields")]
    pub input_fields: Vec<IntrospectionInputValue>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub enum IntrospectionType {
    SCALAR(IntrospectionScalarType),
    OBJECT(IntrospectionObjectType),
    INTERFACE(IntrospectionInterfaceType),
    UNION(IntrospectionUnionType),
    ENUM(IntrospectionEnumType),
    INPUT_OBJECT(IntrospectionInputObjectType),
}

impl IntrospectionType {
    pub fn name(&self) -> &String {
        match &self {
            IntrospectionType::ENUM(e) => &e.name,
            IntrospectionType::OBJECT(o) => &o.name,
            IntrospectionType::INPUT_OBJECT(io) => &io.name,
            IntrospectionType::INTERFACE(i) => &i.name,
            IntrospectionType::SCALAR(s) => &s.name,
            IntrospectionType::UNION(u) => &u.name,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub enum IntrospectionInputType {
    SCALAR(IntrospectionScalarType),
    ENUM(IntrospectionEnumType),
    INPUT_OBJECT(IntrospectionInputObjectType),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub enum IntrospectionOutputType {
    SCALAR(IntrospectionScalarType),
    OBJECT(IntrospectionObjectType),
    INTERFACE(IntrospectionInterfaceType),
    UNION(IntrospectionUnionType),
    ENUM(IntrospectionEnumType),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionNamedTypeRef {
    pub name: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub enum IntrospectionOutputTypeRef {
    SCALAR(IntrospectionNamedTypeRef),
    LIST {
        #[serde(rename = "ofType")]
        of_type: Option<Box<IntrospectionOutputTypeRef>>,
    },
    NON_NULL {
        #[serde(rename = "ofType")]
        of_type: Option<Box<IntrospectionOutputTypeRef>>,
    },
    ENUM(IntrospectionNamedTypeRef),
    INPUT_OBJECT(IntrospectionNamedTypeRef),
    UNION(IntrospectionNamedTypeRef),
    OBJECT(IntrospectionNamedTypeRef),
    INTERFACE(IntrospectionNamedTypeRef),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "kind")]
pub enum IntrospectionInputTypeRef {
    LIST {
        #[serde(rename = "ofType")]
        of_type: Option<Box<IntrospectionOutputTypeRef>>,
    },
    NON_NULL {
        #[serde(rename = "ofType")]
        of_type: Option<Box<IntrospectionOutputTypeRef>>,
    },
    SCALAR(IntrospectionNamedTypeRef),
    ENUM(IntrospectionNamedTypeRef),
    INPUT_OBJECT(IntrospectionNamedTypeRef),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionSchema {
    pub description: Option<String>,
    #[serde(rename = "queryType")]
    pub query_type: IntrospectionNamedTypeRef,
    #[serde(rename = "mutationType")]
    pub mutation_type: Option<IntrospectionNamedTypeRef>,
    #[serde(rename = "subscriptionType")]
    pub subscription_type: Option<IntrospectionNamedTypeRef>,
    pub types: Vec<IntrospectionType>,
    pub directives: Vec<IntrospectionDirective>,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum DirectiveLocation {
    QUERY,
    MUTATION,
    SUBSCRIPTION,
    FIELD,
    FRAGMENT_DEFINITION,
    FRAGMENT_SPREAD,
    INLINE_FRAGMENT,
    VARIABLE_DEFINITION,
    /** Type System Definitions */
    SCHEMA,
    SCALAR,
    OBJECT,
    FIELD_DEFINITION,
    ARGUMENT_DEFINITION,
    INTERFACE,
    UNION,
    ENUM,
    ENUM_VALUE,
    INPUT_OBJECT,
    INPUT_FIELD_DEFINITION,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IntrospectionDirective {
    pub name: String,
    pub description: Option<String>,
    #[serde(rename = "isRepeatable")]
    pub is_repeatable: Option<bool>,
    pub locations: Vec<DirectiveLocation>,
    pub args: Vec<IntrospectionInputValue>,
}

pub fn parse_introspection_from_string(input: &str) -> Result<IntrospectionQuery> {
    serde_json::from_str(input)
}

pub fn parse_introspection<R>(input: R) -> Result<IntrospectionQuery>
where
    R: io::Read,
{
    serde_json::from_reader::<R, IntrospectionQuery>(input)
}

#[test]
fn test_product_introspection() {
    use std::fs::File;
    let json_file = File::open("./src/introspection/test_files/product_introspection.json")
        .expect("failed to open json file");
    parse_introspection(json_file).expect("failed to parse introspection json");
}

#[test]
fn test_github_introspection() {
    use std::fs::File;
    let json_file = File::open("./src/introspection/test_files/github_introspection.json")
        .expect("failed to open json file");
    parse_introspection(json_file).expect("failed to parse introspection json");
}

#[test]
fn test_shopify_introspection() {
    use std::fs::File;
    let json_file = File::open("./src/introspection/test_files/shopify_introspection.json")
        .expect("failed to open json file");
    parse_introspection(json_file).expect("failed to parse introspection json");
}