Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Federation + Dataloader work #431

Closed
Boshen opened this issue Mar 5, 2021 · 7 comments
Closed

make Federation + Dataloader work #431

Boshen opened this issue Mar 5, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@Boshen
Copy link

Boshen commented Mar 5, 2021

When Apollo Gateway sends something like

  query Query {
    _entities(representations: [{__typename:"User", userId:1},{__typename:"User", userId:2}]) {
      ...on User {
        userId
      }
    }
  }

dataloader inside #[graphql(entity)] is not working at the moment.

I guess the relevant code is here, but I couldn't investigate no further..:

                for item in representations {
                    res.push(
                        self.inner.find_entity(ctx, &item.0).await?.ok_or_else(|| {
                            ServerError::new("Entity not found.").at(ctx.item.pos)
                        })?,
                    );
                }
@Boshen Boshen added the enhancement New feature or request label Mar 5, 2021
@sunli829
Copy link
Collaborator

sunli829 commented Mar 5, 2021

I'll check this out tomorrow.😄

@sunli829
Copy link
Collaborator

sunli829 commented Mar 6, 2021

I added an example:

pub async fn test_find_entity_with_context() {

@Boshen
Copy link
Author

Boshen commented Mar 6, 2021

@sunli829 I think you need to check the keys, otherwise your test passes despite dataloader malfunctioning:

        async fn load(&self, keys: &[ID]) -> Result<HashMap<ID, Self::Value>, Self::Error> {
            assert_eq!(
                vec![ID::from("1"), ID::from("2"), ID::from("3"), ID::from("4")],
                keys
            );
           ...
        }

I'm still learning async rust, but after some debugging it seems Delay::new(self.delay).await; inside dataloader.load_manyis not blocking inside the for item in representations loop

@Boshen
Copy link
Author

Boshen commented Mar 6, 2021

Using join_all worked:

                let representations: Vec<Any> = ctx.param_value("representations", None)?;
                let res = join_all(
                    representations
                        .iter()
                        .map(|item| self.inner.find_entity(ctx, &item.0)),
                )
                .await
                .into_iter()
                .map(|item| {
                    item.unwrap()
                        .ok_or_else(|| ServerError::new("Entity not found.").at(ctx.item.pos))
                        .unwrap()
                })
                .collect();
                return Ok(Some(Value::List(res)));
            }

But I guess there is a better way ...

@sunli829
Copy link
Collaborator

sunli829 commented Mar 7, 2021

Using join_all worked:

                let representations: Vec<Any> = ctx.param_value("representations", None)?;
                let res = join_all(
                    representations
                        .iter()
                        .map(|item| self.inner.find_entity(ctx, &item.0)),
                )
                .await
                .into_iter()
                .map(|item| {
                    item.unwrap()
                        .ok_or_else(|| ServerError::new("Entity not found.").at(ctx.item.pos))
                        .unwrap()
                })
                .collect();
                return Ok(Some(Value::List(res)));
            }

But I guess there is a better way ...

It is indeed better to use join_all, thank you for your suggestion. 🙂

sunli829 added a commit that referenced this issue Mar 7, 2021
@sunli829
Copy link
Collaborator

sunli829 commented Mar 7, 2021

Released in v2.5.11

@Boshen
Copy link
Author

Boshen commented Mar 7, 2021

🌹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants