Build a complete app with React and GraphQL-2

Welcome to part-2 of the series. You can find part-1 here.

This series have been inspired by this youtube tutorial from freecodecamp.

We will first add another type author to schema.js

The changes are marked in bold. The process is almost similar to the one for books type.

One thing to note that we have make the id as GraphQLID. The difference from GraphQLString is that, we can pass a number also in id.

Image for post
Image for post
number in id

The usual string will also work.

Image for post
Image for post
id is string

Next, we will make the relation between the book and author types. Each book have an author, so in book array we add a key authorId, for each object corresponding to the id in authors array.

Also, in BookType we are adding query to get the author by using the parent’s authorId.

Now, we can query in graphiql and get the author details also for a book.

Image for post
Image for post
author details also present

Now, we will do the opposite. Each author have book, in fact each author can have many books. So, we have a new variable type called GraphQLList.

Now, on running our query for authors, we get all his books

Image for post
Image for post
author’s books

Now, what if we want all the books in our database or all authors. It is very simple using GraphQL, as we need to just update our RootQuery with GraphQLList as below in schema.js.

Now, a books query will return all books.

Image for post
Image for post
The books query

An authors query can also be done. We can also include the book.

Image for post
Image for post
authors query

Now, it’s time to store all our data into mongoDB. So, head over to https://mlab.com/ and login.

Then Create New mongoDB deployment.

Image for post
Image for post
Create New mongoDB

Click on the Free plan.

Image for post
Image for post
The free plan

Select a Region and then click Continue.

Image for post
Image for post
Select a Region

Give a name to your database and click continue.

Image for post
Image for post
Database name

In the next page, Submit the order.

Image for post
Image for post
Submit Order

Next, click on your new database.

Image for post
Image for post
The new DB

In Users tab, click on Add database user. It will open a pop-up in which you need to provide a username and password.

Image for post
Image for post
username and password

Now, to connect to our database we need to use the query string highlighted below.

Image for post
Image for post
Query string

Now, open your terminal and stop nodemon and install mongoose.

Image for post
Image for post
install mongoose

Next, we add the code in App.js to connect to our database.

The running nodemon, will show the success message.

Image for post
Image for post
Connected finally

Now, we need to create our mongodb schemas. So, go ahead and create a folder models inside server. Inside it we create two files — book.js and author.js

In author.js add below code to export an authorSchema.

In book.js add below code to export an bookSchema.

Next, head over to schema.js and delete the dummy data and all return statements with dummy data. We will add an mutation, which will be used to insert data in our database.

Next, head over to graphiql to add an author with mutation.

Image for post
Image for post
Author added with mutation

Also, check in your mlab whether that author was added.

Image for post
Image for post
mlab checking

Now, we will add mutation to add a book.

Next, head over to graphiql to add an book with mutation. Notice, that the authorId we have to get from mlab, from the newly created author.

Image for post
Image for post
book mutation

Add some more book and then head over to mlab to verify your additions.

Image for post
Image for post
mlab checking

This concludes part-2 of the series. You can find code till here in the github link.

You can find part-3 here.

Written by

UI Lead- ReactJS, JavaScript and everything in-between.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store