/
query.rs
216 lines (179 loc) · 4.96 KB
/
query.rs
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
extern crate graphers_core as core;
extern crate serde;
mod schema;
pub use self::schema::reflect;
pub use self::schema::Country;
pub use self::schema::Location;
pub use self::schema::CoordinateSystem;
use std::borrow::Cow;
#[derive(Debug, Clone)]
pub struct NationalId(u32, u32);
impl core::Coerce for NationalId {
fn coerce(value: &core::Value) -> core::value::CoercionResult<Self> {
match value {
&core::Value::String(ref s) => {
Ok(NationalId(s[0..6].parse().unwrap(), s[7..11].parse().unwrap()))
}
_ => panic!("cannot convert {:?} into national id", value),
}
}
}
impl serde::Serialize for NationalId {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer {
format!("{}-{}", self.0, self.1).serialize(serializer)
}
}
#[derive(Debug)]
pub struct QueryRoot;
#[derive(Debug)]
pub struct Person {
id: String,
first_name: String,
last_name: String,
national_id: Option<NationalId>,
country: Country,
age: i32,
}
#[derive(Debug)]
pub struct Post {
id: String,
title: String,
tags: Vec<String>,
}
pub struct Schema;
impl Schema {
pub fn query<'a>(&self, context: &'a ::graphers::Context) -> schema::QueryResult<'a, QueryRoot> {
schema::query(self, context)
}
}
impl schema::Schema for Schema {
type QueryRoot = QueryRoot;
type Person = Person;
type Post = Post;
type NationalId = NationalId;
fn query_root(&self) -> QueryRoot {
QueryRoot
}
}
impl schema::HasSchema for Person {
type Schema = Schema;
}
impl schema::ResolvePerson for Person {
fn id(&self) -> Cow<str> {
self.id.as_str().into()
}
fn first_name(&self) -> Cow<str> {
self.first_name.as_str().into()
}
fn last_name(&self) -> Cow<str> {
self.last_name.as_str().into()
}
fn national_id(&self) -> Option<NationalId> {
self.national_id.clone()
}
fn country(&self) -> Country {
self.country
}
fn friend(&self) -> Option<Person> {
None
}
fn age(&self) -> i32 {
self.age
}
fn tags(&self) -> Vec<Cow<str>> {
vec!["foo".into(), "bar".into()]
}
}
impl schema::Taggable for Person {
fn id(&self) -> Cow<str> {
schema::ResolvePerson::id(self)
}
fn tags(&self) -> Vec<Cow<str>> {
schema::ResolvePerson::tags(self)
}
}
impl schema::HasSchema for QueryRoot {
type Schema = Schema;
}
impl schema::ResolveQueryRoot for QueryRoot {
fn person(&self, id: Cow<str>) -> Person {
Person {
id: id.to_string(),
first_name: String::from("Jonas"),
last_name: String::from("Nicklas"),
national_id: None,
country: Country::SWEDEN,
age: 30,
}
}
fn post(&self, id: Cow<str>) -> Post {
Post {
id: id.to_string(),
title: String::from("Hello GraphQL"),
tags: vec![String::from("GraphQL"), String::from("Rust")],
}
}
fn tagged(&self, _tags: Option<Vec<Cow<str>>>) -> Vec<Box<schema::Taggable<Schema=<Self as schema::HasSchema>::Schema>>> {
vec![
Box::new(self.person("6543".into())),
Box::new(self.post("9876".into())),
]
}
fn search(&self, _keyword: Cow<str>) -> Vec<schema::SearchResult<Schema>> {
vec![
schema::SearchResult::Person(self.person("6543".into())),
schema::SearchResult::Post(self.post("9876".into())),
]
}
fn inhabitants(&self, country: Country) -> Vec<Person> {
vec![
Person {
id: "123".to_string(),
first_name: String::from("Jonas"),
last_name: String::from("Nicklas"),
national_id: None,
country: country,
age: 30,
}
]
}
fn locate(&self, location: Location) -> Option<Person> {
if location.lat == 12 && location.lng == 20 {
Some(self.person("1220".into()))
} else {
None
}
}
fn person_by_national_id(&self, id: NationalId) -> Option<Person> {
Some(Person {
id: "123".to_string(),
national_id: Some(id.clone()),
first_name: String::from("Jonas"),
last_name: String::from("Nicklas"),
country: Country::SWEDEN,
age: 30,
})
}
}
impl schema::HasSchema for Post {
type Schema = Schema;
}
impl schema::ResolvePost for Post {
fn id(&self) -> Cow<str> {
self.id.as_str().into()
}
fn title(&self) -> Cow<str> {
self.title.as_str().into()
}
fn tags(&self) -> Vec<Cow<str>> {
self.tags.iter().map(|v| v.as_str().into()).collect::<Vec<_>>().into()
}
}
impl schema::Taggable for Post {
fn id(&self) -> Cow<str> {
schema::ResolvePost::id(self)
}
fn tags(&self) -> Vec<Cow<str>> {
schema::ResolvePost::tags(self)
}
}