calebmer / graphql-resolve-batch

A GraphQL batching model which groups execution by GraphQL fields.
MIT License
267 stars 12 forks source link

Error: Expected Iterable, but did not find one #9

Closed runemadsen closed 6 years ago

runemadsen commented 6 years ago

I'm not sure whether this package still works for graphql-tools. I have the following code:

type Product {
  images: [Image]
}

type Image {
  url: String
}

And this resolver:

{
Product: {
    images: createBatchResolver(async products => {
      const pids = products.map(p => p.id);
      const images = await db.find("images", { productId: pids });
      console.log(images);
      return images;
    })
}

I am getting the following error: Error: Expected Iterable, but did not find one for field Product.images.. However, my console.log logs an array of images. Is this a problem with async/await?

runemadsen commented 6 years ago

For others with this problem, you have to filter the db results back to an array ordered by the original ids:

{
Product: {
    images: createBatchResolver(async products => {
      const pids = products.map(p => p.id);
      const images = await db.find("images", { productId: pids });
      return pids.map(id => images.filter(img => img.productId === id));
    })
}