/
index.js
243 lines (229 loc) · 5.8 KB
/
index.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const { ApolloServer, gql } = require("apollo-server");
const { DateTime, EmailAddress } = require("@okgrow/graphql-scalars");
const depthLimit = require("graphql-depth-limit");
let meId = 1;
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
scalar DateTime
scalar EmailAddress
"""
高度單位
"""
enum HeightUnit {
"公尺"
METRE
"公分"
CENTIMETRE
"英尺 (1 英尺 = 30.48 公分)"
FOOT
}
"""
重量單位
"""
enum WeightUnit {
"公斤"
KILOGRAM
"公克"
GRAM
"磅 (1 磅 = 0.45359237 公斤)"
POUND
}
"""
使用者
"""
type User {
"識別碼"
id: ID!
"帳號"
email: EmailAddress!
"名字"
name: String
"年齡"
age: Int
"身高"
height(unit: HeightUnit = CENTIMETRE): Float
"體重"
weight(unit: WeightUnit = KILOGRAM): Float @deprecated (reason: "It's secret")
"朋友"
friends: [User]
"貼文"
posts: [Post]
"生日 ( ISO 格式)"
birthDay: DateTime
}
"""
貼文
"""
type Post {
"識別碼"
id: ID!
"作者"
author: User
"標題"
title: String
"內容"
content: String
"建立時間"
createdAt: DateTime
"按讚者"
likeGivers: [User]
}
type Query {
"測試用 Hello World"
hello: String
"目前使用者"
me: User
"所有使用者"
users: [User]
"特定使用者"
user(name: String!): User
}
`;
// Provide resolver functions for your schema fields
const datas = generateDatas();
const findUserById = id => datas.users.find(user => user.id === id);
const findUserByName = name => datas.users.find(user => user.name === name);
const filterPostsByAuthorId = authorId =>
datas.posts.filter(post => post.authorId === authorId);
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: (root, args, context) => {
return "Hello world!";
},
me: (root, args, { userId }) => {
return findUserById(userId);
},
users: () => datas.users,
user: (root, { name }, context) => {
return findUserByName(name);
}
},
User: {
friends: (parent, args, context) => {
return parent.friendIds.map(id => findUserById(id));
},
posts: (parent, args, context) => {
return filterPostsByAuthorId(parent.id);
},
height: (parent, args) => {
const { unit } = args;
if (!unit || unit === "CENTIMETRE") return parent.height;
else if (unit === "METRE") return parent.height / 100;
else if (unit === "FOOT") return parent.height / 30.48;
throw new Error(`Height unit "${unit}" not supported.`);
},
weight: (parent, args, context) => {
const { unit } = args;
if (!unit || unit === "KILOGRAM") return parent.weight;
else if (unit === "GRAM") return parent.weight * 100;
else if (unit === "POUND") return parent.weight / 0.45359237;
throw new Error(`Weight unit "${unit}" not supported.`);
}
},
Post: {
likeGivers: (parent, args, context) => {
return parent.likeGiverIds.map(id => findUserById(id));
},
author: (parent, args, context) => {
return findUserById(parent.authorId);
}
},
DateTime,
EmailAddress
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req, res }) => ({
userId: meId
}),
validationRules: [depthLimit(5)]
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
// Optional: Export a root value to be passed during execution
// export const rootValue = {};
// Optional: Export a root function, that returns root to be passed
// during execution, accepting headers and secrets. It can return a
// promise. rootFunction takes precedence over rootValue.
// export function rootFunction(headers, secrets) {
// return {
// headers,
// secrets,
// };
// };
function generateDatas() {
return {
users: [
{
id: 1,
name: "Fong",
email: "fong@test.com",
password: "123456",
age: 25,
friendIds: [2, 3],
height: 175.0,
weight: 70.0,
birthDay: "1997-07-12"
},
{
id: 2,
name: "Kevin",
email: "kevin@test.com",
password: "kevin123456",
age: 40,
height: 185.0,
weight: 90.0,
friendIds: [1]
},
{
id: 3,
name: "Mary",
email: "Mary@test.com",
password: "mary123456",
age: 18,
height: 162,
weight: null,
friendIds: [1]
}
],
posts: [
{
id: 1,
authorId: 1,
title: "Hello World!!",
content: "This is my first post. Nice to see you guys.",
createdAt: "2018-10-15",
likeGiverIds: [1, 3]
},
{
id: 2,
authorId: 2,
title: "Good Night",
content:
"Started earnest brother believe an exposed so. Me he believing daughters if forfeited at furniture. Age again and stuff downs spoke. Late hour new nay able fat each sell. Nor themselves age introduced frequently use unsatiable devonshire get. They why quit gay cold rose deal park. One same they four did ask busy. Reserved opinions fat him nay position. Breakfast as zealously incommode do agreeable furniture. One too nay led fanny allow plate. ",
createdAt: "2018-10-11",
likeGiverIds: [2, 3]
},
{
id: 3,
authorId: 3,
title: "Love U",
content:
"好濕。燕 草 如 碧 絲,秦 桑 低 綠 枝。當 君 懷 歸 日,是 妾 斷 腸 時 。春 風 不 相 識, 何 事 入 羅 幃 ?",
createdAt: "2018-10-10",
likeGiverIds: [1, 2]
},
{
id: 4,
authorId: 1,
title: "Love U Too",
content: "This is my first post. Nice to see you guys.",
createdAt: "2018-10-10",
likeGiverIds: [1, 2, 3]
}
]
};
}