Connect MongoDB Atlas GraphQL to an On-Premise MongoDB Cluster - Tutorial

To connect MongoDB Atlas GraphQL to an on-premise MongoDB cluster, you can follow these steps:

  1. Set up a VPN connection between the on-premise network and the Atlas VPC to enable network communication.
  2. Ensure that the on-premise cluster has a valid SSL certificate and is accessible from the Atlas VPC.
  3. Create a new MongoDB Atlas cluster in the same region as the GraphQL service.
  4. Enable VPC peering between the new Atlas cluster and the Atlas VPC.
  5. Set up a Data Source for the on-premise cluster in the new Atlas cluster.
  6. Create a GraphQL schema that defines the data that will be exposed from the on-premise cluster through the GraphQL service.
  7. Use the @mongoDataSource directive to define which data source should be used for each GraphQL resolver.
  8. Deploy the GraphQL service to a public endpoint using a service like AWS Lambda or Google Cloud Functions.

Here is an example GraphQL schema with the @mongoDataSource directive:

type User @mongoDataSource(clusterName: "on-premise-cluster") {
  _id: ObjectId!
  name: String!
  email: String!

type Query {
  getUserById(id: ObjectId!): User
  getUsersByName(name: String!): [User]

type Mutation {
  addUser(name: String!, email: String!): User
  updateUser(id: ObjectId!, name: String!, email: String!): User
  deleteUser(id: ObjectId!): User

Note that this is just an example and you will need to customize it according to your on-premise MongoDB cluster’s data model and access patterns.