GraphQL server for Swasthya AI Placement Assignment
http://18.206.209.243:4000/graphql
Visit the React Client Repository
Run the MYSQL script dump_2329_22052021.sql
on MySQL Workbench
Steps to install and run GraphQL and React Servers:
- clone the repository (
git clone https://github.com/Rajrahane/Swasthya-AI-Placement-GraphQL-Server.git
) - run
npm install
- change the username,password for GraphQL dbConnection/dbConnection.js
- run
npm start
- view the GraphQL server on http://localhost:4000/graphql
- view the React Server on http://localhost:3000/
- User Table: id,first_name,last_name,mobile_number,email_address
- Blog Table: id,name
- Comment Table: id,message,user_id,blog_id
- Friend Table: id,user_id,friend_id
Friend table adds a given friend to the user's friendList.
React UI:
/users Endpoint: User can Register, Search, View Profile
/blogs Endpoint: A new Blog can be created.Search Blog based on ID.
Blog Profile shows blog name and lists the comments.
Summary
- Create a DB schema in MYSQL for the tables.
- Create a GraphQL Schema to fetch data, create new User and Blog, post a new Comment.
- Create getFriends RootQuery in GraphQL to fetch Nth Level Friend of a User
- Create a React Frontend for Forms, Search, Profile.
Solution for Nth Level Friend List:
Create a Friends graph and make a BFS recursive call until Nth level.
Consider input as (user_id:int,givenLevel:int) returns list(users) Initialize an empty Set of VisitedUsers Initialize an empty queue.Push (user_id) into it Initialize currentLevel=0 while(q is not empty and currentLevel lessthan givenLevel){ init new empty queue q2 while(q not empty){ pop each user. if(user not visited){ mark as visited in VisitedUsers fetch all friends ids from Friends table in Database foreach(friend){ if(friend not visited){ push friend to queue q2 } } } } currentLevel++ push all elements in q2 to q, i.e. q=q2 } if(currentLevel less than givenLevel){ //no nth level depth possible, too few levels return empty list } return to_list(q)
Sample Database Data:
1. 7 users are created
2. 2 blogs are created
3. 1st blog has 2 comments added
4. To mock friend relations, 6 relations are created {1,2,3}, {1,4}, {4,6}, {4,5}
Sample GraphQL Query for getFriends at any level:
{ getFriends(input:{ user_id:5 level:3 }){ id last_name } }Expected Output: Users 2 and 3
Explanation: User 5 has 1st level friend- User 4
User 5 has 2nd level friends- User 1,6 via User 4
User 5 has 3rd level friends- User 2,3 via User 1
Sample Query for edge case (level too high or level<=0)
{ getFriends(input:{ user_id:5 level:4 }){ id last_name } }Expected Ouput: null list
Explanation: User 5 has friends upto level 3 only
Features of GraphQL Repository
- Uses Express NPM Package for Server Setup
- Uses GraphQL to create Schema and Query, Mutation operations
- Uses Sequelize a Promise based Node.js ORM
- Uses ApolloClient to connect to GraphQL Server
- Uses React Router for creating Routes Hierarchy
- Uses React Bootstrap for Bootstrapping
Improvements:
- DFS can be used instead of BFS if some(say 10) and not all users are to be fetched.
- Graph DB - Neo4j can be used instead of MySQL, since a friends graph is formed
- Pagination can be used to fetch Comments from GraphQL.
- GraphQL caching can be used for query caching
React JS UI Snaps
EC2 Deployment
- Launch EC2 using Amazon AWS console.Open ports 4000(GraphQL),3000(React),22(SSH)
- SSH into it. install git `sudo yum install git -y`
- install node using nvm
- install pm2 Daemon Process Manager. `npm install pm2 -g`
- git pull both repositories(React and GraphQL)
- Install Docker on EC2
-
Install docker-compose(linux)
`sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose`
`sudo chmod +x /usr/local/bin/docker-compose`
`docker-compose --version` - Deploy DB using Docker:In /deployment folder: `cd deployment`, `docker-compose up -d` This launches a mysql db and connects a persistent docker volume to it
- Deploy GraphQL using pm2:in graphql repository: `pm2 start --name swasthya-ai-graphql-server npm -- start`
- Deploy React using pm2:in react repository:change graphql endpoint in src/index.js to http://18.206.209.243:4000/graphql then `pm2 start --name swasthya-ai-react-server npm -- start`