Skip to content

Commit 7cd231b

Browse files
committed
added introspection lib
1 parent 0c1492a commit 7cd231b

File tree

8 files changed

+225879
-0
lines changed

8 files changed

+225879
-0
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ authors = ["Dotan Simha <[email protected]>"]
1212

1313
[dependencies]
1414
graphql-parser = "^0.4.0"
15+
serde = { version = "1.0", features = ["derive"] }
16+
serde_json = "1.0"
1517

1618
[dev-dependencies]
1719
graphql-parser = "0.4.0"

src/introspection/introspection.rs

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
use std::io;
2+
3+
use serde::{Deserialize, Serialize};
4+
use serde_json::Result;
5+
6+
#[derive(Serialize, Deserialize, Debug)]
7+
pub struct IntrospectionQuery {
8+
__schema: IntrospectionSchema,
9+
}
10+
11+
#[derive(Serialize, Deserialize, Debug)]
12+
pub struct IntrospectionScalarType {
13+
name: String,
14+
description: Option<String>,
15+
#[serde(rename = "specifiedByURL")]
16+
specified_by_url: Option<String>,
17+
}
18+
19+
#[derive(Serialize, Deserialize, Debug)]
20+
pub struct IntrospectionInputValue {
21+
name: String,
22+
description: Option<String>,
23+
#[serde(rename = "defaultValue")]
24+
default_value: Option<String>,
25+
#[serde(rename = "isDeprecated")]
26+
is_deprecated: Option<bool>,
27+
#[serde(rename = "deprecationReason")]
28+
deprecation_reason: Option<String>,
29+
#[serde(rename = "type")]
30+
type_ref: Option<IntrospectionInputTypeRef>,
31+
}
32+
33+
#[derive(Serialize, Deserialize, Debug)]
34+
#[serde(tag = "kind")]
35+
pub struct IntrospectionField {
36+
name: String,
37+
description: Option<String>,
38+
args: Vec<IntrospectionInputValue>,
39+
#[serde(rename = "isDeprecated")]
40+
is_deprecated: Option<bool>,
41+
#[serde(rename = "deprecationReason")]
42+
deprecation_reason: Option<String>,
43+
#[serde(rename = "type")]
44+
type_ref: IntrospectionOutputTypeRef,
45+
}
46+
47+
#[derive(Serialize, Deserialize, Debug)]
48+
pub struct IntrospectionObjectType {
49+
name: String,
50+
description: Option<String>,
51+
fields: Vec<IntrospectionField>,
52+
interfaces: Vec<IntrospectionNamedTypeRef>,
53+
}
54+
55+
#[derive(Serialize, Deserialize, Debug)]
56+
pub struct IntrospectionInterfaceType {
57+
name: String,
58+
description: Option<String>,
59+
fields: Vec<IntrospectionField>,
60+
interfaces: Option<Vec<IntrospectionNamedTypeRef>>,
61+
#[serde(rename = "possibleTypes")]
62+
possible_types: Vec<IntrospectionNamedTypeRef>,
63+
}
64+
65+
#[derive(Serialize, Deserialize, Debug)]
66+
pub struct IntrospectionUnionType {
67+
name: String,
68+
description: Option<String>,
69+
#[serde(rename = "possibleTypes")]
70+
possible_types: Vec<IntrospectionNamedTypeRef>,
71+
}
72+
73+
#[derive(Serialize, Deserialize, Debug)]
74+
pub struct IntrospectionEnumValue {
75+
name: String,
76+
description: Option<String>,
77+
#[serde(rename = "isDeprecated")]
78+
is_deprecated: Option<bool>,
79+
#[serde(rename = "deprecationReason")]
80+
deprecation_reason: Option<String>,
81+
}
82+
83+
#[derive(Serialize, Deserialize, Debug)]
84+
pub struct IntrospectionEnumType {
85+
name: String,
86+
description: Option<String>,
87+
#[serde(rename = "enumValues")]
88+
enum_values: Vec<IntrospectionEnumValue>,
89+
}
90+
91+
#[derive(Serialize, Deserialize, Debug)]
92+
pub struct IntrospectionInputObjectType {
93+
name: String,
94+
description: Option<String>,
95+
#[serde(rename = "inputFields")]
96+
input_fields: Vec<IntrospectionInputValue>,
97+
}
98+
99+
#[derive(Serialize, Deserialize, Debug)]
100+
#[serde(tag = "kind")]
101+
pub enum IntrospectionType {
102+
SCALAR(IntrospectionScalarType),
103+
OBJECT(IntrospectionObjectType),
104+
INTERFACE(IntrospectionInterfaceType),
105+
UNION(IntrospectionUnionType),
106+
ENUM(IntrospectionEnumType),
107+
INPUT_OBJECT(IntrospectionInputObjectType),
108+
}
109+
110+
#[derive(Serialize, Deserialize, Debug)]
111+
#[serde(tag = "kind")]
112+
pub enum IntrospectionInputType {
113+
SCALAR(IntrospectionScalarType),
114+
ENUM(IntrospectionEnumType),
115+
INPUT_OBJECT(IntrospectionInputObjectType),
116+
}
117+
118+
#[derive(Serialize, Deserialize, Debug)]
119+
#[serde(tag = "kind")]
120+
pub enum IntrospectionOutputType {
121+
SCALAR(IntrospectionScalarType),
122+
OBJECT(IntrospectionObjectType),
123+
INTERFACE(IntrospectionInterfaceType),
124+
UNION(IntrospectionUnionType),
125+
ENUM(IntrospectionEnumType),
126+
}
127+
128+
#[derive(Serialize, Deserialize, Debug)]
129+
pub struct IntrospectionNamedTypeRef {
130+
name: String,
131+
}
132+
133+
#[derive(Serialize, Deserialize, Debug)]
134+
#[serde(tag = "kind")]
135+
pub enum IntrospectionOutputTypeRef {
136+
SCALAR(IntrospectionNamedTypeRef),
137+
LIST {
138+
#[serde(rename = "ofType")]
139+
of_type: Option<Box<IntrospectionOutputTypeRef>>,
140+
},
141+
NON_NULL {
142+
#[serde(rename = "ofType")]
143+
of_type: Option<Box<IntrospectionOutputTypeRef>>,
144+
},
145+
ENUM(IntrospectionNamedTypeRef),
146+
INPUT_OBJECT(IntrospectionNamedTypeRef),
147+
UNION(IntrospectionNamedTypeRef),
148+
OBJECT(IntrospectionNamedTypeRef),
149+
INTERFACE(IntrospectionNamedTypeRef),
150+
}
151+
152+
#[derive(Serialize, Deserialize, Debug)]
153+
#[serde(tag = "kind")]
154+
pub enum IntrospectionInputTypeRef {
155+
LIST {
156+
#[serde(rename = "ofType")]
157+
of_type: Option<Box<IntrospectionOutputTypeRef>>,
158+
},
159+
NON_NULL {
160+
#[serde(rename = "ofType")]
161+
of_type: Option<Box<IntrospectionOutputTypeRef>>,
162+
},
163+
SCALAR(IntrospectionNamedTypeRef),
164+
ENUM(IntrospectionNamedTypeRef),
165+
INPUT_OBJECT(IntrospectionNamedTypeRef),
166+
}
167+
168+
#[derive(Serialize, Deserialize, Debug)]
169+
pub struct IntrospectionSchema {
170+
description: Option<String>,
171+
#[serde(rename = "queryType")]
172+
query_type: IntrospectionNamedTypeRef,
173+
#[serde(rename = "mutationType")]
174+
mutation_type: Option<IntrospectionNamedTypeRef>,
175+
#[serde(rename = "subscriptionType")]
176+
subscription_type: Option<IntrospectionNamedTypeRef>,
177+
types: Vec<IntrospectionType>,
178+
directives: Vec<IntrospectionDirective>,
179+
}
180+
181+
#[derive(Serialize, Deserialize, Debug)]
182+
pub enum DirectiveLocation {
183+
QUERY,
184+
MUTATION,
185+
SUBSCRIPTION,
186+
FIELD,
187+
FRAGMENT_DEFINITION,
188+
FRAGMENT_SPREAD,
189+
INLINE_FRAGMENT,
190+
VARIABLE_DEFINITION,
191+
/** Type System Definitions */
192+
SCHEMA,
193+
SCALAR,
194+
OBJECT,
195+
FIELD_DEFINITION,
196+
ARGUMENT_DEFINITION,
197+
INTERFACE,
198+
UNION,
199+
ENUM,
200+
ENUM_VALUE,
201+
INPUT_OBJECT,
202+
INPUT_FIELD_DEFINITION,
203+
}
204+
205+
#[derive(Serialize, Deserialize, Debug)]
206+
pub struct IntrospectionDirective {
207+
name: String,
208+
description: Option<String>,
209+
#[serde(rename = "isRepeatable")]
210+
is_repeatable: Option<bool>,
211+
locations: Vec<DirectiveLocation>,
212+
args: Vec<IntrospectionInputValue>,
213+
}
214+
215+
pub fn parse_introspection<R>(input: R) -> Result<IntrospectionQuery>
216+
where
217+
R: io::Read,
218+
{
219+
serde_json::from_reader::<R, IntrospectionQuery>(input)
220+
}
221+
222+
#[test]
223+
fn test_product_introspection() {
224+
use std::fs::File;
225+
let json_file = File::open("./src/introspection/test_files/product_introspection.json")
226+
.expect("failed to open json file");
227+
parse_introspection(json_file).expect("failed to parse introspection json");
228+
}
229+
230+
#[test]
231+
fn test_github_introspection() {
232+
use std::fs::File;
233+
let json_file = File::open("./src/introspection/test_files/github_introspection.json")
234+
.expect("failed to open json file");
235+
parse_introspection(json_file).expect("failed to parse introspection json");
236+
}
237+
238+
#[test]
239+
fn test_shopify_introspection() {
240+
use std::fs::File;
241+
let json_file = File::open("./src/introspection/test_files/shopify_introspection.json")
242+
.expect("failed to open json file");
243+
parse_introspection(json_file).expect("failed to parse introspection json");
244+
}

src/introspection/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![allow(non_camel_case_types)]
2+
mod introspection;
3+
4+
pub use self::introspection::*;

0 commit comments

Comments
 (0)