In this tutorial you’ll learn the basic setup required to implement GraphQL queries using NodeJS, Express and MongoDB. Also I will try to explain role of each line of code, for this please refer to the embedded code comments. Code for this tutorial can be found here — https://github.com/utkarshp21/graphql-server-setup

Before moving ahead lets first quickly look at the basic structure of a GraphQl query.

{

books{

id

name

genre

}

}

This query will fetch all books from the database as formatted below

{

"data":{

"books": [{

"id": "324234234",

"name": "Book 1",

"pages": 472

},{

"id": "324234235",

"name": "Book 2",

"pages": 420

}]

}

}

Setting up the project

Make a new project folder and initialise it with npm using:

npm init

2. Install Express middleware, express-graphql and Graphql modules. The express-graphql module allows Express to understand GraphQL and provide a simple way to run a server to provide GraphQL APIs. It basically helps Express to process GraphQL queries and we use it as a middleware on a single route and this route will be used as an endpoint to interact with our Graphql data.

npm install express express-graphql graphql --save

3. Create app.js file and use the code below to create your express server. We setup express route /graphql which will handle all GraphQL requests. Also we pass schema and graphiql:true to the graphqlHTTP. Schema helps express-graphql to map out a graph and setting graphiql to true provides an interface to make GraphQl queries.

app.js

4. Now we need create a GraphQL schema. Create a new file schema.js inside folder schema and define the schema as follows. For now our data source is defined in fakeBookDatabase array, we will use this array to implement fetching data from GraphQL and later add MongoDB database.

Schema serves three purpose:

Defines data on the Graph like object types like BookType Defines relation between these object types Describes how to reach into the graph to interact with the data to retrieve or mutate the data

Please go through the comments in code for further details on specific line.

Run your express server using node app.js and got to http://localhost:3000/graphql? . Now your graphql client should be running.

Make a query :-

{

book(id:"1"){

id

name

pages

}

}

it should return the result as :-

5. Connecting Express server to MongoDB database

Till now we were working on dummy database to add functionality of fetching data from a data source using GraphQL RootQuery, next step will be connecting our application to a database so that we add and understand mutation functionality in GraphQl for creating and updating to our database. Also we will add additional GraphQL schema author and define relationship between author and book schema.

We will be using mongoDB database, I will not go into details of object modeling tool — Mongoose or mongoDB. Setup an account on MLab using the tutorial below.

Install mongoose using command

npm install mongoose --save

In app.js file of your express application add the following code to add Mongoose middleware and connect to the MongoDb database

Replace <dbuser> , <MongoDB URI> and <dbpassword> with your own credentials which you got from following above tutorial. Running the application now should log ‘connected to database’.

6. Define Mongoose Schema and compile them to models

Now we need to create a schema for our mongoDB database. We need two schemas for our database — author and book. Create a new folder models in project folder and paste the following code in respective files — author.js and book.js.

NOTE-This schema is completely different from the GraphQL Schema.

For the author model

models/author.js

2. For the book model

models/book.js

6. Updating GraphQL Schema to fetch data from Mlab Database

In our GraphQL schema we will remove the dummy database, add author model, make changes in RootQuery by adding extra fields books, author, authors and make change to existing field book. Also we will import our mongoose models.

7. Adding GraphQL Mutation

Mutation is very similar to RootQuery, it helps user to add/update to the database. Define the constant Mutation add it as an option while exporting GraphQL schema.

Now you should we able to perform the following queries:-

a) Add author to your MLab database.

b) Add book

c) Get Book with ID

d) Get all books

e) Get all authors

Github link:-