Values with the same key can't be returned more than once from the dataloader. I think this is because the value from the HashMap is removed here https://github.com/async-graphql/async-graphql/blob/master/src/dataloader/mod.rs#L166 ?. This is my first time using a dataloader pattern with GraphQL so unsure if this is expected behaviour or not.
When loading data using a dataloader if two values have the same hash key the dataloader will return the same data for values with the same key.
So for example a query like this where multiple users can have the same account (or organisation) should return the same account for all users where their account ID is the same.
query {
users{
id
accountId
account {
id
}
}
}
should return
{
"data": {
"users": [
{
"account": {
"id": "4ed99ea5-bff8-47ec-91f8-e74b73d5f9d1"
},
"accountId": "4ed99ea5-bff8-47ec-91f8-e74b73d5f9d1",
"id": "19164642-c3ee-48fe-b459-dfcb5544833c"
},
{
"account": {
"id": "f7496608-99d6-462f-996b-8b21507e60f1"
},
"accountId": "f7496608-99d6-462f-996b-8b21507e60f1",
"id": "05282b7b-1bf2-4774-8b18-edad120f00f4"
},
{
"account": {
"id": "f7496608-99d6-462f-996b-8b21507e60f1"
},
"accountId": "4ed99ea5-bff8-47ec-91f8-e74b73d5f9d1",
"id": "c6f2f96e-ab06-4381-b12a-15783d26ad25"
},
{
"account": {
"id": "f7496608-99d6-462f-996b-8b21507e60f1"
},
"accountId": "f7496608-99d6-462f-996b-8b21507e60f1",
"id": "99fb4448-34ab-44c6-bd2f-96dc026a6f1d"
},
]
}
}
Records with the same key return the correct value once but then return None and the graphql value evaluates to Null.
{
"data": {
"users": [
{
"account": {
"id": "4ed99ea5-bff8-47ec-91f8-e74b73d5f9d1"
},
"accountId": "4ed99ea5-bff8-47ec-91f8-e74b73d5f9d1",
"id": "19164642-c3ee-48fe-b459-dfcb5544833c"
},
{
"account": {
"id": "f7496608-99d6-462f-996b-8b21507e60f1"
},
"accountId": "f7496608-99d6-462f-996b-8b21507e60f1",
"id": "05282b7b-1bf2-4774-8b18-edad120f00f4"
},
{
"account": null,
"accountId": "4ed99ea5-bff8-47ec-91f8-e74b73d5f9d1",
"id": "c6f2f96e-ab06-4381-b12a-15783d26ad25"
},
{
"account": null,
"accountId": "f7496608-99d6-462f-996b-8b21507e60f1",
"id": "99fb4448-34ab-44c6-bd2f-96dc026a6f1d"
},
]
}
}
My data loader
#[async_trait::async_trait]
impl Loader<Uuid> for AccountLoader {
type Value = Account;
type Error = FieldError;
async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> {
let fetch = sqlx::query_as!(Account, "select id from accounts where id = any($1) ", keys)
.fetch_all(&*self.shared_pool)
.await?;
let accounts = fetch
.into_iter()
.map(|account: Account| (account.id, account.clone()))
.collect();
Ok(accounts)
}
}
on Version 2.4.6
I'm so sorry this is a bug, I fixed it in v2.5.7.
@sunli829 thanks so much, I've tested this on the master branch and confirms it fixes my issue :partying_face: . Thanks for the quick response. While i'm here I just want to say I'm loving async-graphql, its a great library and the docs are brilliant. Keep it up :smile:
@sunli829 thanks so much, I've tested this on the master branch and confirms it fixes my issue 馃コ . Thanks for the quick response. While i'm here I just want to say I'm loving async-graphql, its a great library and the docs are brilliant. Keep it up 馃槃
Haha, I am very happy to be able to help you.