How to set context of resolvers in apollo?

Hi ! I want to set in context argument a collection so that in resolver I call db.find({}).
For example:

User(_, args, ctx) {
    return ctx.db.find({});

I want to do it because it would be much simpler to change db.

Thank you!

In Apollo server, when you instantiate the original server with a settings object, you can provide a function that returns the context object you’d like.

So depending on how your db client works, you can import it, start a connection pool, etc, then return the client with other things needed in the context function.

The context function can use async/await to make sure it’s avail for use also before called in the resolvers.