title: MongoError: Topology is closed, please connect
body: <p>I'm a front-end dev trying to expand my horizons on a new Next project, learning Node, Mongo, and the server side of GraphQL for the first time. Apollo strikes me as the easiest way to jump in, as I've already used client-side Apollo on previous projects.</p>
  
  <p>I've been following the <a href="https://www.apollographql.com/docs/apollo-server/data/data-sources/" rel="noreferrer">official docs</a>, where I learned of <a href="https://github.com/GraphQLGuide/apollo-datasource-mongodb/" rel="noreferrer">apollo-datasource-mongodb</a> (seemingly the best way to plug my Apollo Server straight into a local Mongo database. Unfortunately there don't seem to be any example repos of this package in action for me to cheat off of so I've been left to muddle through.</p>
  
  <p>I have mongo running locally via <code>mongod</code> and I can perform successful <code>find()</code> queries through the mongo shell, so I know the database itself is in good shape and contains nearly <em>600,000 records</em> (I'm working with a rather large dataset).</p>
  
  <p>I also have access to the Apollo Playground at <code>localhost:4000</code> so I know the server is starting properly and connecting to the database (with appropriate Schema tips/errors I've since managed to resolve).</p>
  
  <p>Here's the query I'm using in the Playground:</p>
  
  <pre><code>{
    item(id: 22298006) {
      title
    }
  }
  </code></pre>
  
  <p>and here's what I'm getting back in response:</p>
  
  <pre><code>{
    "errors": [
      {
        "message": "Topology is closed, please connect",
        "locations": [
          {
            "line": 2,
            "column": 3
          }
        ],
        "path": [
          "item"
        ],
        "extensions": {
          "code": "INTERNAL_SERVER_ERROR",
          "exception": {
            "name": "MongoError",
            "stacktrace": [
              "MongoError: Topology is closed, please connect",
  
              ...
  
            ]
          }
        }
      }
    ],
    "data": {
      "item": null
    }
  }
  </code></pre>
  
  <p>I've attached my server file below. I have a suspicion that this might be some kind of timeout error, like it takes to long to comb through all 600k records to find the one with the ID I provided? When I remove <code>useUnifiedTopology: true</code> from the MongoClient definition I get a different error:</p>
  
  <pre><code>UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  </code></pre>
  
  <p>but I'm not using async or promises myself. Which has me thinking — should I be? Could I somehow hold up the process while I wait for the return from <code>findOneById()</code> (if that is, indeed, the problem)?</p>
  
  <p>As an aside, I've seen at least one example codebase where MongoClient included within itself a Server declaration (also from the <code>'mongodb'</code> npm package). Would implementing something like that save me from having to block up a terminal window with <code>mongod</code> every time I want to work on my project?</p>
  
  <p>Thank you so much for your time! If I can get this working I'll definitely do a full writeup on Medium or something to pave the way for others looking to pair MongoClient with ApolloServer for a quick and easy API.</p>
  
  <p><strong>index.js</strong></p>
  
  <pre><code>const { MongoClient } = require('mongodb');
  const assert = require('assert');
  const { ApolloServer, gql } = require('apollo-server');
  const { MongoDataSource } = require('apollo-datasource-mongodb');
  
  
  const client = new MongoClient('mongodb://localhost:27017/projectdb', { useNewUrlParser: true, useUnifiedTopology: true }, (err) =&gt; {
    err &amp;&amp; console.log(err);
  });
  
  client.connect((err) =&gt; {
    assert.equal(null, err);
    client.close();
  });
  
  const db = client.db();
  
  class Items extends MongoDataSource {
    getItem(id) {
      return this.findOneById(id);
    }
  }
  
  const typeDefs = gql`
    type Item {
      id: Int!
      title: String!
    }
    type Query {
      item(id: Int!): Item
    }
  `;
  
  const resolvers = {
    Query: {
      item: (_, { id }, { dataSources }) =&gt; dataSources.items.getItem(id),
    }
  }
  
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    dataSources: () =&gt; ({
      items: new Items(db.collection('items')),
    }),
  });
  
  server.listen().then(({ url }) =&gt; {
    console.log(`Server ready at ${ url }`);
  });
  </code></pre>