0

everybody! I'm trying to use Dataloader by facebook in my graphql project. So, now I'm faced to the next problem. When I ask my database for data by ids for example: select * from books where books.author in (4,5,6,7) I got an Error: "function did not return a Promise of an Array of the same length as the Array of keys". Cause by id 4 I can fetch more then just one book. Does anybody know how to fix it?

2 Answers2

1

Dataloader is expecting you to return an array of the same length as the input to your loader. So, if the loader gets [4,5,6,7] as an input, it will need to return an array with a length of 4. Also keep in mind that the results returned from the loader need to be in the same order as the input ids. This may or may not be something you have to worry about depending on how the data is returned from your database.

adrice727
  • 1,482
  • 12
  • 17
  • This is **crucial**. If the dataloader gets the ids in the order `[4,6,5,7]`, you have to loop through the results and create the arrays in the order of 4, and then 5, and then 6, and then 7. If you just group them by id, and you don't return them in the same order, your cache will be BROKEN – Dan Crews Aug 21 '18 at 01:22
0

You should return an array for each id - array of arrays. You have to convert sql result - flat list with duplicates into 'groupped' arrays of records preserving input ids (amount and order).

xadm
  • 8,219
  • 3
  • 14
  • 25