This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.rs
216 lines (192 loc) · 5.77 KB
/
user.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
use async_graphql::{Context, EmptyMutation, EmptySubscription, Object, Schema};
use entity::*;
use entity_async_graphql::*;
use entity_inmemory::InmemoryDatabase;
#[simple_ent]
#[derive(EntObject, EntFilter)]
struct User {
name: String,
age: u8,
#[ent(edge)]
address: Address,
}
#[simple_ent]
#[derive(EntObject, EntFilter)]
struct Address {
street: String,
city: String,
}
/// Represents the root of our async-graphql query interface
struct RootQuery;
#[Object]
impl RootQuery {
/// Supports retrieving an ent by its id or general filter
async fn ent(
&self,
ctx: &Context<'_>,
id: Option<Id>,
filter: Option<GqlEntFilter>,
) -> async_graphql::Result<Vec<GqlDynEnt>> {
let db = ctx.data::<DatabaseRc>()?;
if let Some(id) = id {
db.get_all(vec![id])
.map(|ents| ents.into_iter().map(GqlDynEnt::from).collect())
.map_err(|x| async_graphql::Error::new(x.to_string()))
} else if let Some(filter) = filter {
db.find_all(filter.into())
.map(|ents| ents.into_iter().map(GqlDynEnt::from).collect())
.map_err(|x| async_graphql::Error::new(x.to_string()))
} else {
Err(async_graphql::Error::new("Must provide one argument"))
}
}
/// Supports retrieving an address by its id or specific filter
async fn address(
&self,
ctx: &Context<'_>,
id: Option<Id>,
filter: Option<GqlAddressFilter>,
) -> async_graphql::Result<Vec<Address>> {
let db = ctx.data::<DatabaseRc>()?;
if let Some(id) = id {
db.get_all_typed::<Address>(vec![id])
.map_err(|x| async_graphql::Error::new(x.to_string()))
} else if let Some(filter) = filter {
db.find_all_typed::<Address>(filter.into())
.map_err(|x| async_graphql::Error::new(x.to_string()))
} else {
Err(async_graphql::Error::new("Must provide one argument"))
}
}
/// Supports retrieving a user by its id or specific filter
async fn user(
&self,
ctx: &Context<'_>,
id: Option<Id>,
filter: Option<GqlUserFilter>,
) -> async_graphql::Result<Vec<User>> {
let db = ctx.data::<DatabaseRc>()?;
if let Some(id) = id {
db.get_all_typed::<User>(vec![id])
.map_err(|x| async_graphql::Error::new(x.to_string()))
} else if let Some(filter) = filter {
db.find_all_typed::<User>(filter.into())
.map_err(|x| async_graphql::Error::new(x.to_string()))
} else {
Err(async_graphql::Error::new("Must provide one argument"))
}
}
}
fn main() {
let db = preload_db();
let schema = Schema::build(RootQuery, EmptyMutation, EmptySubscription)
.data(WeakDatabaseRc::upgrade(&db).unwrap())
.finish();
// Do a generic search across all ents for a field "name" that ends with
// the text "Flintstone" so we pull out the Flintstones
run_query_and_print(
&schema,
r#"
{
ent(filter: {
fields: [
{
name: "name"
predicate: {
text_ends_with: "Flintstone"
}
}
]
}) {
id
name: field(name: "name")
}
}
"#,
);
// Now, do the same query using our user-specific filter
run_query_and_print(
&schema,
r#"
{
user(filter: { name: { text_ends_with: "Flintstone" } }) {
id
name
}
}
"#,
);
// Demonstrate finding users by their address
run_query_and_print(
&schema,
r#"
{
user(filter: {
address: {
city: {
equals: "Bedrock"
}
}
}) {
id
name
}
}
"#,
);
}
#[inline]
fn run_query_and_print(schema: &Schema<RootQuery, EmptyMutation, EmptySubscription>, input: &str) {
let input = input.trim();
println!("Query: {}", input);
let res = futures::executor::block_on(schema.execute(input));
match serde_json::to_string(&res) {
Ok(x) => println!("Result: {}", x),
Err(_) => println!("Result: {:?}", res),
}
}
fn preload_db() -> WeakDatabaseRc {
let db = InmemoryDatabase::default();
entity::global::set_db(db);
let flintstone_address = Address::build()
.street("345 Cave Stone Road".to_string())
.city("Bedrock".to_string())
.finish_and_commit()
.unwrap()
.unwrap();
let _ = User::build()
.name("Fred Flintstone".to_string())
.age(58)
.address(flintstone_address.id())
.finish_and_commit()
.unwrap()
.unwrap();
let _ = User::build()
.name("Wilma Flintstone".to_string())
.age(57)
.address(flintstone_address.id())
.finish_and_commit()
.unwrap()
.unwrap();
let rubble_address = Address::build()
.street("123 Cave Stone Road".to_string())
.city("Bedrock".to_string())
.finish_and_commit()
.unwrap()
.unwrap();
let _ = User::build()
.name("Barney Rubble".to_string())
.age(55)
.address(rubble_address.id())
.finish_and_commit()
.unwrap()
.unwrap();
let _ = User::build()
.name("Betty Rubble".to_string())
.age(51)
.address(rubble_address.id())
.finish_and_commit()
.unwrap()
.unwrap();
entity::global::db()
}