Todo-backendAPI
Todo-backendAPI copied to clipboard
Backend for a To-Do application. This is part of series of medium article that I wrote.
Contents
- About the Project
- Requirements
- Tech Stack and Packages used
- Initial Setup
- Working with docker
- Running Project locally
- Initial Steps
- Running Tests
- Linting Code
- API of application
- Registering User
- Login User
- Profile of User
- Create TODO
- Get all todos
- Updating a Todo
- Deleting a Todo
- Support
About the Project
Just a simple but effective backend for a todo application.
Requirements
- Node.js and npm installed for your OS.
- You need to have docker installed for your OS.
- GraphQL-Playground for testing your API.
Tech Stack and Packages used
- Docker used to contain dependencies and tooling
node>= 8.9.0npm>= 5.5.1TypeScript>= 3.0.1graphqlfor making API queriesmongoDbfor databasewinstonfor logging/monitoring applicationgulpfor solving problem of repetitionbluebirdfor promisesmocha and chaifor testingnycfor code coveragets-nodefor traspiling TypeScript on the server so that it could be used in Node.jstslintfor linting code
Initial Setup
// Cloning the repository
git clone https://github.com/knrt10/Todo-backendAPI.git
// Change directory to project folder
cd Todo-backendAPI/
Working with docker
After following above intital steps, its very easy to access the api using docker. All you need is to do it follow small steps given below.
Changing shared/config.ts
Initial mongoDb is setup for localhost. master branch contains setup for running server using npm so you need to change branch
git checkout docker
After changing that run this command
npm run dockerStart
This will run a script scripts/dockerCompose.sh and create docker image and with help of docker-compose.yml from our project directory it will start server automatically. Now to access the application proceed to API of application.
To Stop Our Docker process type this command in your terminal
npm run dockerStop
Running Project locally
Initial Steps
Follow these simple steps
- Installing dependencies
npm i- This will create a
node_modulesfolder.
- Building the project
- After dependencies are installed run
npm run build - This will build the application and create a
dist/folder
- After dependencies are installed run
- Starting mongodb Server
- You need to start your mongodb server for your OS. For Mac and Unix it is
mongod.
- You need to start your mongodb server for your OS. For Mac and Unix it is
- Running the application
- Start your application by running
npm startornpm run dev
- Start your application by running
- Accessing API of application
- Now to access the application proceed to API of application.
- Stopping the application
- press ctrl + c in your terminal where server is runnning.
- Running docker again.
- If you want to run docker again follow Working with docker.
Running Tests
IMPORTANT:- To run tests your config should be localDatabase: true in shared/config.ts and your local mongoDB server should be running. If so then run simple command
npm run build && npm run coverage
Linting Code
You can lint the code automatically by running this is your terminal
npm run lint
API of application
This is simple API of application. Go to http://localhost:3000/graphql or open GraphQL-Playground and enter above URL for testing your API.
Registering User
mutation registerUser($input: UserInputRegister) {
registerUser(input: $input) {
code
message
data {
token
success
user {
id
createdAt
username
name
password
updatedAt
}
}
}
}
and input
{
"input": {
"username": "knrt10",
"name": "Kautilya",
"password": "test"
}
}
Login User
query loginUser($input: UserInputLogin) {
loginUser(input: $input) {
code,
message,
data {
success
user {
id
name
username
password
}
token
}
}
}
and input
{
"input": {
"username": "knrt10",
"password": "test"
}
}
Profile of User
query profileUser{
profileUser {
code
message
data {
success
token
user {
name
}
}
}
}
and headers
{
"x-access-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVjMjBkNzRiMGJjNTc4MTRmOGE4YjQ5ZSIsImlhdCI6MTU0NTY1NzI5OSwiZXhwIjoxNTQ1NzQwMDk5fQ.wG4i5gvxTG6Ts-6jfQp1ZdDtF6RysMh-WtXQYACBl74"
}
Create TODO
mutation addTodo($input: todoInput) {
addTodo(input: $input) {
code
message
data {
success
todo {
id
postedByid
description
updatedAt
createdAt
name
}
}
}
}
and input
{
"input": {
"title": "knrt10",
"description": "test"
}
}
and also headers
{
"x-access-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVjMjBkNzRiMGJjNTc4MTRmOGE4YjQ5ZSIsImlhdCI6MTU0NTY1NzI5OSwiZXhwIjoxNTQ1NzQwMDk5fQ.wG4i5gvxTG6Ts-6jfQp1ZdDtF6RysMh-WtXQYACBl74"
}
Get all todos
query todoUsers {
todoUsers{
code
message
data {
success
todos {
title
description
}
}
}
}
and also headers
{
"x-access-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVjMjBkNzRiMGJjNTc4MTRmOGE4YjQ5ZSIsImlhdCI6MTU0NTY1NzI5OSwiZXhwIjoxNTQ1NzQwMDk5fQ.wG4i5gvxTG6Ts-6jfQp1ZdDtF6RysMh-WtXQYACBl74"
}
Updating a Todo
mutation updateTodo($input: todoInputUpdate) {
updateTodo(input: $input) {
code
message
data {
success
todo {
id
postedByid
description
updatedAt
createdAt
name
title
}
}
}
}
and input
{
"input": {
"id": "5c2503576c89915ab8ac3572",
"title": "Woooa this is working?",
"description": "Yep."
}
}
and also header
{
"x-access-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVjMjUwMzIwNmM4OTkxNWFiOGFjMzU3MSIsImlhdCI6MTU0NTkyOTUwNCwiZXhwIjoxNTQ2MDEyMzA0fQ.hn4csdGR5w-2yXWVUEmW4wh8U0s5SfWXfClmP0jVgOY"
}
Deleting a Todo
mutation deleteTodo($id: String) {
deleteTodo(id: $id) {
code
message
data {
success
}
}
}
and input
{
"id": "5c20ecda2e62db1ae4d9ff42"
}
and also headers
{
"x-access-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVjMjBkNzRiMGJjNTc4MTRmOGE4YjQ5ZSIsImlhdCI6MTU0NTY1NzI5OSwiZXhwIjoxNTQ1NzQwMDk5fQ.wG4i5gvxTG6Ts-6jfQp1ZdDtF6RysMh-WtXQYACBl74"
}
Support
I wrote this repo by using my free time. A little motivation and support helps me a lot. If you like this nifty hack you can support me by doing any (or all :wink: ) of the following:
