Mock GraphQL Server
Discover how to create a mock JSON GraphQL server on our blog. Find easy-to-follow tutorials and tips to improve your testing and development process using simulated environments.

Blog post by Mohit kandhari - Published at 6/23/2024, 9:11:54 AM
A fake JSON server is a useful tool for testing front-end applications without needing to set up a real backend. One popular tool for this purpose is json-server. Here's a guide on how to set it up:
Setting Up a Fake JSON Server Using json-server
Prerequisites:
- Node.js installed on your machine.
Step-by-Step Guide:
1. Install json-server:
Open your terminal or command prompt and run the following command to install json-server globally: npm install -g json-server.
2. Create a JSON file:
Create a file named db.json in your project directory. This file will act as your fake database. Here is an example of what db.json might look like:

3. Start the server:
Run the following command in your terminal: json-server --watch db.json
This command starts a JSON server at http://localhost:3000 by default and watches for any changes in db.json.
4. Access the server:
- To get all posts: GET http://localhost:3000/posts
- To get a specific post: GET http://localhost:3000/posts/1
- To add a new post: POST http://localhost:3000/posts
- To update a post: PUT http://localhost:3000/posts/1
- To delete a post: DELETE http://localhost:3000/posts/1
You can use any REST client (like Postman) or your front-end application to interact with the fake JSON server.
2. Set Up GraphQL Server Using Apollo Server
To convert your JSON API to a GraphQL API, you'll use Apollo Server. Follow these steps:
- Install Apollo Server and GraphQL: npm install apollo-server graphql axios.
- Create a graphql-server.js file:

Run the GraphQL server: node graphql-server.js.
Testing the GraphQL Server
With your GraphQL server running, you can test it using GraphQL Playground (which Apollo Server provides by default) or using tools like Postman, Insomnia, or cURL.
- Open GraphQL Playground: Navigate to the URL provided by Apollo Server (typically http://localhost:4000).
- Run Queries: To get all posts:

Setup: https://github.com/cwmohit/Mock-GraphQl-Server