Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: Topology is closed, please connect

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.

I've been following the official docs, where I learned of apollo-datasource-mongodb (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.

I have mongo running locally via mongod and I can perform successful find() queries through the mongo shell, so I know the database itself is in good shape and contains nearly 600,000 records (I'm working with a rather large dataset).

I also have access to the Apollo Playground at localhost:4000 so I know the server is starting properly and connecting to the database (with appropriate Schema tips/errors I've since managed to resolve).

Here's the query I'm using in the Playground:

{
  item(id: 22298006) {
    title
  }
}

and here's what I'm getting back in response:

{
  "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
  }
}

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 useUnifiedTopology: true from the MongoClient definition I get a different error:

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)

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 findOneById() (if that is, indeed, the problem)?

As an aside, I've seen at least one example codebase where MongoClient included within itself a Server declaration (also from the 'mongodb' npm package). Would implementing something like that save me from having to block up a terminal window with mongod every time I want to work on my project?

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.

index.js

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) => {
  err && console.log(err);
});

client.connect((err) => {
  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 }) => dataSources.items.getItem(id),
  }
}

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => ({
    items: new Items(db.collection('items')),
  }),
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${ url }`);
});
like image 571
Teagan Atwater Avatar asked Oct 15 '22 07:10

Teagan Atwater


1 Answers

Actually there is one more thing that may be causing this as I also had the same exact error "Topology Closed, please connect".

The thing is if you have a dynamic IP address then in MongoDB atlas you should try allowing all IP addresses.

Add the IP address: 0.0.0.0/0 All my problems were solved after whitelisting this IP address which allows all.

Image of dynamic IP 0.0.0.0/0:

enter image description here

like image 155
Baraja Swargiary Avatar answered Nov 02 '22 12:11

Baraja Swargiary