-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmodel.rs
189 lines (182 loc) · 7.43 KB
/
model.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
use async_graphql::{dynamic::*, Value, ID};
use futures_util::StreamExt;
use crate::{simple_broker::SimpleBroker, Book, BookChanged, MutationType, Storage};
impl From<MutationType> for FieldValue<'_> {
fn from(value: MutationType) -> Self {
match value {
MutationType::Created => FieldValue::value("CREATED"),
MutationType::Deleted => FieldValue::value("DELETED"),
}
}
}
pub fn schema() -> Result<Schema, SchemaError> {
let mutation_type = Enum::new("MutationType")
.item(EnumItem::new("CREATED").description("New book created."))
.item(EnumItem::new("DELETED").description("Current book deleted."));
let book = Object::new("Book")
.description("A book that will be stored.")
.field(Field::new("id", TypeRef::named_nn(TypeRef::ID), |ctx| {
FieldFuture::new(async move {
let book = ctx.parent_value.try_downcast_ref::<Book>()?;
Ok(Some(Value::from(book.id.to_owned())))
})
}))
.field(Field::new(
"name",
TypeRef::named_nn(TypeRef::STRING),
|ctx| {
FieldFuture::new(async move {
let book = ctx.parent_value.try_downcast_ref::<Book>()?;
Ok(Some(Value::from(book.name.to_owned())))
})
},
))
.field(Field::new(
"author",
TypeRef::named_nn(TypeRef::STRING),
|ctx| {
FieldFuture::new(async move {
let book = ctx.parent_value.try_downcast_ref::<Book>()?;
Ok(Some(Value::from(book.author.to_owned())))
})
},
));
let book_changed = Object::new("BookChanged")
.field(Field::new(
"mutationType",
TypeRef::named_nn(mutation_type.type_name()),
|ctx| {
FieldFuture::new(async move {
let book_changed = ctx.parent_value.try_downcast_ref::<BookChanged>()?;
Ok(Some(FieldValue::from(book_changed.mutation_type)))
})
},
))
.field(Field::new("id", TypeRef::named_nn(TypeRef::ID), |ctx| {
FieldFuture::new(async move {
let book_changed = ctx.parent_value.try_downcast_ref::<BookChanged>()?;
Ok(Some(Value::from(book_changed.id.to_owned())))
})
}))
.field(Field::new(
"book",
TypeRef::named(book.type_name()),
|ctx| {
FieldFuture::new(async move {
let book_changed = ctx.parent_value.try_downcast_ref::<BookChanged>()?;
let id = book_changed.id.to_owned();
let book_id = id.parse::<usize>()?;
let store = ctx.data_unchecked::<Storage>().lock().await;
let book = store.get(book_id).cloned();
Ok(book.map(FieldValue::owned_any))
})
},
));
let query_root = Object::new("Query")
.field(Field::new(
"getBooks",
TypeRef::named_list(book.type_name()),
|ctx| {
FieldFuture::new(async move {
let store = ctx.data_unchecked::<Storage>().lock().await;
let books: Vec<Book> = store.iter().map(|(_, book)| book.clone()).collect();
Ok(Some(FieldValue::list(
books.into_iter().map(FieldValue::owned_any),
)))
})
},
))
.field(
Field::new("getBook", TypeRef::named(book.type_name()), |ctx| {
FieldFuture::new(async move {
let id = ctx.args.try_get("id")?;
let book_id = match id.string() {
Ok(id) => id.to_string(),
Err(_) => id.u64()?.to_string(),
};
let book_id = book_id.parse::<usize>()?;
let store = ctx.data_unchecked::<Storage>().lock().await;
let book = store.get(book_id).cloned();
Ok(book.map(FieldValue::owned_any))
})
})
.argument(InputValue::new("id", TypeRef::named_nn(TypeRef::ID))),
);
let mutatation_root = Object::new("Mutation")
.field(
Field::new("createBook", TypeRef::named(TypeRef::ID), |ctx| {
FieldFuture::new(async move {
let mut store = ctx.data_unchecked::<Storage>().lock().await;
let name = ctx.args.try_get("name")?;
let author = ctx.args.try_get("author")?;
let entry = store.vacant_entry();
let id: ID = entry.key().into();
let book = Book {
id: id.clone(),
name: name.string()?.to_string(),
author: author.string()?.to_string(),
};
entry.insert(book);
let book_mutated = BookChanged {
mutation_type: MutationType::Created,
id: id.clone(),
};
SimpleBroker::publish(book_mutated);
Ok(Some(Value::from(id)))
})
})
.argument(InputValue::new("name", TypeRef::named_nn(TypeRef::STRING)))
.argument(InputValue::new(
"author",
TypeRef::named_nn(TypeRef::STRING),
)),
)
.field(
Field::new("deleteBook", TypeRef::named_nn(TypeRef::BOOLEAN), |ctx| {
FieldFuture::new(async move {
let mut store = ctx.data_unchecked::<Storage>().lock().await;
let id = ctx.args.try_get("id")?;
let book_id = match id.string() {
Ok(id) => id.to_string(),
Err(_) => id.u64()?.to_string(),
};
let book_id = book_id.parse::<usize>()?;
if store.contains(book_id) {
store.remove(book_id);
let book_mutated = BookChanged {
mutation_type: MutationType::Deleted,
id: book_id.into(),
};
SimpleBroker::publish(book_mutated);
Ok(Some(Value::from(true)))
} else {
Ok(Some(Value::from(false)))
}
})
})
.argument(InputValue::new("id", TypeRef::named_nn(TypeRef::ID))),
);
let subscription_root = Subscription::new("Subscription").field(SubscriptionField::new(
"bookMutation",
TypeRef::named_nn(book_changed.type_name()),
|_| {
SubscriptionFieldFuture::new(async {
Ok(SimpleBroker::<BookChanged>::subscribe()
.map(|book| Ok(FieldValue::owned_any(book))))
})
},
));
Schema::build(
query_root.type_name(),
Some(mutatation_root.type_name()),
Some(subscription_root.type_name()),
)
.register(mutation_type)
.register(book)
.register(book_changed)
.register(query_root)
.register(subscription_root)
.register(mutatation_root)
.data(Storage::default())
.finish()
}