apollo-datasource-mongodb
apollo-datasource-mongodb copied to clipboard
Change proposal for over 20x speedup in findManyByIds
Speed up proposal for method findManyByIds.
https://github.com/GraphQLGuide/apollo-datasource-mongodb/blob/08e1c076739867a9e73a707adc66767a7e8525d7/src/cache.js#L173
As-Is right now, it's opening a lot of findOneById with a promise.all, but it's really inefficient once you have over 100 keys and it's slowing the UI.
While I don't exactly know how to implement this in the library exactly, the solution will look similar to this at the mongo level:
this.collection.find({ _id: { $in: arrIds } })
This will result in a single call to the server-side, speeding up for bigger queries.
@gamebak In the end Dataloader only makes a single request with all the ids passed
It doesn't, I've tested this, unless I'm missing something.
It's certainly the intent of the DataLoader function to use $in:
https://github.com/GraphQLGuide/apollo-datasource-mongodb/blob/2e048ce2b6d9e1983ab6150ffebb9c886c367b05/src/cache.js#L112
If it's not working, would be happy to look at a PR that fixes it.