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
Copy file name to clipboardExpand all lines: docs/book/content/advanced/dataloaders.md
+31-24Lines changed: 31 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -35,8 +35,12 @@ Once the list of users has been returned, a separate query is run to find the cu
35
35
You can see how this could quickly become a problem.
36
36
37
37
A common solution to this is to introduce a **dataloader**.
38
-
This can be done with Juniper using the crate [cksac/dataloader-rs](https://github.com/cksac/dataloader-rs), which has two types of dataloaders; cached and non-cached. This example will explore the non-cached option.
38
+
This can be done with Juniper using the crate [cksac/dataloader-rs](https://github.com/cksac/dataloader-rs), which has two types of dataloaders; cached and non-cached.
39
39
40
+
#### Cached Loader
41
+
DataLoader provides a memoization cache, after .load() is called once with a given key, the resulting value is cached to eliminate redundant loads.
42
+
43
+
DataLoader caching does not replace Redis, Memcache, or any other shared application-level cache. DataLoader is first and foremost a data loading mechanism, and its cache only serves the purpose of not repeatedly loading the same data in the context of a single request to your Application. [(read more)](https://github.com/graphql/dataloader#caching)
40
44
41
45
### What does it look like?
42
46
@@ -47,16 +51,17 @@ This can be done with Juniper using the crate [cksac/dataloader-rs](https://gith
Once created, a dataloader has the functions `.load()` and `.load_many()`.
131
-
When called these return a Future.
132
-
In the above example `cult_loader.load(id: i32)` returns `Future<Cult>`. If we had used `cult_loader.load_many(Vec<i32>)` it would have returned `Future<Vec<Cult>>`.
140
+
Once created, a dataloader has the async functions `.load()` and `.load_many()`.
141
+
In the above example `cult_loader.load(id: i32).await` returns `Cult`. If we had used `cult_loader.load_many(Vec<i32>).await` it would have returned `Vec<Cult>`.
133
142
134
143
135
144
### Where do I create my dataloaders?
@@ -165,15 +174,13 @@ pub async fn graphql(
165
174
st: web::Data<Arc<Schema>>,
166
175
data: web::Json<GraphQLRequest>,
167
176
) -> Result<HttpResponse, Error> {
168
-
let mut rt = futures::executor::LocalPool::new();
169
177
170
178
// Context setup
171
179
let cult_loader = get_loader();
172
180
let ctx = Context::new(cult_loader);
173
181
174
182
// Execute
175
-
let future_execute = data.execute(&st, &ctx);
176
-
let res = rt.run_until(future_execute);
183
+
let res = data.execute(&st, &ctx).await;
177
184
let json = serde_json::to_string(&res).map_err(error::ErrorInternalServerError)?;
0 commit comments