cvburgess/SQLDataSource

Create new DataLoader per request

lorensr opened this issue · 3 comments

https://github.com/cvburgess/SQLDataSource/blob/master/SQLCache.js#L7

Is this possible with the current data source API?

It's recommended both for security and memory usage (the Map used grows until DataLoader is released):

https://github.com/graphql/dataloader#creating-a-new-dataloader-per-request

Thank you for the issue and links!

This is a great idea, and to be honest, a silly oversight.

If you'd like to open a PR please do, otherwise, I will add it to my list but it may be a bit before I can tackle it.

Nevermind—it looks like the dataSources function below is called once per request.

const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({
    db: new MyDatabase()
  })
});

and each new SQLDataSource creates a new SQLCache

Phew, that was my understanding of DataSources but I know there have been new releases so I might have missed something.

Thank you!