Skip to content

Commit ada4f28

Browse files
committed
Release NodeJS+GraphQL example (using apolloServer)
0 parents  commit ada4f28

29 files changed

+664
-0
lines changed

Diff for: .eslintrc.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"ecmaVersion": "latest",
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
}
13+
}

Diff for: .gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
**/.terraform/*
2+
*.tfstate
3+
*.tfstate.*
4+
crash.log
5+
**/node_modules/*
6+
**/.idea/*
7+
*.map
8+
*.tfvars
9+
package-lock.json
10+
**/.serverless/*
11+
dump.rdb
12+
userAuth.zip
13+
serverless.yml
14+
**/.vscode/*
15+
**/.nyc_output/*
16+
**/coverage/*
17+
env.bat
18+
./serverless_dev_pokers/*
19+
serverless_dev_pokers/
20+
sls_pokers.sh
21+
**/.DS_Store

Diff for: package.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "example-nodejs-graphql",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"dev": "ts-node-dev --inspect --watch -- ./src/index.ts",
8+
"compile": "tsc",
9+
"bundle": "esbuild ./src/index.ts --platform=node --bundle --outfile=./dist/index.js",
10+
"build": "tsc && esbuild ./src/index.ts --platform=node --bundle --outfile=./dist/index.js",
11+
"start": "node --es-module-specifier-resolution=node ./dist/index.js"
12+
},
13+
"author": "pokers",
14+
"license": "ISC",
15+
"dependencies": {
16+
"@graphql-tools/schema": "^8.3.11",
17+
"@graphql-tools/utils": "^8.6.10",
18+
"apollo-server-express": "^3.7.0",
19+
"body-parser-graphql": "^1.1.0",
20+
"compression": "^1.7.4",
21+
"cors": "^2.8.5",
22+
"dotenv": "^16.0.1",
23+
"express": "^4.18.1",
24+
"graphql": "^16.5.0",
25+
"lodash": "^4.17.21",
26+
"morgan": "^1.10.0",
27+
"promise-mysql": "^5.2.0"
28+
},
29+
"devDependencies": {
30+
"@types/compression": "^1.7.2",
31+
"@types/cors": "^2.8.12",
32+
"@types/express": "^4.17.13",
33+
"@types/graphql": "^14.5.0",
34+
"@types/http-errors": "^1.8.2",
35+
"@types/jest": "^27.5.1",
36+
"@types/knex": "^0.16.1",
37+
"@types/lodash": "^4.14.182",
38+
"@types/morgan": "^1.9.3",
39+
"@types/node": "^17.0.33",
40+
"@types/supertest": "^2.0.12",
41+
"@typescript-eslint/eslint-plugin": "^5.23.0",
42+
"@typescript-eslint/parser": "^5.23.0",
43+
"concurrently": "^7.2.0",
44+
"eslint": "^8.15.0",
45+
"eslint-config-prettier": "^8.5.0",
46+
"eslint-plugin-import": "^2.26.0",
47+
"eslint-plugin-prettier": "^4.0.0",
48+
"graphql-import": "^1.0.2",
49+
"graphql-import-node": "0.0.5",
50+
"nodemon": "^2.0.16",
51+
"prettier": "^2.6.2",
52+
"supertest": "^6.2.3",
53+
"ts-jest": "^28.0.2"
54+
}
55+
}

Diff for: src/config/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { MySqlConfig } from "../types"
2+
const config = {
3+
port: 8080
4+
}
5+
export default config
6+
7+
export const MySql:MySqlConfig = {
8+
host: process.env.GO_MYSQL_URL,
9+
port: Number(process.env.DB_RDS_PORT),
10+
user: process.env.DB_RDS_USER,
11+
password: process.env.DB_RDS_PASS,
12+
database: process.env.DB_RDS_DATABASE,
13+
}

Diff for: src/controller/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { UserController } from './user'
2+
3+
export { UserController }

Diff for: src/controller/user/UserController.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {Request, Response} from 'express'
2+
import { UserQuerySet } from '../../service/mysql'
3+
4+
const getUserList = async (req:Request, res:Response) => {
5+
try{
6+
const result = await UserQuerySet.getUserList(10, 225)
7+
console.log('Query result : ', result)
8+
9+
res.status(200).send({
10+
success: true,
11+
data: result
12+
})
13+
}catch(e){
14+
console.error(e)
15+
res.status(500).send({message : 'Failed to get user list'})
16+
}
17+
}
18+
19+
const getUser = async (req:Request, res:Response)=>{
20+
try{
21+
const userId:number = parseInt(req.params.id)
22+
const result = await UserQuerySet.getUser(userId)
23+
console.log('Query result : ', result)
24+
25+
res.status(200).send({
26+
success: true,
27+
data: result[0]
28+
})
29+
}catch(e){
30+
console.error(e)
31+
res.status(500).send({message : 'Failed to get user'})
32+
}
33+
}
34+
35+
const postUser = async (req:Request, res:Response)=>{
36+
try{
37+
res.status(201).send({message: "success"})
38+
}catch(e){
39+
console.error(e)
40+
res.status(500).send({message : 'Failed to get user'})
41+
}
42+
}
43+
44+
45+
46+
export const UserController = {
47+
getUserList,
48+
getUser,
49+
postUser,
50+
}

Diff for: src/controller/user/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { UserController } from "./UserController";
2+
3+
export { UserController}

Diff for: src/gqlschema/index.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'graphql-import-node'
2+
import { merge } from 'lodash'
3+
import * as userTypeDefs from './user.graphql'
4+
import * as orderTypeDefs from './ordering.graphql'
5+
import { makeExecutableSchema } from '@graphql-tools/schema'
6+
import { UserResolver, OrderingResolver } from '../resolver/'
7+
import { GraphQLSchema } from 'graphql'
8+
9+
const schema: GraphQLSchema = makeExecutableSchema({
10+
typeDefs: [userTypeDefs, orderTypeDefs],
11+
resolvers : merge(UserResolver, OrderingResolver),
12+
});
13+
14+
export default schema;

Diff for: src/gqlschema/ordering.graphql

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
'id','int(11) unsigned','NO','PRI',NULL,'auto_increment'
3+
'status_id','int(11) unsigned','NO','MUL',NULL,''
4+
'user_id','int(11) unsigned','NO','MUL',NULL,''
5+
'scooter_id','varchar(20)','NO','MUL',NULL,''
6+
'billing_info_id','int(11) unsigned','NO','',NULL,''
7+
'voucher_id','int(11) unsigned','YES','',NULL,''
8+
'reservation_id','int(11) unsigned','YES','MUL',NULL,''
9+
'start_time','datetime','YES','MUL',NULL,''
10+
'end_time','datetime','YES','MUL',NULL,''
11+
'price','decimal(10,2)','YES','',NULL,''
12+
'description','varchar(255)','YES','',NULL,''
13+
"""
14+
15+
type Ordering {
16+
id: Int
17+
status_id: Int
18+
user_id: Int
19+
scooter_id: String
20+
billing_info_id: Int
21+
voucher_id: Int
22+
reservation_id: Int
23+
start_time: String
24+
end_time: String
25+
price: Float
26+
description: String
27+
}
28+
29+
type Query {
30+
ordering(userId:Int!, count:Int, startDt:String): [ Ordering ]
31+
}

Diff for: src/gqlschema/user.graphql

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
'id','int(11) unsigned','NO','PRI',NULL,'auto_increment'
3+
'cognito_username','varchar(255)','NO','MUL',NULL,''
4+
'username','varchar(100)','NO','UNI',NULL,''
5+
'password','varchar(255)','YES','',NULL,''
6+
'name','varchar(30)','YES','',NULL,''
7+
'market','varchar(10)','YES','',NULL,''
8+
'email','varchar(100)','YES','',NULL,''
9+
'birthday','date','YES','',NULL,''
10+
'gender','enum(\'F\',\'M\')','YES','',NULL,''
11+
'phone_number','varchar(50)','NO','MUL',NULL,''
12+
'used_count','int(11)','YES','',NULL,''
13+
'status_id','int(11) unsigned','NO','MUL',NULL,''
14+
'ordering_id','int(11) unsigned','YES','MUL',NULL,''
15+
'description','varchar(255)','YES','',NULL,''
16+
"""
17+
type User {
18+
id: Int!
19+
cognito_username: String!
20+
username: String
21+
password: String
22+
name: String
23+
market: String
24+
email: String
25+
birthday: String
26+
gender: String
27+
phone_number: String!
28+
used_count: Int
29+
status_id: Int
30+
ordering_id: Int
31+
description: String
32+
ordering: [Ordering]
33+
}
34+
35+
type Query {
36+
users(count:Int, offset: Int): [ User ]
37+
user(userId:Int!, count: Int, startDt: String): User
38+
}
39+
40+
type Mutation {
41+
addUser(cognito_username: String): Boolean
42+
}

Diff for: src/handler

Whitespace-only changes.

Diff for: src/index.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import express, { Express } from 'express'
2+
import morgan from 'morgan'
3+
import cors from 'cors'
4+
import bodyParser from 'body-parser'
5+
import config from './config'
6+
import {initRoutes} from './route'
7+
import { bodyParserGraphQL } from 'body-parser-graphql'
8+
import compression from 'compression'
9+
10+
11+
const app:Express = express()
12+
13+
app.set('port', config.port)
14+
15+
const corsOptions = {
16+
origin: '*',
17+
}
18+
app.use(cors(corsOptions))
19+
20+
app.use(morgan('dev'))
21+
22+
app.use(bodyParser.json())
23+
app.use(bodyParser.urlencoded({extended: true}))
24+
app.use(bodyParserGraphQL())
25+
app.use(compression())
26+
27+
initRoutes(app)
28+
29+
app.listen(config.port, () =>{
30+
console.log("Start server...\n")
31+
})
32+

Diff for: src/resolver/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { UserResolver } from "./user"
2+
import { OrderingResolver } from "./ordering"
3+
4+
export { UserResolver, OrderingResolver}

Diff for: src/resolver/ordering/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { OrderingResolver } from "./orderingResolver"
2+
3+
export { OrderingResolver }

Diff for: src/resolver/ordering/orderingResolver.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import { GraphQLResolveInfo, ObjectTypeDefinitionNode } from 'graphql'
3+
import { IResolvers } from '@graphql-tools/utils'
4+
import { UserQuerySet, OrderingQuerySet } from '../../service/mysql'
5+
6+
7+
let getDateBefore = (days: number):string=>{
8+
function pad(n:number) { return n<10 ? "0"+n : n }
9+
const now = new Date()
10+
now.setTime(now.getTime() + (9*60*60*1000))
11+
now.setTime(now.getTime() - (days * 24 * 60*60*1000))
12+
return now.getFullYear()+"-"+
13+
pad(now.getMonth()+1)+"-"+
14+
pad(now.getDate())+" "+
15+
pad(now.getHours())+":"+
16+
pad(now.getMinutes())+":"+
17+
pad(now.getSeconds())
18+
}
19+
20+
let queryOrdering = async (parent:any, args:any, context:any, info:GraphQLResolveInfo) => {
21+
try{
22+
// console.log('Params : ', parent, JSON.stringify(args), JSON.stringify(context), JSON.stringify(info))
23+
console.log('Args : ', JSON.stringify(args))
24+
const count:number = (args || ({} as any)).count? args.count:10
25+
const startDt:string = (args || ({} as any)).startDt? args.startDt:getDateBefore(10)
26+
const result:Array<any> = await OrderingQuerySet.getOrderingByUser(args.userId, count, startDt, "DESC")
27+
return result
28+
}catch(e){
29+
console.log(e)
30+
throw e
31+
}
32+
}
33+
34+
const OrderingResolver:IResolvers = {
35+
Query: {
36+
ordering: queryOrdering
37+
}
38+
}
39+
40+
export { OrderingResolver }

Diff for: src/resolver/user/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { UserResolver } from "./userResolver"
2+
3+
export { UserResolver }

0 commit comments

Comments
 (0)