versatile-dataloader

Crates.ioversatile-dataloader
lib.rsversatile-dataloader
version0.1.1
created_at2025-05-18 13:49:43.982253+00
updated_at2025-06-15 16:31:39.876528+00
descriptionAn async data loader for Rust.
homepage
repositoryhttps://github.com/worldofjoni/versatile-dataloader
max_upload_size
id1678656
size57,131
Jonatan Ziegler (worldofjoni)

documentation

README

Versatile Dataloader

A dataloader (originally developed by facebook) allows to batch requests coming in at roughly the same time. This is useful e.g., to avoid the n+1 problem in GraphQL.

Forked/extracted from async-graphql as it has no dependencies towards async-graphql and might be useful on its own.

Example

use std::collections::{HashSet, HashMap};
use std::convert::Infallible;
use versatile_dataloader::{Loader, DataLoader};

/// This loader simply converts the integer key into a string value.
struct MyLoader;

impl Loader<i32, String> for MyLoader {
    type Error = Infallible;

    async fn load(&self, keys: &[i32]) -> Result<HashMap<i32, String>, Self::Error> {
        // Implement database access or similar here.
        Ok(keys.iter().copied().map(|n| (n, n.to_string())).collect())
    }
}


tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {

// Load data with dataloader:
let loader = DataLoader::new(MyLoader, tokio::spawn);
let strings = vec![loader.load_one(1).await.unwrap(), loader.load_one(2).await.unwrap(), loader.load_one(3).await.unwrap()];
// The dataloader load function is only called once.

assert_eq!(strings, vec![Some("1".to_string()), Some("2".to_string()), Some("3".to_string())]);

});

Related projects

  • async-graphql includes a dataloder where this one is based on. We adapted the API to make the return type generic rather than an associated type. This allows to use one struct as a dataloader for different return types all with the same key (e.g., a UUID).
  • dataloader-rs is similar, but it has a less versatile API that does not support errors.

License

Licensed under either of

Commit count: 35

cargo fmt