forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-schemas.js
executable file
·363 lines (306 loc) · 15.8 KB
/
process-schemas.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const { sortBy } = require('lodash')
const { parse, buildASTSchema } = require('graphql')
const helpers = require('./schema-helpers')
const externalScalars = require('../../../lib/graphql/non-schema-scalars')
.map(scalar => {
scalar.id = helpers.getId(scalar.name)
scalar.href = helpers.getFullLink('scalars', scalar.id)
return scalar
})
// select and format all the data from the schema that we need for the docs
// used in the build step by script/graphql/build-static-files.js
module.exports = async function processSchemas (idl, previewsPerVersion) {
const schemaAST = parse(idl.toString())
const schema = buildASTSchema(schemaAST)
// list of objects is used when processing mutations
const objectsInSchema = schemaAST.definitions.filter(def => def.kind === 'ObjectTypeDefinition')
const data = {}
data.queries = {
connections: [],
fields: []
}
data.mutations = []
data.objects = []
data.interfaces = []
data.enums = []
data.unions = []
data.inputObjects = []
data.scalars = []
await Promise.all(schemaAST.definitions.map(async (def) => {
// QUERIES
if (def.name.value === 'Query') {
await Promise.all(def.fields.map(async (field) => {
const query = {}
const queryArgs = []
query.name = field.name.value
query.type = helpers.getType(field)
query.kind = helpers.getTypeKind(query.type, schema)
query.id = helpers.getId(query.type)
query.href = helpers.getFullLink(query.kind, query.id)
query.description = await helpers.getDescription(field.description.value)
query.isDeprecated = helpers.getDeprecationStatus(field.directives, query.name)
query.deprecationReason = await helpers.getDeprecationReason(field.directives, query)
query.preview = await helpers.getPreview(field.directives, query, previewsPerVersion)
await Promise.all(field.arguments.map(async (arg) => {
const queryArg = {}
queryArg.name = arg.name.value
queryArg.defaultValue = arg.defaultValue ? arg.defaultValue.value : undefined
queryArg.type = helpers.getType(arg)
queryArg.id = helpers.getId(queryArg.type)
queryArg.kind = helpers.getTypeKind(queryArg.type, schema)
queryArg.href = helpers.getFullLink(queryArg.kind, queryArg.id)
queryArg.description = await helpers.getDescription(arg.description.value)
queryArg.isDeprecated = helpers.getDeprecationStatus(arg.directives, queryArg.name)
queryArg.deprecationReason = await helpers.getDeprecationReason(arg.directives, queryArg)
queryArg.preview = await helpers.getPreview(arg.directives, queryArg, previewsPerVersion)
queryArgs.push(queryArg)
}))
query.args = sortBy(queryArgs, 'name')
// QUERY CONNECTIONS
// QUERY FIELDS
query.id.endsWith('connection')
? data.queries.connections.push(query)
: data.queries.fields.push(query)
}))
return
}
// MUTATIONS
if (def.name.value === 'Mutation') {
await Promise.all(def.fields.map(async (field) => {
const mutation = {}
const inputFields = []
const returnFields = []
mutation.name = field.name.value
mutation.kind = helpers.getKind(def.name.value)
mutation.id = helpers.getId(mutation.name)
mutation.href = helpers.getFullLink('mutations', mutation.id)
mutation.description = await helpers.getDescription(field.description.value)
mutation.isDeprecated = helpers.getDeprecationStatus(field.directives, mutation.name)
mutation.deprecationReason = await helpers.getDeprecationReason(field.directives, mutation)
mutation.preview = await helpers.getPreview(field.directives, mutation, previewsPerVersion)
// there is only ever one input field argument, but loop anyway
await Promise.all(field.arguments.map(async (field) => {
const inputField = {}
inputField.name = field.name.value
inputField.type = helpers.getType(field)
inputField.id = helpers.getId(inputField.type)
inputField.kind = helpers.getTypeKind(inputField.type, schema)
inputField.href = helpers.getFullLink(inputField.kind, inputField.id)
inputFields.push(inputField)
}))
mutation.inputFields = sortBy(inputFields, 'name')
// get return fields
// first get the payload, then find payload object's fields. these are the mutation's return fields.
const returnType = helpers.getType(field)
const mutationReturnFields = objectsInSchema.find(obj => obj.name.value === returnType)
if (!mutationReturnFields) console.log(`no return fields found for ${returnType}`)
await Promise.all(mutationReturnFields.fields.map(async (field) => {
const returnField = {}
returnField.name = field.name.value
returnField.type = helpers.getType(field)
returnField.id = helpers.getId(returnField.type)
returnField.kind = helpers.getTypeKind(returnField.type, schema)
returnField.href = helpers.getFullLink(returnField.kind, returnField.id)
returnField.description = await helpers.getDescription(field.description.value)
returnField.isDeprecated = helpers.getDeprecationStatus(field.directives, returnField.name)
returnField.deprecationReason = await helpers.getDeprecationReason(field.directives, returnField)
returnField.preview = await helpers.getPreview(field.directives, returnField, previewsPerVersion)
returnFields.push(returnField)
}))
mutation.returnFields = sortBy(returnFields, 'name')
data.mutations.push(mutation)
}))
return
}
// OBJECTS
if (def.kind === 'ObjectTypeDefinition') {
// objects ending with 'Payload' are only used to derive mutation values
// they are not included in the objects docs
if (def.name.value.endsWith('Payload')) return
const object = {}
const objectImplements = []
const objectFields = []
object.name = def.name.value
object.kind = helpers.getKind(def.kind)
object.id = helpers.getId(object.name)
object.href = helpers.getFullLink('objects', object.id)
object.description = await helpers.getDescription(def.description.value)
object.isDeprecated = helpers.getDeprecationStatus(def.directives, object.name)
object.deprecationReason = await helpers.getDeprecationReason(def.directives, object)
object.preview = await helpers.getPreview(def.directives, object, previewsPerVersion)
// an object's interfaces render in the `Implements` section
// interfaces do not have directives so they cannot be under preview/deprecated
if (def.interfaces.length) {
await Promise.all(def.interfaces.map(async (graphqlInterface) => {
const objectInterface = {}
objectInterface.name = graphqlInterface.name.value
objectInterface.id = helpers.getId(objectInterface.name)
objectInterface.href = helpers.getFullLink('interfaces', objectInterface.id)
objectImplements.push(objectInterface)
}))
}
// an object's fields render in the `Fields` section
if (def.fields.length) {
await Promise.all(def.fields.map(async (field) => {
if (!field.description) return
const objectField = {}
objectField.name = field.name.value
objectField.description = await helpers.getDescription(field.description.value)
objectField.type = helpers.getType(field)
objectField.id = helpers.getId(objectField.type)
objectField.kind = helpers.getTypeKind(objectField.type, schema)
objectField.href = helpers.getFullLink(objectField.kind, objectField.id)
objectField.arguments = await helpers.getArguments(field.arguments, schema)
objectField.isDeprecated = helpers.getDeprecationStatus(field.directives)
objectField.deprecationReason = await helpers.getDeprecationReason(field.directives, objectField)
objectField.preview = await helpers.getPreview(field.directives, objectField, previewsPerVersion)
objectFields.push(objectField)
}))
}
if (objectImplements.length) object.implements = sortBy(objectImplements, 'name')
if (objectFields.length) object.fields = sortBy(objectFields, 'name')
data.objects.push(object)
return
}
// INTERFACES
if (def.kind === 'InterfaceTypeDefinition') {
const graphqlInterface = {}
const interfaceFields = []
graphqlInterface.name = def.name.value
graphqlInterface.kind = helpers.getKind(def.kind)
graphqlInterface.id = helpers.getId(graphqlInterface.name)
graphqlInterface.href = helpers.getFullLink('interfaces', graphqlInterface.id)
graphqlInterface.description = await helpers.getDescription(def.description.value)
graphqlInterface.isDeprecated = helpers.getDeprecationStatus(def.directives)
graphqlInterface.deprecationReason = await helpers.getDeprecationReason(def.directives, graphqlInterface)
graphqlInterface.preview = await helpers.getPreview(def.directives, graphqlInterface, previewsPerVersion)
// an interface's fields render in the "Fields" section
if (def.fields.length) {
await Promise.all(def.fields.map(async (field) => {
if (!field.description) return
const interfaceField = {}
interfaceField.name = field.name.value
interfaceField.description = await helpers.getDescription(field.description.value)
interfaceField.type = helpers.getType(field)
interfaceField.id = helpers.getId(interfaceField.type)
interfaceField.kind = helpers.getTypeKind(interfaceField.type, schema)
interfaceField.href = helpers.getFullLink(interfaceField.kind, interfaceField.id)
interfaceField.arguments = await helpers.getArguments(field.arguments, schema)
interfaceField.isDeprecated = helpers.getDeprecationStatus(field.directives)
interfaceField.deprecationReason = await helpers.getDeprecationReason(field.directives, interfaceField)
interfaceField.preview = await helpers.getPreview(field.directives, interfaceField, previewsPerVersion)
interfaceFields.push(interfaceField)
}))
}
graphqlInterface.fields = sortBy(interfaceFields, 'name')
data.interfaces.push(graphqlInterface)
return
}
// ENUMS
if (def.kind === 'EnumTypeDefinition') {
const graphqlEnum = {}
const enumValues = []
graphqlEnum.name = def.name.value
graphqlEnum.kind = helpers.getKind(def.kind)
graphqlEnum.id = helpers.getId(graphqlEnum.name)
graphqlEnum.href = helpers.getFullLink('enums', graphqlEnum.id)
graphqlEnum.description = await helpers.getDescription(def.description.value)
graphqlEnum.isDeprecated = helpers.getDeprecationStatus(def.directives)
graphqlEnum.deprecationReason = await helpers.getDeprecationReason(def.directives, graphqlEnum)
graphqlEnum.preview = await helpers.getPreview(def.directives, graphqlEnum, previewsPerVersion)
await Promise.all(def.values.map(async (value) => {
const enumValue = {}
enumValue.name = value.name.value
enumValue.description = await helpers.getDescription(value.description.value)
enumValues.push(enumValue)
}))
graphqlEnum.values = sortBy(enumValues, 'name')
data.enums.push(graphqlEnum)
return
}
// UNIONS
if (def.kind === 'UnionTypeDefinition') {
const union = {}
const possibleTypes = []
union.name = def.name.value
union.kind = helpers.getKind(def.kind)
union.id = helpers.getId(union.name)
union.href = helpers.getFullLink('unions', union.id)
union.description = await helpers.getDescription(def.description.value)
union.isDeprecated = helpers.getDeprecationStatus(def.directives)
union.deprecationReason = await helpers.getDeprecationReason(def.directives, union)
union.preview = await helpers.getPreview(def.directives, union, previewsPerVersion)
// union types do not have directives so cannot be under preview/deprecated
await Promise.all(def.types.map(async (type) => {
const possibleType = {}
possibleType.name = type.name.value
possibleType.id = helpers.getId(possibleType.name)
possibleType.href = helpers.getFullLink('objects', possibleType.id)
possibleTypes.push(possibleType)
}))
union.possibleTypes = sortBy(possibleTypes, 'name')
data.unions.push(union)
return
}
// INPUT OBJECTS
// NOTE: input objects ending with `Input` are NOT included in the v4 input objects sidebar
// but they are still present in the docs (e.g., https://developer.github.com/v4/input_object/acceptenterpriseadministratorinvitationinput/)
// so we will include them here
if (def.kind === 'InputObjectTypeDefinition') {
const inputObject = {}
const inputFields = []
inputObject.name = def.name.value
inputObject.kind = helpers.getKind(def.kind)
inputObject.id = helpers.getId(inputObject.name)
inputObject.href = helpers.getFullLink('input-objects', inputObject.id)
inputObject.description = await helpers.getDescription(def.description.value)
inputObject.isDeprecated = helpers.getDeprecationStatus(def.directives)
inputObject.deprecationReason = await helpers.getDeprecationReason(def.directives, inputObject)
inputObject.preview = await helpers.getPreview(def.directives, inputObject, previewsPerVersion)
if (def.fields.length) {
await Promise.all(def.fields.map(async (field) => {
const inputField = {}
inputField.name = field.name.value
inputField.description = await helpers.getDescription(field.description.value)
inputField.type = helpers.getType(field)
inputField.id = helpers.getId(inputField.type)
inputField.kind = helpers.getTypeKind(inputField.type, schema)
inputField.href = helpers.getFullLink(inputField.kind, inputField.id)
inputField.isDeprecated = helpers.getDeprecationStatus(field.directives)
inputField.deprecationReason = await helpers.getDeprecationReason(field.directives, inputField)
inputField.preview = await helpers.getPreview(field.directives, inputField, previewsPerVersion)
inputFields.push(inputField)
}))
}
inputObject.inputFields = sortBy(inputFields, 'name')
data.inputObjects.push(inputObject)
return
}
// SCALARS
if (def.kind === 'ScalarTypeDefinition') {
const scalar = {}
scalar.name = def.name.value
scalar.kind = helpers.getKind(def.kind)
scalar.id = helpers.getId(scalar.name)
scalar.href = helpers.getFullLink('scalars', scalar.id)
scalar.description = await helpers.getDescription(def.description.value)
scalar.isDeprecated = helpers.getDeprecationStatus(def.directives)
scalar.deprecationReason = await helpers.getDeprecationReason(def.directives, scalar)
scalar.preview = await helpers.getPreview(def.directives, scalar, previewsPerVersion)
data.scalars.push(scalar)
}
}))
// add non-schema scalars and sort all scalars alphabetically
data.scalars = sortBy(data.scalars.concat(externalScalars), 'name')
// sort all the types alphabetically
data.queries.connections = sortBy(data.queries.connections, 'name')
data.queries.fields = sortBy(data.queries.fields, 'name')
data.mutations = sortBy(data.mutations, 'name')
data.objects = sortBy(data.objects, 'name')
data.interfaces = sortBy(data.interfaces, 'name')
data.enums = sortBy(data.enums, 'name')
data.unions = sortBy(data.unions, 'name')
data.inputObjects = sortBy(data.inputObjects, 'name')
data.scalars = sortBy(data.scalars, 'name')
return data
}