Log in to GraphQL Editor
Installing MongoDB for GraphQL

Artur Czemiel


Installing MongoDB for GraphQL

GraphQL is a query language for APIs that was originally built by Facebook. Its biggest advantage is making a lot easier to get the data you actually need from a query. Today we will show you how to set up MongoDB with GraphQL.

Install MongoDB with Homebrew

First you need to install a Homebrew. To do it go to Visual Code Studio and run this line from Brew.sh:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now you should have your Homebrew installed. If you had it already installed, we suggest to do a brew update before proceeding, then you can install MongoDB:

$ brew update
$ brew install mongo

What you need to do now is to go to a root directory, create the directory for the database and set its permissions, then you can star Mongo:

$ sudo mkdir -p /data/db
$ sudo chmod 777 /data/db
$ mongod

Et voila! We have it installed and running.


Mongoose provides a straight-forward, schema-based solution to model your application data. It includes:

  • built-in type casting,
  • validation,
  • query building,
  • business logic hooks

So let's install Mongoos. Make sure you are in the right directory and use $npm install:

$ npm install --save mongoose

Connecting Mongo

Create a new folder and move your resolvers.js and schema.js files there. Remember to update your index.js file to make sure everything will be correctly imported here. Once we have it done we can create a new file in our fresh folder which we will use to connect Mongo to our databases, let's call it connect.js.

import mongoose from 'mongoose';

mongoose.Promise = global.Promise;
mongoose.connect =('mongodb://localhost/users', {
    useMongoClient: true

When we have it done we can start creating our schema for Mongo with the same elements as our original schema.

const usersSchema = new mongoose.Schema({
        type: String

The next important step is to create a value to our variable with that model inside, passing the schema and export:

const Users = mongoose.model('users', usersSchema)
export {Users};

Now let's create some GraphQL resolvers. So let's go to resolvers.js and start with importing Mongoose and Users into resolvers:

import mongoose from 'mongoose';
import { Users } from './connect';

export const resolvers ={
    Query: {
        getUser: ({id}) => {
            return new User (id, userDatabase[id]);
    Mutation: {
        createUser: (root, { input}) => {
            const newUser = new Users({
                name: input.name,

            newUser.id = newUser._id;

            return new Promise((resolve, object)) =>
                newUser.save((err) => {
                    if (err) reject (err)
                    else resolve(newUser)

In next part we will create nodejs GraphQL server. Stay tuned!


Check out our other blogposts

Elide - Opinionated APIs for web & mobile
Tomek Poniatowicz
Tomek Poniatowicz
Elide - Opinionated APIs for web & mobile
2 min read
almost 4 years ago
GraphQL Tutorial - Schemas and Types Part 1
Robert Matyszewski
Robert Matyszewski
GraphQL Tutorial - Schemas and Types Part 1
7 min read
over 5 years ago
New Ghost 3.0 and how to use it with GraphQL (Gatsby)
Tomek Poniatowicz
Tomek Poniatowicz
New Ghost 3.0 and how to use it with GraphQL (Gatsby)
2 min read
over 4 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor