title: Connect Apollo with mongodb
body: <p>I want to connect my Apollo server with my mongoDB. I know there are many examples out there, but I get stuck at the async part and did not found a solution or example for that (that's strange, am I completly wrong?)</p>
  
  <p>I started with the example from next.js <a href="https://github.com/zeit/next.js/tree/master/examples/api-routes-apollo-server-and-client" rel="nofollow noreferrer">https://github.com/zeit/next.js/tree/master/examples/api-routes-apollo-server-and-client</a> .
  But the mongodb integration is missing. </p>
  
  <h1>My code</h1>
  
  <p><strong>pages/api/graphql.js</strong> </p>
  
  <pre class="lang-js prettyprint-override"><code>    import {ApolloServer} from 'apollo-server-micro';
      import {schema} from '../../apollo/schema';
  
      const apolloServer = new ApolloServer({schema});
  
      export const config = {
          api: {
              bodyParser: false
          }
      };
  
      export default apolloServer.createHandler({path: '/api/graphql'});
  </code></pre>
  
  <p><strong>apollo/schema.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    import {makeExecutableSchema} from 'graphql-tools';
      import {typeDefs} from './type-defs';
      import {resolvers} from './resolvers';
  
      export const schema = makeExecutableSchema({
          typeDefs,
          resolvers
      });
  </code></pre>
  
  <p><strong>apollo/resolvers.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    const Items = require('./connector').Items;
      export const resolvers = {
          Query: {
              item: async (_parent, args) =&gt; {
                  const {id} = args;
                  const item = await Items.findOne(objectId(id));
                  return item;
              },
              ...
          }
      }
  </code></pre>
  
  <p><strong>apollo/connector.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    require('dotenv').config();
      const MongoClient = require('mongodb').MongoClient;
  
      const password = process.env.MONGO_PASSWORD;
      const username = process.env.MONGO_USER;
      const uri = `mongodb+srv://${username}:${password}@example.com`;
  
      const client = await MongoClient.connect(uri);
      const db = await client.db('databaseName')
      const Items = db.collection('items')
  
      module.exports = {Items}
  </code></pre>
  
  <p>So the problem is the <code>await</code> in <code>connector.js</code>. I have no idea how to call this in an async function or how to provide the MongoClient on an other way to the resolver. If I just remove the <code>await</code>, it  returns – obviously – an pending promise and can't call the function <code>.db('databaseName')</code> on it.</p>