apollographql / apollo-server

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.79k stars 2.03k forks source link

How to use Dataloader with Apollo server ? #47

Closed nghiepdev closed 8 years ago

nghiepdev commented 8 years ago

I see great https://youtu.be/UBGzsb2UkeY?t=24m45s use Dataloader. How to use Dataloader with Apollo server? I'm using MongoDB.

Please give me an example.

Tks.

nnance commented 8 years ago

@tronghiep92 there are a couple of projects that are based on Apollo Server and uses Dataloader as well:

https://github.com/apollostack/GitHunt https://github.com/nnance/swapi-apollo

Let me know you need a better example

helfer commented 8 years ago

@tronghiep92 please feel free to re-open the issue, or ask questions on the slack channel if you still need more info.

wmertens commented 8 years ago

@tronghiep92 So basically all you need to do is wrap your item loader with DataLoader and it will automatically group calls, batching them if possible: https://github.com/nnance/swapi-apollo/blob/5e2208db6cb3e91dbb7aa34b531454a57c6a973f/src/connectors/swapi.ts#L10-L16

However, these examples work with data that does not expire, and you may need to implement cache clearing. The documentation at https://github.com/facebook/dataloader/blob/master/README.md is good, I think.