Build A ToDo App Using GraphQL And Node.js
Part 1: Implementing A Backend Service
Let’s build a ToDo MERN app using GraphQL and Node.js, In this part, we will implement a backend service.
Introduction
In this article, we will create a backend service with GraphQL i.e Apollo Server and MongoDB database.
We will be using the MongoDB
database so make sure you install it locally or you can use free cloud service from MongoDB.
By creating this App, you will learn
- How to design GraphQL schema.
- How to design Queries and Mutations.
- How to build a CRUD app using GraphQL API.
Initial Setup
Create a new project using npm
npm init -y
This command will set the default value in package.json
Once the project is created, install the necessary dependencies
npm i apollo-server mongoose graphql dotenv
Creating Initial Files
Create a new file with the name server.js
in the root directory of the project.
Don’t forget to add .env
file too.
In this file, we are setting up our server. For now, we using dummy data for testing purposes.
Here, we defined GraphQL Schema and resolvers.
Every GraphQL server uses a schema to define the structure of data that clients can query.
Resolvers tell the GraphQL Server to fetch data associated with a particular type.
Note: Currently we using a dummy schema.
Let’s run and check, to run this file node server.js
Setting up the database
Here process.env.DATABASE_URL
holds actual database server address in .env
the file.
Design Mongoose Model
Design GraphQL schema for ToDo
A type
defines the queriable fields for every ToDo in our database and input
defines the input structure for the data.
Mutation
types define the entry point for a write operation.
GraphQL supports scalar type out of the box: ID
, String
, Boolean
, Int
and Float
.
Queries and Mutation accept arguments of type scalar and custom.
Create resolvers
Resolvers will accept arguments from the client.
Running app
Run app using node server.js
As you can see here. Documentation created for you in IDE.