cksac / dataloader-rs

Rust implementation of Facebook's DataLoader using async-await.
Apache License 2.0
261 stars 23 forks source link

how to pass argument to load method #26

Closed fbucek closed 3 years ago

fbucek commented 3 years ago

I am using async_graphql and dataloader and it works fine.

But when I want to limit output using last argument. I do not know how to do this with dataloader.

authors {
 name
 books(last: 30)
}

Getting books is called like this in graphql object ( without data loader )

pub async fn books(
    &self,
    ctx: &Context<'_>,
    last: i32,
) -> Result<Vec<Books>, AppError> {
    ctx.data_unchecked::<BooksRepository>()
        .get_for_id(self.id, last)
        .await
}

When using dataloader I do not know how to make it work.

pub async fn books(
    &self,
    ctx: &Context<'_>,
    last: i32,
) -> Result<Vec<books>, AppError> {
    let loader = ctx.data_unchecked::<BooksLoader>();
    loader.load(self.id).await
}

Maybe I just overlooked something or just do not have mu day, but I do not know how to solve it. Thanks for suggestion.

cksac commented 3 years ago

You can implement with other key type, e.g BatchFn<(usize, usize)>, but in this case (1, 20) and (1, 30) is treat as two different request.

fbucek commented 3 years ago

Thanks for suggestion, I have tried Arc<Mutex<Config>> mutex, so I can set variables for method in struct which implements BatchFn, but then I have realized that I do not know how to do SQL query to return for X items last X attributes.

For example for last 10 authors get last 10 books. Now it is 11 queries, but I do not know how to make it in 2 queries in SQL regardless of dataloader.

I am closing, thanks for advice. I will try it when I solve SQL problem.