- User
| Resource | Method | Description |
|---|---|---|
| /user/:userId | GET | searching with userid |
| /user | POST | craete an user with name, email, password |
| /user/list/:page?/:size? | GET | get user list for a page with size |
| /user/list/post/:userId/:page?/:size? | GET | get post list for a page with size based on user id |
| /user/list/comment/:userId/:page?/:size? | GET | get comment list for a page with size based on user id |
- Post
| Resource | Method | Description |
|---|---|---|
| /post/:postId | GET | get a post with postid |
| /post | POST | create a post with author, title, content |
| /post | PUT | update a post with postid / author, title, content, update time |
| /post | DELETE | delete a post with postid |
| /post/list/:page?/: size? | GET | get post list with size |
| /post/list/comment/:postId/:page?/:size? | GET | get comment list for a post with postid based page and size |
- Comment
| Resource | Method | Description |
|---|---|---|
| /comment/:commentId | GET | get comment |
| /comment | POST | create comment |
| /comment | PUT | update comment - author, content, time |
| /comment | DELETE | delete comment / |
| URL | How to Use |
|---|---|
| /graphql | Try Queries - query - mutaion |
- MySQL
- Create DB, user for the project
CREATE DATABASE board;
CREATE USER 'class'@'localhost' IDENTIFIED BY '101';
ALTER USER 'class'@'localhost' IDENTIFIED WITH mysql_native_password BY '101';
GRANT ALL PRIVILEGES ON board.* TO 'class'@'localhost';
FLUSH PRIVILEGES;- node.js: v10.15.3
- npm packages
"dependencies": {
"apollo-server-express": "^2.5.0",
"dotenv": "^8.0.0",
"express": "^4.17.0",
"graphql": "^14.3.0",
"moment": "^2.24.0",
"mysql2": "^1.6.5",
"sequelize": "^5.8.6"
},
"devDependencies": {
"bcrypt": "^3.0.6",
"eslint": "^5.16.0",
"faker": "^4.1.0",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
"nodemon": "^1.19.0"- How to start
# git clone
git clone https://github.com/intothedeep/board-graphQL-REST-nodejs.git
# install packages
npm i
# start
nodemon start# REST
|app.js
|--controllers
|----index.js
|----post.js
|----user.js
|----comment.js
|--routes
|----index.js
|----post.js
|----user.js
|----comment.js
|--models
|----index.js
|----Post.js
|----User.js
|----Comment.js
|--daos
# GraphQL
|app.js
|schema.js
|resolvers.js
# misc
|--docs// app.js
// This code block will generate dummy data and insert them into the DB
// set this true if you want to reset DB.
await db.sequelize.sync({ force: true });
// When this block is active, dummy data will be created every time you restart server
// To stop creating dummies, make this code below as comments
await db.sequelize.sync({ force: false });
const userIds = await db.User.findAll().map(user => user.id);
const newUserIds = [
...new Set(
['admin', ...times(10,() => faker.name.firstName())].filter(id => !userIds.includes(id)))
];
await db.User.bulkCreate(
newUserIds.map(id => ({
id,
name: `${id} ${faker.name.lastName()}`
})
)
);
console.log(newUserIds, newUserIds.length);
let posts = await db.Post.bulkCreate(
times(30, () => ({
title: faker.lorem.sentence(),
content: faker.lorem.paragraph(),
userId: newUserIds[random(0, newUserIds.length - 1)]
}))
);
postIds = posts.map(post => post.id);
await db.Comment.bulkCreate(
times(300, () => ({
content: faker.lorem.sentence(),
userId: newUserIds[random(0, newUserIds.length - 1)],
postId: postIds[random(0, 10)]
}))
);