Crate channel_loader[][src]

Expand description
use async_graphql::SimpleObject;
use channel_loader::diesel::{DieselError, DieselLoader};
use db::schema::users;
use diesel::prelude::*;
use diesel_connection::PooledConnection;
use std::collections::HashMap;

derive_id! {
  #[derive(Identifiable)]
  #[table_name = "users"]
  #[graphql(name = "UserID")]
  pub struct UserId( #[column_name = "id"] i32);
}

#[derive(SimpleObject, Identifiable, Associations, Queryable, Debug, Clone)]
#[belongs_to(UserId, foreign_key = "id")]
#[table_name = "users"]
pub struct User {
  pub id: i32,
  pub name: String,
}

#[derive(Default)]
pub struct UserLoader;

impl DieselLoader for UserLoader {
  type Key = UserId;
  type Value = Vec<User>;
  fn load(
    conn: PooledConnection,
    keys: Vec<UserId>,
  ) -> Result<HashMap<Self::Key, Self::Value>, DieselError> {
    let users: Vec<User> = User::belonging_to(&keys)
      .select(users::all_columns)
      .load::<User>(&conn)?;

    let grouped_users = users.grouped_by(&keys);

    let mut data: HashMap<UserId, Vec<User>> = HashMap::new();

    data.extend(keys.into_iter().zip(grouped_users.into_iter()));

    Ok(data)
  }
}

define_static_loader!(UserLoader);
attach_loader!(User, UserLoader);

Modules

diesel
task

Macros

attach_loader

Implements Loadable using the corresponding static instance as defined by define_static_loader

define_static_loader

Defines a static DataLoader instance from a type that implements TaskHandler and registers reactor initialization to be handled via booter::boot();

Traits

Key

Params to crate::Loadable::load_by; typically i32 or newtype wrapper

Loadable

Loadable types load using the corresponding static loader associated by type via define_static_loader