-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschema.js
238 lines (228 loc) · 6.31 KB
/
schema.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
const {
GraphQLInt,
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} = require('graphql');
const AppType = new GraphQLObjectType({
name: 'App',
description: 'Top-level objects that link together and contain configuration information for your packages, droplets, processes, tasks, and more.',
fields: () => ({
name: {
type: GraphQLString,
description: 'Name of the app',
},
guid: {
type: GraphQLString,
description: 'Guid of the app',
},
packages: {
type: new GraphQLList(PackageType),
description: 'Packages for the app.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadManyByURL(obj.links.packages.href)
},
},
processes: {
type: new GraphQLList(ProcessType),
description: 'Processes for the app.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadManyByURL(obj.links.processes.href)
},
},
droplets: {
type: new GraphQLList(DropletType),
description: 'Droplets for the app.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadManyByURL(obj.links.droplets.href)
},
},
current_droplet: {
type: DropletType,
description: 'Droplet the app will use when running.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadByURL(obj.links.current_droplet.href)
},
}
})
});
const TaskType = new GraphQLObjectType({
name: 'Task',
description: 'One-off jobs that are intended to perform a task, stop, and be cleaned up, freeing up resources.',
fields: () => ({
name: {
type: GraphQLString,
description: 'Name of the task.'
},
command: {
type: GraphQLString,
description: 'Command that will be executed.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadByURL(obj.links.self.href).then(task => {
return task.command
})
},
},
droplet: {
type: DropletType,
description: 'The droplet used to run the command.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadByURL(obj.links.droplet.href)
},
}
})
});
const PackageType = new GraphQLObjectType({
name: 'Package',
description: 'An application’s ‘source code’; either raw bits for your application or a pointer to these bits.',
fields: () => ({
guid: {
type: GraphQLString,
description: 'Guid of the package',
},
state: {
type: GraphQLString,
description: 'State of the package',
},
})
});
const ProcessType = new GraphQLObjectType({
name: 'Process',
description: 'The runnable units of an app.',
fields: () => ({
guid: {
type: GraphQLString,
description: 'Guid of the process',
},
type: {
type: GraphQLString,
description: 'A unique identifier for processes belonging to an app.',
},
instance_count: {
type: GraphQLInt,
description: 'The number of instances to run.',
resolve: (obj) => {
return obj.instances
}
},
allocated_memory_mb: {
type: GraphQLInt,
description: 'Memory allocated per instance in mb',
resolve: (obj) => {
return obj.memory_in_mb
}
},
instances: {
type: new GraphQLList(InstanceType),
description: 'Instances of a process.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadManyByURL(obj.links.stats.href)
},
},
routes: {
type: new GraphQLList(RouteType),
description: 'Instances of a process.',
resolve: (obj, args, {loaders}) => {
return loaders.resource.loadByURL(obj.links.app.href)
.then((app) => {
return loaders.resource.loadManyByURL(app.links.route_mappings.href)
}).then((route_mappings) => {
const route_urls = route_mappings.filter((mapping) => {
return mapping.links.process.href.split("/").pop() === obj.type
}).map((mapping) => {
return mapping.links.route.href
});
return loaders.v2Resource.loadMany(route_urls)
})
},
}
})
});
const RouteType = new GraphQLObjectType({
name: 'Route',
description: 'Address to a process',
fields: () => ({
host: {
type: GraphQLString,
description: 'Hostname for the route',
},
domain: {
type: GraphQLString,
description: 'Domain for the route',
resolve: (obj, args, {loaders}) => {
return loaders.v2Resource.loadRelatively(obj.domain_url).then((domain) => {
return domain.name
})
}
}
})
});
const InstanceType = new GraphQLObjectType({
name: 'Instance',
description: 'Instance of a process',
fields: () => ({
actual_memory_mb: {
type: GraphQLInt,
description: 'Memory used by the instance in mb',
resolve: (obj) => {
return obj.usage.mem / 100000
}
},
index: {
type: GraphQLInt,
description: 'Index of the instance.'
}
})
});
const DropletType = new GraphQLObjectType({
name: 'Droplet',
description: 'The result of staging an application package.',
fields: () => ({
guid: {
type: GraphQLString,
description: 'Guid of the droplet',
},
state: {
type: GraphQLString,
description: 'State of the droplet.',
},
package: {
type: PackageType,
description: 'Package staged to create droplet',
resolve: (root, args, {loaders}) => {
return loaders.resource.loadByURL(root.links.package.href)
},
}
})
});
const QueryType = new GraphQLObjectType({
name: 'Query',
description: 'The root of all... queries',
fields: () => ({
apps: {
type: new GraphQLList(AppType),
description: 'List of apps',
args: {
limit: {
description: 'Max number to display',
type: GraphQLInt
}
},
resolve: (root, {limit}, {loaders}) => {
const url = limit ? `/v3/apps/?per_page=${limit}` : `/v3/apps/`;
return loaders.app.loadAll(url)
},
},
tasks: {
type: new GraphQLList(TaskType),
description: 'List of tasks',
resolve: (obj, args, {loaders}) => {
return loaders.task.loadAll()
}
}
}),
});
module.exports = new GraphQLSchema({
query: QueryType,
});