You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you are using async runtime like `tokio` for mutable references you will need to use a corresponding async version of RwLock:
88
+
using e.g. `RwLock` or `RefCell`.
89
+
90
+
91
+
92
+
93
+
## Dealing with mutable references
94
+
95
+
Context cannot be specified by a mutable reference, because concurrent fields resolving may be performed. If you have something in your context that requires access by mutable reference, then you need to leverage the [interior mutability][1] for that.
96
+
97
+
For example, when using async runtime with [work stealing][2] (like `tokio`), which obviously requires thread safety in addition, you will need to use a corresponding async version of `RwLock`:
91
98
```rust
92
99
# externcrate juniper;
93
100
# usestd::collections::HashMap;
94
101
# usejuniper::graphql_object;
95
-
# usetokio::sync::RwLock;
96
-
#
97
-
// This struct represents our context.
102
+
usetokio::sync::RwLock;
103
+
98
104
structDatabase {
99
105
requested_count:HashMap<i32, i32>,
100
106
}
101
107
102
-
// Mark the Database as a valid context type for Juniper
103
108
impljuniper::ContextforDatabase {}
104
109
105
110
structUser {
@@ -108,21 +113,15 @@ struct User {
108
113
times_requested:i32,
109
114
}
110
115
111
-
// Assign Database as the context type for User and envelope it in RwLock
0 commit comments