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.
- A Grafbase Account to deploy your project to
- A MongoDB Database with Data API enabled
- Create a directory and initialize your Grafbase project by running the following commands:
npx grafbase init
- 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,
})
- 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 useruserCreateMany
— Batch create new usersuserUpdate
— Update an existing useruserUpdateMany
— Batch update usersuserDelete
— Delete a useruserDeleteMany
— Batch delete usersuser
— Fetch a single user recorduserCollection
— Fetch multiple users from a collection
- Run the local development Grafbase server:
npx grafbase dev
- 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"
}
}
}
}
- 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
}
}
]
}
}
}
}
- 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.
- 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.