GraphQLGuide / apollo-datasource-mongodb

Apollo data source for MongoDB
MIT License
285 stars 64 forks source link

Change proposal for over 20x speedup in findManyByIds #84

Open gamebak opened 3 years ago

gamebak commented 3 years ago

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.

0xChqrles commented 2 years ago

@gamebak In the end Dataloader only makes a single request with all the ids passed

gamebak commented 2 years ago

It doesn't, I've tested this, unless I'm missing something.

lorensr commented 2 years ago

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.