Welcome to Grafbase!

This guide applies to a previous version of the Grafbase Platform and the features discussed have been sunset. If you are looking for our documentation on using Grafbase as your federation platform, click here.

MongoDB Connector

MongoDB Connector

MongoDB is a highly adaptable document database that boasts a vast array of versatile querying and indexing functionalities.

The MongoDB Atlas Data API is an innovative interface built on top of your MongoDB database, tailored to excel in serverless environments, like the edge. Operating over HTTP, this API can be seamlessly integrated using Grafbase to generate a fully functional GraphQL CRUD API.

The MongoDB connector allows you to connect one or more MongoDB Atlas Databases using the Data API to Grafbase.

  1. Create a directory and initialize your Grafbase project by running the following commands:
npx grafbase init
  1. In your project directory, open the grafbase.config.ts file and replace the existing content with the following schema:
import { config, connector, graph } from '@grafbase/sdk' const g = graph.Standalone() const mongo = connector.MongoDB('MongoDB', { apiKey: g.env('MONGODB_API_KEY'), url: g.env('MONGODB_API_URL'), dataSource: g.env('MONGODB_DATASOURCE'), database: g.env('MONGODB_DATABASE'), }) g.datasource(mongo) export default config({ graph: g, })
  1. Also inside the grafbase.config.ts file you will need to define all models for MongoDB collections you already have or want to create:
const address = g .type('Address', { street: g.string(), city: g.string(), country: g.string(), }) .collection('addresses') mongo .model('User', { name: g.string(), email: g.string(), age: g.int().optional(), address: g.ref(address).optional(), metadata: g.json().optional(), }) .collection('users')

Note: Make sure to insert the above before invoking g.datasource(mongo);.

This schema will generate a fully working GraphQL API with queries and mutations to:

  • userCreate — Create a new user
  • userCreateMany — Batch create new users
  • userUpdate — Update an existing user
  • userUpdateMany — Batch update users
  • userDelete — Delete a user
  • userDeleteMany — Batch delete users
  • user — Fetch a single user record
  • userCollection — Fetch multiple users from a collection
  1. Run the local development Grafbase server:
npx grafbase dev
  1. Visit Pathfinder and execute your first GraphQL mutation:
mutation { mongo { userCreate( input: { name: "Jamie Barton" email: "jamie@grafbase.com" address: { street: "49 Featherstone Street" city: "London" country: "United Kingdom" } } ) { insertedId } } }

You should see a response that looks something like this:

{ "data": { "mongo": { "userCreate": { "insertedId": "651aa27120c7f53fa521a1f9" } } } }
  1. Visit Pathfinder and execute your first GraphQL query:
query { mongo { userCollection(first: 100) { edges { node { id name email age address { street city country } metadata } } } } }

You should see a response that looks something like this:

{ "data": { "mongo": { "userCollection": { "edges": [ { "node": { "id": "651aa27120c7f53fa521a1f9", "name": "Jamie Barton", "email": "jamie@grafbase.com", "age": null, "address": { "street": "49 Featherstone Street", "city": "London", "country": "United Kingdom" }, "metadata": null } } ] } } } }
  1. Configure caching rules for queries inside grafbase/grafbase.config.ts by updating the default export:
export default config({ graph: g, cache: { rules: [ { types: ['Query'], maxAge: 60, staleWhileRevalidate: 60, }, ], }, })

This configuration will enable a maxAge of 60 seconds to all queries, and return a stale response after that while updating in the background for the next request.

  1. Build locally

You can now continue to build your GraphQL API using Grafbase locally with the CLI. When you're ready to deploy to production, follow the guide on how to deploy with GitHub.

Get Started

Start building your federated graph now.