Skip to content

Commit 18c479e

Browse files
esseswannLegNeato
authored andcommitted
Fixes for async example
1 parent dfda435 commit 18c479e

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

docs/book/content/types/objects/using_contexts.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,21 @@ struct Database {
105105
requested_count: HashMap<i32, i32>,
106106
}
107107

108-
impl juniper::Context for Database {}
109-
110108
struct User {
111109
id: i32,
112-
name: String,
113-
times_requested: i32,
110+
name: String
114111
}
115112

116-
#[graphql_object(context = RwLock<Database>)]
113+
#[graphql_object(context=RwLock<Database>)]
117114
impl User {
118-
async fn times_requested<'db>(&self, context: &'db RwLock<Database>) -> Vec<&'db User> {
115+
async fn times_requested<'db>(&self, context: &'db RwLock<Database>) -> i32 {
119116
// Acquire a mutable reference and await if async RwLock is used,
120117
// which is necessary if context consists async operations like
121118
// querying remote databases.
122119
// If context is immutable use .read() on RwLock.
123120
let mut context = context.write().await;
124121
// Preform a mutable operation.
125-
context.requested_count.entry(self.id).and_modify(|e| { *e += 1 }).or_insert(1)
122+
context.requested_count.entry(self.id).and_modify(|e| { *e += 1 }).or_insert(1).clone()
126123
}
127124

128125
fn name(&self) -> &str {

0 commit comments

Comments
 (0)