-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
118 lines (96 loc) · 2.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// const qqq: string = "안녕하세요";
// console.log(qqq);
//
import { DataSource } from "typeorm";
import { Board } from "./Board.postgres";
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
// API-DOCS 만들기
const typeDefs = `#graphql
input CreateBoardInput{
writer: String
title: String
contents: String
}
type MyBoard {
number: Int
writer: String
title: String
contents: String
}
type Query {
fetchBoards: [MyBoard]
}
type Mutation{
# 연습용 (example)
# createBoard(writer: String, title: String, contents: String): String
# 실무용 (practice)
createBoard(createBoardInput: CreateBoardInput): String
}
`;
// API 만들기
const resolvers = {
Query: {
fetchBoards: async () => {
// 1. 모두 꺼내기
const result = await Board.find();
console.log(result);
// 2. 한개만 꺼내기
// const result = await Board.findOne({ where: { number: 3 } });
// console.log(result);
return result;
},
},
Mutation: {
createBoard: async (parent: any, args: any, context: any, info: any) => {
await Board.insert({
...args.createBoardInput,
// 하나 하나 모두 입력하는 비효율적인 방식
// writer: args.createBoardInput.writer,
// title: args.createBoardInput.title,
// contents: args.createBoardInput.contents,
});
return "게시글 등록 성공";
},
updateBoard: async () => {
// 3번 게시글을 영희로 바꿔줘
await Board.update({ number: 3 }, { writer: "영희" });
},
deleteBoard: async () => {
// 3번 게시글 삭제해줘
await Board.delete({ number: 3 });
// 3번 게시글 삭제했다 치자.(소프트 삭제)
await Board.update({ number: 3 }, { isDeleted: true });
// 소프트삭제, deletedAt이 초기값 NULL 이면?, new Date() 들어가 있으면? 삭제 된거
Board.update({ number: 3 }, { deletedAt: new Date() });
},
},
};
// @ts-ignore
const server = new ApolloServer({
typeDefs,
resolvers,
cors: true,
});
const AppDataSource = new DataSource({
type: "postgres",
host: "34.63.244.122",
port: 5003,
username: "postgres",
password: "postgres2022",
database: "postgres",
entities: [Board],
synchronize: true,
logging: true, // 명령어가 어떻게 바뀌는지 보기
});
AppDataSource.initialize()
.then(() => {
console.log("DB 접속 성공");
startStandaloneServer(server).then(() => {
console.log("graphql 서버가 실행되었습니다.");
});
})
.catch((error) => {
console.log("DB 접속 실패");
console.log("원인 : ", error);
});