-
Notifications
You must be signed in to change notification settings - Fork 4
/
graphqlTypes.ts
204 lines (187 loc) · 4.98 KB
/
graphqlTypes.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
import {
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLFloat,
GraphQLInt,
GraphQLBoolean,
GraphQLEnumType,
GraphQLID,
GraphQLUnionType,
GraphQLList
} from "graphql";
import { GraphQLDateTime } from "graphql-iso-date";
import {
fromGlobalId,
globalIdField,
nodeDefinitions,
connectionDefinitions,
connectionFromArray
} from "graphql-relay";
import { users, workingGroups, data } from "./db";
import { paginate } from "./utils";
// @ts-ignore
export let { nodeInterface, nodeField } = nodeDefinitions(
(globalId: string) => {
let { type, id } = fromGlobalId(globalId);
// @ts-ignore
let source = data[type];
if (source) {
return source.find(
(item: { id: number }) => item.id === parseInt(id, 10)
);
}
return null;
},
// @ts-ignore
(obj: { type: string }) => {
switch (obj.type) {
case "User":
return userType;
case "WorkingGroup":
return workingGroupType;
case "SiteStatistics":
return siteStatisticsType;
case "Ticket":
return ticketType;
case "TodoItem":
return todoItemType;
}
}
);
export let userType: GraphQLObjectType = new GraphQLObjectType({
name: "User",
fields: () => ({
id: globalIdField(),
dbId: {
type: new GraphQLNonNull(GraphQLID),
resolve: obj => obj.id
},
avatarUrl: { type: GraphQLString },
fullName: { type: new GraphQLNonNull(GraphQLString) }
}),
interfaces: [nodeInterface]
});
export let userConnection = connectionDefinitions({ nodeType: userType });
export let workingGroupType: GraphQLObjectType = new GraphQLObjectType({
name: "WorkingGroup",
fields: () => ({
id: globalIdField(),
dbId: {
type: new GraphQLNonNull(GraphQLID),
resolve: obj => obj.id
},
name: { type: new GraphQLNonNull(GraphQLString) },
membersConnection: {
type: userConnection.connectionType,
resolve(obj, args) {
return connectionFromArray(
obj.memberIds
.map((id: number) => users.find(u => u.id === id))
.filter(Boolean),
args
);
}
},
members: {
args: {
limit: {
type: new GraphQLNonNull(GraphQLInt)
},
offset: {
type: new GraphQLNonNull(GraphQLInt)
}
},
type: new GraphQLList(userType),
resolve(obj, args) {
return paginate(
args.offset,
args.limit,
obj.memberIds
.map((id: number) => users.find(u => u.id === id))
.filter(Boolean)
);
}
}
}),
interfaces: [nodeInterface]
});
export let assigneeUnionType = new GraphQLUnionType({
name: "AssigneeType",
types: [userType, workingGroupType],
resolveType(value) {
if (value.type === "User") {
return userType;
} else if (value.type === "WorkingGroup") {
return workingGroupType;
}
}
});
export let siteStatisticsType: GraphQLObjectType = new GraphQLObjectType({
name: "SiteStatistics",
fields: () => ({
id: globalIdField(),
weeklySales: { type: new GraphQLNonNull(GraphQLFloat) },
weeklyOrders: { type: new GraphQLNonNull(GraphQLInt) },
currentVisitorsOnline: { type: new GraphQLNonNull(GraphQLInt) }
}),
interfaces: [nodeInterface]
});
export let ticketStatusEnum = new GraphQLEnumType({
name: "TicketStatus",
values: {
Done: { value: "Done" },
Progress: { value: "Progress" },
OnHold: { value: "OnHold" },
Rejected: { value: "Rejected" }
}
});
export let ticketType: GraphQLObjectType = new GraphQLObjectType({
name: "Ticket",
fields: () => ({
id: globalIdField(),
dbId: {
type: new GraphQLNonNull(GraphQLID),
resolve: obj => obj.id
},
assignee: {
type: assigneeUnionType,
resolve: obj => {
if (obj.assignee == null) {
return null;
}
switch (obj.assignee.type) {
case "User":
return users.find(u => u.id === obj.assignee.id);
case "WorkingGroup":
return workingGroups.find(wg => wg.id === obj.assignee.id);
default:
return null;
}
}
},
status: { type: new GraphQLNonNull(ticketStatusEnum) },
subject: { type: new GraphQLNonNull(GraphQLString) },
lastUpdated: {
type: GraphQLDateTime,
resolve: obj => (obj.lastUpdated ? new Date(obj.lastUpdated) : null)
},
trackingId: { type: new GraphQLNonNull(GraphQLString) }
}),
interfaces: [nodeInterface]
});
export let ticketConnection = connectionDefinitions({ nodeType: ticketType });
export let todoItemType: GraphQLObjectType = new GraphQLObjectType({
name: "TodoItem",
fields: () => ({
id: globalIdField(),
dbId: {
type: new GraphQLNonNull(GraphQLID),
resolve: obj => obj.id
},
completed: { type: GraphQLBoolean },
text: { type: new GraphQLNonNull(GraphQLString) }
}),
interfaces: [nodeInterface]
});
export let todoConnection = connectionDefinitions({ nodeType: todoItemType });