Fixes for async example

This commit is contained in:
jsus 2021-09-20 23:39:24 +03:00 committed by Christian Legnitto
parent dfda435bc7
commit 18c479e999

View file

@ -105,24 +105,21 @@ struct Database {
requested_count: HashMap<i32, i32>, requested_count: HashMap<i32, i32>,
} }
impl juniper::Context for Database {}
struct User { struct User {
id: i32, id: i32,
name: String, name: String
times_requested: i32,
} }
#[graphql_object(context = RwLock<Database>)] #[graphql_object(context=RwLock<Database>)]
impl User { impl User {
async fn times_requested<'db>(&self, context: &'db RwLock<Database>) -> Vec<&'db User> { async fn times_requested<'db>(&self, context: &'db RwLock<Database>) -> i32 {
// Acquire a mutable reference and await if async RwLock is used, // Acquire a mutable reference and await if async RwLock is used,
// which is necessary if context consists async operations like // which is necessary if context consists async operations like
// querying remote databases. // querying remote databases.
// If context is immutable use .read() on RwLock. // If context is immutable use .read() on RwLock.
let mut context = context.write().await; let mut context = context.write().await;
// Preform a mutable operation. // Preform a mutable operation.
context.requested_count.entry(self.id).and_modify(|e| { *e += 1 }).or_insert(1) context.requested_count.entry(self.id).and_modify(|e| { *e += 1 }).or_insert(1).clone()
} }
fn name(&self) -> &str { fn name(&self) -> &str {