-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUser.ts
144 lines (130 loc) · 3.72 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
import { schemaComposer, ResolverResolveParams } from 'graphql-compose';
export const UserTC = schemaComposer.createObjectTC(`
type User {
id: Int
name: String
age: Int
gender: String
}
`);
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' },
];
const filterArgConfig = {
name: 'filter',
type: `input FilterUserInput {
gender: String
age: Int
}`,
};
function filteredUserList(list: typeof userList, filter = {} as any) {
let result = list.slice();
if (filter.gender) {
result = result.filter((o) => o.gender === filter.gender);
}
if (filter.id) {
if (filter.id.$lt) {
result = result.filter((o) => o.id < filter.id.$lt);
}
if (filter.id.$gt) {
result = result.filter((o) => o.id > filter.id.$gt);
}
}
if (filter.age) {
if (filter.age.$lt) {
result = result.filter((o) => o.age < filter.age.$lt);
}
if (filter.age.$gt) {
result = result.filter((o) => o.age > filter.age.$gt);
}
}
return result;
}
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: 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);
},
});