Building APIs with GraphQL, NodeJs, and Mongoose

Getting Setup

Haider Malik
8 min readApr 8, 2018

First of all, we need to create package.json file to install dependencies. Open your terminal and run this command. It will automatically create a package.json file for you.

npm init --yes

Installing dependencies

I am going to use the express framework to build our APIs. Express provides another express-graphql package to work with GraphQL. We are going to use Mongoose ODM to interact with MongoDB database. graphql-tools allows you to create the graphql schema.

npm install --save express express-graphql graphql-tools mongoose

Installing dev-dependencies

I am going to use ES6 in this article. We need a babel to transpile our ES6 code to ES5. Let me show you how to setup babel in NodeJs project

npm install --save-dev babel-cli babel-preset-env babel-preset-stage-0

Now you need to create a new start script in the package.json file to run the nodejs server. I am going to use nodemon to run my nodejs application. index.js will be the root/entry file in our project.

"start": "nodemon ./index.js --exec babel-node -e js"

Final look at Package.json

{
"name": "gql-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon ./index.js --exec…

--

--