Sitemap
We’ve moved to freeCodeCamp.org/news

We’ve moved to https://freecodecamp.org/news and publish tons of tutorials each week. See you there.

How to set up a GraphQL Server using Node.js, Express & MongoDB

6 min readNov 5, 2018

--

Why GraphQL?

Getting started

npm init -y 
npm install @babel/cli @babel/core @babel/preset-env body-parser concurrently cors express express-graphql graphql graphql-tools merge-graphql-schemas mongoose nodemon
npm install --save-dev @babel/node 

Babel

Server

MLab free option to use in our MongoDB Server.
Our database created with MLab.

Express

The GraphiQL playground.

MongoDB and Schema

GraphQL

Queries

Mutations

Modularizing our schema

Playing with our queries and mutations

Create user

mutation {
addUser(id: "1", name: "Dan Abramov", email: "dan@dan.com") {
id
name
email
}
}
The data object that GraphiQL Playground returned for us.
In our first mutation, we created a user.

Delete user

mutation {
deleteUser(id: "1", name: "Dan Abramov", email: "dan@dan.com") {
id
name
email
}
}

Get all users

query {
users {
id
name
email
}
}
All our users will be returned.

Get a specific user

query {
user(id: "2"){
id
name
email
}
}
The exact user that we asked.

And we’re done!

--

--

Responses (12)