From 13fc0dadeb61e87b03051439242745fb19c2e384 Mon Sep 17 00:00:00 2001
From: jsus <esseswann@gmail.com>
Date: Fri, 24 Sep 2021 14:35:46 +0300
Subject: [PATCH] Using newtype for context

---
 docs/book/content/types/objects/using_contexts.md | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/docs/book/content/types/objects/using_contexts.md b/docs/book/content/types/objects/using_contexts.md
index 1b65771c..a2174ad4 100644
--- a/docs/book/content/types/objects/using_contexts.md
+++ b/docs/book/content/types/objects/using_contexts.md
@@ -101,23 +101,29 @@ For example, when using async runtime with [work stealing][2] (like `tokio`), wh
 # use juniper::graphql_object;
 use tokio::sync::RwLock;
 
-impl juniper::Context for Database {}
-
 struct Database {
     requested_count: HashMap<i32, i32>,
 }
 
+// Since we cannot directly implement juniper::Context
+// for RwLock we use the newtype idiom
+struct DatabaseContext(RwLock<Database>)
+
+impl juniper::Context for DatabaseContext {}
+
 struct User {
     id: i32,
     name: String
 }
 
-#[graphql_object(context=RwLock<Database>)]
+#[graphql_object(context=DatabaseContext)]
 impl User {
-    async fn times_requested<'db>(&self, context: &'db RwLock<Database>) -> i32 {
+    async fn times_requested<'db>(&self, context: &'db DatabaseContext) -> i32 {
         // Acquire a mutable reference and await if async RwLock is used,
         // which is necessary if context consists async operations like 
         // querying remote databases.
+        // Obtain base type
+        let DatabaseContext(context) = context;
         // If context is immutable use .read() on RwLock.
         let mut context = context.write().await;
         // Preform a mutable operation.