-
Notifications
You must be signed in to change notification settings - Fork 11
/
User.ts
271 lines (246 loc) · 6.9 KB
/
User.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { schemaComposer, ResolverResolveParams } from 'graphql-compose';
import type { ConnectionSortMapOpts } from '../connection';
export const UserTC = schemaComposer.createObjectTC(`
type User {
id: Int
name: String
age: Int
gender: String
}
`);
export const UserLinkTC = schemaComposer.createObjectTC(`
type UserLink {
id: Int
type: String
userId: Int
otherUserId: Int
}
`);
export const userList = [
{ id: 1, name: 'user01', age: 11, gender: 'm' },
{ id: 2, name: 'user02', age: 12, gender: 'm' },
{ id: 3, name: 'user03', age: 13, gender: 'f' },
{ id: 4, name: 'user04', age: 14, gender: 'm' },
{ id: 5, name: 'user05', age: 15, gender: 'f' },
{ id: 6, name: 'user06', age: 16, gender: 'f' },
{ id: 7, name: 'user07', age: 17, gender: 'f' },
{ id: 8, name: 'user08', age: 18, gender: 'm' },
{ id: 9, name: 'user09', age: 19, gender: 'm' },
{ id: 10, name: 'user10', age: 49, gender: 'f' },
{ id: 11, name: 'user11', age: 49, gender: 'm' },
{ id: 12, name: 'user12', age: 47, gender: 'f' },
{ id: 15, name: 'user15', age: 45, gender: 'm' },
{ id: 14, name: 'user14', age: 45, gender: 'm' },
{ id: 13, name: 'user13', age: 45, gender: 'f' },
];
export const userLinkList = [
{ id: 1, type: 'likes', userId: 1, otherUserId: 2 },
{ id: 2, type: 'dislikes', userId: 2, otherUserId: 1 },
];
const filterArgConfig = schemaComposer.createInputTC(`
input FilterUserInput {
gender: String
age: Int
}
`);
const filterEdgeArgConfig = schemaComposer.createInputTC({
name: 'FilterNodeEdgeUserInput',
fields: {
edge: `
input FilterNodeEdgeEdgeUserInput {
type: String
}
`,
node: `
input FilterNodeNodeEdgeUserInput {
gender: String
age: Int
}
`,
},
});
function filterUserLink(
link: typeof userLinkList[0],
filter = {} as Partial<typeof userLinkList[0]>
) {
let pred = true;
if (filter.type) {
pred = pred && link.type === filter.type;
}
return pred;
}
function filterUser(user: typeof userList[0], filter = {} as any) {
let pred = true;
if (filter.gender) {
pred = pred && user.gender === filter.gender;
}
if (filter.id) {
if (filter.id.$lt) {
pred = pred && user.id < filter.id.$lt;
}
if (filter.id.$gt) {
pred = pred && user.id > filter.id.$gt;
}
}
if (filter.age) {
if (filter.age.$lt) {
pred = pred && user.age < filter.age.$lt;
}
if (filter.age.$gt) {
pred = pred && user.age > filter.age.$gt;
}
}
return pred;
}
function filteredUserList(list: typeof userList, filter = {}) {
return list.slice().filter((o) => filterUser(o, filter));
}
function sortUserList(
list: typeof userList,
sortValue = {} as Record<keyof typeof userList[0], number>
) {
const fields = Object.keys(sortValue) as Array<keyof typeof userList[0]>;
list.sort((a, b) => {
let result = 0;
fields.forEach((field) => {
if (result === 0) {
if (a[field] < b[field]) {
result = sortValue[field] * -1;
} else if (a[field] > b[field]) {
result = sortValue[field];
}
}
});
return result;
});
return list;
}
function prepareFilterFromArgs(resolveParams = {} as ResolverResolveParams<any, any>) {
const args = resolveParams.args || {};
const filter = { ...args.filter };
if (resolveParams.rawQuery) {
Object.keys(resolveParams.rawQuery).forEach((k) => {
filter[k] = resolveParams.rawQuery[k];
});
}
return filter;
}
export const findManyResolver = schemaComposer.createResolver<any, any>({
name: 'findMany',
kind: 'query',
type: UserTC,
args: {
filter: filterArgConfig,
sort: UserTC.schemaComposer.createEnumTC({
name: 'SortUserInput',
values: {
ID_ASC: { value: { id: 1 } },
ID_DESC: { value: { id: -1 } },
AGE_ASC: { value: { age: 1 } },
AGE_DESC: { value: { age: -1 } },
},
}),
limit: 'Int',
skip: 'Int',
},
resolve: (resolveParams) => {
const args = resolveParams.args || {};
const { sort, limit, skip } = args;
let list = userList.slice();
list = sortUserList(list, sort as any);
list = filteredUserList(list, prepareFilterFromArgs(resolveParams));
if (skip) {
list = list.slice(skip as any);
}
if (limit) {
list = list.slice(0, limit as any);
}
return Promise.resolve(list);
},
});
export const countResolver = schemaComposer.createResolver({
name: 'count',
kind: 'query',
type: 'Int',
args: {
filter: filterArgConfig,
},
resolve: (resolveParams) => {
return Promise.resolve(filteredUserList(userList, prepareFilterFromArgs(resolveParams)).length);
},
});
function getThroughLinkResolver(list: typeof userLinkList, filter: any) {
const nodeFilter = filter ? filter.node : {};
const edgeFilter = filter ? filter.edge : {};
return list
.map((link) => ({
...link,
node: userList.find((u) => u.id === link.otherUserId && filterUser(u, nodeFilter)),
}))
.filter((l) => !!l.node && filterUserLink(l, edgeFilter));
}
export const findManyThroughLinkResolver = schemaComposer.createResolver({
name: 'findManyThroughLink',
kind: 'query',
type: UserTC,
args: {
filter: filterEdgeArgConfig,
limit: 'Int',
skip: 'Int',
},
resolve: async (resolveParams) => {
const args = resolveParams.args || {};
const { limit, skip } = args;
let list = userLinkList.slice();
if (skip) {
list = list.slice(skip as any);
}
if (limit) {
list = list.slice(0, limit as any);
}
return getThroughLinkResolver(list, args.filter);
},
});
export const countThroughLinkResolver = schemaComposer.createResolver({
name: 'count',
kind: 'query',
type: 'Int',
args: {
filter: filterEdgeArgConfig,
},
resolve: async (resolveParams) => {
const args = resolveParams.args || {};
return getThroughLinkResolver(userLinkList.slice(), args.filter).length;
},
});
export const sortOptions: ConnectionSortMapOpts = {
ID_ASC: {
value: { id: 1 },
cursorFields: ['id'],
beforeCursorQuery: (rawQuery, cursorData) => {
if (!rawQuery.id) rawQuery.id = {};
rawQuery.id.$lt = cursorData.id;
},
afterCursorQuery: (rawQuery, cursorData) => {
if (!rawQuery.id) rawQuery.id = {};
rawQuery.id.$gt = cursorData.id;
},
},
AGE_ID_DESC: {
value: { age: -1, id: -1 },
cursorFields: ['age', 'id'],
beforeCursorQuery: (rawQuery, cursorData) => {
if (!rawQuery.age) rawQuery.age = {};
if (!rawQuery.id) rawQuery.id = {};
rawQuery.age = { $gt: cursorData.age };
rawQuery.id = { $gt: cursorData.id };
},
afterCursorQuery: (rawQuery, cursorData) => {
if (!rawQuery.age) rawQuery.age = {};
if (!rawQuery.id) rawQuery.id = {};
rawQuery.age = { $lt: cursorData.age };
rawQuery.id = { $lt: cursorData.id };
},
},
};