Update book. #537
This commit is contained in:
parent
23bcf4cdfd
commit
c3febc7ab1
|
@ -69,6 +69,7 @@ The following is an example of using `DataLoader` to optimize queries::
|
|||
use async_graphql::*;
|
||||
use async_graphql::dataloader::*;
|
||||
use itertools::Itertools;
|
||||
use std::sync::Arc;
|
||||
|
||||
struct UserNameLoader {
|
||||
pool: sqlx::Pool<Postgres>,
|
||||
|
@ -77,7 +78,7 @@ struct UserNameLoader {
|
|||
#[async_trait::async_trait]
|
||||
impl Loader<u64> for UserNameLoader {
|
||||
type Value = String;
|
||||
type Error = sqlx::Error;
|
||||
type Error = Arc<sqlx::Error>;
|
||||
|
||||
async fn load(&self, keys: &[u64]) -> Result<HashMap<u64, Self::Value>, Self::Error> {
|
||||
let pool = ctx.data_unchecked::<Pool<Postgres>>();
|
||||
|
@ -85,6 +86,7 @@ impl Loader<u64> for UserNameLoader {
|
|||
Ok(sqlx::query_as(query)
|
||||
.fetch(&self.pool)
|
||||
.map_ok(|name: String| name)
|
||||
.map_err(Arc::new)
|
||||
.try_collect().await?)
|
||||
}
|
||||
}
|
||||
|
@ -122,7 +124,7 @@ struct PostgresLoader {
|
|||
#[async_trait::async_trait]
|
||||
impl Loader<UserId> for PostgresLoader {
|
||||
type Value = User;
|
||||
type Error = sqlx::Error;
|
||||
type Error = Arc<sqlx::Error>;
|
||||
|
||||
async fn load(&self, keys: &[UserId]) -> Result<HashMap<UserId, Self::Value>, Self::Error> {
|
||||
// Load users from database
|
||||
|
|
|
@ -67,6 +67,7 @@ SELECT name FROM user WHERE id = $1
|
|||
use async_graphql::*;
|
||||
use async_graphql::dataloader::*;
|
||||
use itertools::Itertools;
|
||||
use std::sync::Arc;
|
||||
|
||||
struct UserNameLoader {
|
||||
pool: sqlx::Pool<Postgres>,
|
||||
|
@ -75,14 +76,15 @@ struct UserNameLoader {
|
|||
#[async_trait::async_trait]
|
||||
impl Loader<u64> for UserNameLoader {
|
||||
type Value = String;
|
||||
type Error = sqlx::Error;
|
||||
|
||||
type Error = Arc<sqlx::Error>;
|
||||
|
||||
async fn load(&self, keys: &[u64]) -> Result<HashMap<u64, Self::Value>, Self::Error> {
|
||||
let pool = ctx.data_unchecked::<Pool<Postgres>>();
|
||||
let query = format!("SELECT name FROM user WHERE id IN ({})", keys.iter().join(","));
|
||||
Ok(sqlx::query_as(query)
|
||||
.fetch(&self.pool)
|
||||
.map_ok(|name: String| name)
|
||||
.map_err(Arc::new)
|
||||
.try_collect().await?)
|
||||
}
|
||||
}
|
||||
|
@ -120,7 +122,7 @@ struct PostgresLoader {
|
|||
#[async_trait::async_trait]
|
||||
impl Loader<UserId> for PostgresLoader {
|
||||
type Value = User;
|
||||
type Error = sqlx::Error;
|
||||
type Error = Arc<sqlx::Error>;
|
||||
|
||||
async fn load(&self, keys: &[UserId]) -> Result<HashMap<UserId, Self::Value>, Self::Error> {
|
||||
// 从数据库中加载User
|
||||
|
|
Loading…
Reference in New Issue