Creating Mock API using Mirage in a React application

Creating Mock API using Mirage in a React application

Creating Mock API using Mirage in a React application

Creating independent front-ends for any application leads to easily maintainable and flexible code but, for creating such independent front-ends, one has to mock the complete API so that the front-end has data and handles all the requests. The process of mocking the API and data can get tedious. Using API mocking libraries can help reduce the time of creating a full-fledged front-end that does not depend on any back-end services.

One such API mocking library is Mirage. In this article, we are going to use Mirage and create a full-fledged React application that does not rely on any back-end service. We are going to create a REST API for the following application:

Image of the application we are going to create

Image of the application we are going to create

It is a book store application where we can add books along with the genre of the books. It also displays our stored book list. The book list is displayed based on two categories, one is the complete list of books stored, the other is the list of books based on genre.

I have provided the link to the starter code which includes all the basic setup that is required for this application:

After cloning or downloading the starter-code, run npm install in the terminal to install all the dependencies. After installing the dependencies, run the application using npm startcommand. This is how the application look with the starter code:

Image of the application with the starter code

Image of the application with the starter code

Try clicking on the show all books button, you should see an error in the console, Mirage error. This is because our application is currently sending HTTP requests to an endpoint which we haven’t coded yet. So let’s start building the API,

We have to install Mirage to our application first, so run the following command to install mirage:

Next, inside the components folder, create a file named server.js and add the following code inside the file:

The createServer( ) function creates the server for us. All the routes,models and mock data will go inside this function.  If you want to use the Mirage server only during the development phase, inside the index.js file, add the following code:

By writing the above code, the mirage server will run only in the development phase.

Now that we have completed setting up the Mirage server, let’s move onto creating models for the API. We are going to create two models, one for storing books and the other for storing genres.

Inside the createServer function, add the following code:

The models property inside the createServer function specifies the number of Models this server consists of. When using POST requests, Mirage uses it’s in-memory database to store our application data.

**Note– Don’t forget to import Model from miragejs.

Now that we have created models, let’s move on to creating endpoints for these models. For this application we are going to use the following endpoints:

Inside the createServer function, add another property (a method in this case) called routes( ). This method will contain all the endpoints required for the application.

All the endpoints should be written inside the routes function. Let’s add the first endpoint for creating a book:

Just like creating endpoints for REST API, the above POST request takes in two parameters, first one is the name of the endpoint and the second is a callback function. The callback function has two parameters: schema (for accessing the data layer of Mirage) and request (to access the request body and params). We can access the book Model using schema.books, for creating new data we use the create method.

Mirage automatically creates documents with incrementing ids so we don’t have to worry about each document having a unique id. For example, consider adding 3 books to the database, this is how they will be represented:

Now run the application again, try adding a new book by clicking the add book button. You should see 201 status code for the Mirage request inside the console window. Next, for getting the list of all books stored along with deleting a specific book add the following endpoints:

The above two endpoints are pretty straight forward, the GET request endpoint returns the complete list of books from the in-memory database using the all( ) method. Then, the DELETE request endpoint is a dynamic endpoint, which takes an id as the parameter, inside the callback function,we use the destroy( ) method to delete the document from the database.

To check whether the application is working, try adding a new book and then click the show all books button, you should see the list of books appearing inside the books section:

GIF displaying the add-book feature

GIF displaying the add-book feature

The cover page of all the books will be a static image which we are loading from the images folder. You can go ahead and change it. As you can see we are now able to add a new book to the Mirage database. Next, for checking the DELETE endpoint, try deleting the a book from the list by clicking the delete button on the book item. After clicking the book item, click on the show all books button again you should no longer see the book you deleted.

There are two other end points that we need to add to our server, the GET all genres endpoint and the GET all books of specific genre endpoint:

****We are going to add the GET all books of specific genre end-point when we discuss relational databases.

So right now, our application works and we are able to add/get/delete books from the Mirage database, the point here to notice is that every time we reload the application, we receive an empty list of books. This is because the Mirage server reruns when the application gets reloaded and therefore the list is empty.What if we want to load some initial mock data so that our book list is not empty when we start the application? For doing so, Mirage provides a method called seeds inside of which we add all the initial mock data which can be used in the application.

Other than loading initial books, if you observe the application, when clicking on the Group by Genre button, we don’t see any list appearing. The list contains all the genres that have been created in the database but, we have not added any genre to the database yet.

So let’s add some initial books and genres to the database. Add the following method inside the createServer function:

As one can see in the code above, using the server.create method, we create both initial books and genres. Now when you run the application and click the show all books button, you should see all the 4 books appearing in the list. Along with this, you can also see the list of genres by clicking the group by genre button:

Group by Genre list gets displayed

Group by Genre list gets displayed

So we have completed the basic operations that are performed in the back-end but, we can see that nothing really happens when you click on one of the items inside the Group by Genre list. This is where relational data comes into picture and we’re going to learn that next.

We have created models that are not related to each other. What if we want the models to have some kind of relationship? Mirage provides a way to add relationships between models. Let’s take a look at how that is possible.

We will create a one-to-many relationship between the book and genre model i.e each genre can be related to many books and every book is related to one genre. This way we are creating a one-to-many relationship. Let’s write code for that. Inside the models property of the createServer function, make the following changes:

In the above code, by adding belongsTo and hasMany methods, we are specifying the server that the book and genre models maintain a one-to-many relationship. Now the server knows that book.genre and genre.books properties are relationships between the models.

**Note- Don’t forget to import belongsTo and hasMany methods from ‘miragejs’

Now that we have specified relationships, let’s add some initial mock data:

Until now, we have created books only using the name property but as one can see in the above code, we are now creating new books having the genre property attached to it. Mirage understands that this property is a relationship and hence all the books from now will be saved with the following properties in the database:

We can now add the end-point for displaying all the books related to a particular genre:

In the above end-point we are first searching for the genre with the particular id and then finding all books related to that genre. Now, try clicking on one of the items inside the Group by Genre list you should the specific book related to that genre appear in the books section:

GIF displaying the group-by-genre feature

GIF displaying the group-by-genre feature

So there you go, we have created an API which loads initial data, contains endpoints, performs most of the CRUD operations and also contains models. Did you see how easy it was to create a fully independent front-end application? Go ahead and try creating some awesome independent front-ends.

For the live demo, checkout the link below:

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Build Real-World React Native App #9 : Implementing Remove Ads Feature

One of the simplest ways to make money from apps is through Advertisements. Hence, we have a popular ads network for mobile that is Admob. There are ...

Build Real-World React Native App #8 : implement Dark mode

In this chapter, we are going to implement a new feature in our app. The feature is called dark mode which is very prevalent in the app nowadays. The ...

How to use the redux dev tools to speed up development and debugging

Redux dev tools are development tools used to debug Redux-based applications. In this article, we'll learn more about how this tool aids the ...


Aaron Adams August 13, 2020 at 10:14 pm

One problem I found with Mirage is there’s no built in pagination support.

Vivek Bisht August 14, 2020 at 12:34 pm

Yes, I agree. In general, the basic setup and handling the endpoints gets done very easily with Mirage and not to forget, the data layer of Mirage. Something like “json-server” is quite useful when you are looking for pagination support out of the box.

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy