Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

process-schemas.js 16 KB

You have to be logged in to leave a comment. Sign In
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
  1. const { sortBy } = require('lodash')
  2. const { parse, buildASTSchema } = require('graphql')
  3. const helpers = require('./schema-helpers')
  4. const externalScalars = require('../../../lib/graphql/non-schema-scalars')
  5. .map(scalar => {
  6. scalar.id = helpers.getId(scalar.name)
  7. scalar.href = helpers.getFullLink('scalars', scalar.id)
  8. return scalar
  9. })
  10. // select and format all the data from the schema that we need for the docs
  11. // used in the build step by script/graphql/build-static-files.js
  12. module.exports = async function processSchemas (idl, previewsPerVersion) {
  13. const schemaAST = parse(idl.toString())
  14. const schema = buildASTSchema(schemaAST)
  15. // list of objects is used when processing mutations
  16. const objectsInSchema = schemaAST.definitions.filter(def => def.kind === 'ObjectTypeDefinition')
  17. const data = {}
  18. data.queries = {
  19. connections: [],
  20. fields: []
  21. }
  22. data.mutations = []
  23. data.objects = []
  24. data.interfaces = []
  25. data.enums = []
  26. data.unions = []
  27. data.inputObjects = []
  28. data.scalars = []
  29. await Promise.all(schemaAST.definitions.map(async (def) => {
  30. // QUERIES
  31. if (def.name.value === 'Query') {
  32. await Promise.all(def.fields.map(async (field) => {
  33. const query = {}
  34. const queryArgs = []
  35. query.name = field.name.value
  36. query.type = helpers.getType(field)
  37. query.kind = helpers.getTypeKind(query.type, schema)
  38. query.id = helpers.getId(query.type)
  39. query.href = helpers.getFullLink(query.kind, query.id)
  40. query.description = await helpers.getDescription(field.description.value)
  41. query.isDeprecated = helpers.getDeprecationStatus(field.directives, query.name)
  42. query.deprecationReason = await helpers.getDeprecationReason(field.directives, query)
  43. query.preview = await helpers.getPreview(field.directives, query, previewsPerVersion)
  44. await Promise.all(field.arguments.map(async (arg) => {
  45. const queryArg = {}
  46. queryArg.name = arg.name.value
  47. queryArg.defaultValue = arg.defaultValue ? arg.defaultValue.value : undefined
  48. queryArg.type = helpers.getType(arg)
  49. queryArg.id = helpers.getId(queryArg.type)
  50. queryArg.kind = helpers.getTypeKind(queryArg.type, schema)
  51. queryArg.href = helpers.getFullLink(queryArg.kind, queryArg.id)
  52. queryArg.description = await helpers.getDescription(arg.description.value)
  53. queryArg.isDeprecated = helpers.getDeprecationStatus(arg.directives, queryArg.name)
  54. queryArg.deprecationReason = await helpers.getDeprecationReason(arg.directives, queryArg)
  55. queryArg.preview = await helpers.getPreview(arg.directives, queryArg, previewsPerVersion)
  56. queryArgs.push(queryArg)
  57. }))
  58. query.args = sortBy(queryArgs, 'name')
  59. // QUERY CONNECTIONS
  60. // QUERY FIELDS
  61. query.id.endsWith('connection')
  62. ? data.queries.connections.push(query)
  63. : data.queries.fields.push(query)
  64. }))
  65. return
  66. }
  67. // MUTATIONS
  68. if (def.name.value === 'Mutation') {
  69. await Promise.all(def.fields.map(async (field) => {
  70. const mutation = {}
  71. const inputFields = []
  72. const returnFields = []
  73. mutation.name = field.name.value
  74. mutation.kind = helpers.getKind(def.name.value)
  75. mutation.id = helpers.getId(mutation.name)
  76. mutation.href = helpers.getFullLink('mutations', mutation.id)
  77. mutation.description = await helpers.getDescription(field.description.value)
  78. mutation.isDeprecated = helpers.getDeprecationStatus(field.directives, mutation.name)
  79. mutation.deprecationReason = await helpers.getDeprecationReason(field.directives, mutation)
  80. mutation.preview = await helpers.getPreview(field.directives, mutation, previewsPerVersion)
  81. // there is only ever one input field argument, but loop anyway
  82. await Promise.all(field.arguments.map(async (field) => {
  83. const inputField = {}
  84. inputField.name = field.name.value
  85. inputField.type = helpers.getType(field)
  86. inputField.id = helpers.getId(inputField.type)
  87. inputField.kind = helpers.getTypeKind(inputField.type, schema)
  88. inputField.href = helpers.getFullLink(inputField.kind, inputField.id)
  89. inputFields.push(inputField)
  90. }))
  91. mutation.inputFields = sortBy(inputFields, 'name')
  92. // get return fields
  93. // first get the payload, then find payload object's fields. these are the mutation's return fields.
  94. const returnType = helpers.getType(field)
  95. const mutationReturnFields = objectsInSchema.find(obj => obj.name.value === returnType)
  96. if (!mutationReturnFields) console.log(`no return fields found for ${returnType}`)
  97. await Promise.all(mutationReturnFields.fields.map(async (field) => {
  98. const returnField = {}
  99. returnField.name = field.name.value
  100. returnField.type = helpers.getType(field)
  101. returnField.id = helpers.getId(returnField.type)
  102. returnField.kind = helpers.getTypeKind(returnField.type, schema)
  103. returnField.href = helpers.getFullLink(returnField.kind, returnField.id)
  104. returnField.description = await helpers.getDescription(field.description.value)
  105. returnField.isDeprecated = helpers.getDeprecationStatus(field.directives, returnField.name)
  106. returnField.deprecationReason = await helpers.getDeprecationReason(field.directives, returnField)
  107. returnField.preview = await helpers.getPreview(field.directives, returnField, previewsPerVersion)
  108. returnFields.push(returnField)
  109. }))
  110. mutation.returnFields = sortBy(returnFields, 'name')
  111. data.mutations.push(mutation)
  112. }))
  113. return
  114. }
  115. // OBJECTS
  116. if (def.kind === 'ObjectTypeDefinition') {
  117. // objects ending with 'Payload' are only used to derive mutation values
  118. // they are not included in the objects docs
  119. if (def.name.value.endsWith('Payload')) return
  120. const object = {}
  121. const objectImplements = []
  122. const objectFields = []
  123. object.name = def.name.value
  124. object.kind = helpers.getKind(def.kind)
  125. object.id = helpers.getId(object.name)
  126. object.href = helpers.getFullLink('objects', object.id)
  127. object.description = await helpers.getDescription(def.description.value)
  128. object.isDeprecated = helpers.getDeprecationStatus(def.directives, object.name)
  129. object.deprecationReason = await helpers.getDeprecationReason(def.directives, object)
  130. object.preview = await helpers.getPreview(def.directives, object, previewsPerVersion)
  131. // an object's interfaces render in the `Implements` section
  132. // interfaces do not have directives so they cannot be under preview/deprecated
  133. if (def.interfaces.length) {
  134. await Promise.all(def.interfaces.map(async (graphqlInterface) => {
  135. const objectInterface = {}
  136. objectInterface.name = graphqlInterface.name.value
  137. objectInterface.id = helpers.getId(objectInterface.name)
  138. objectInterface.href = helpers.getFullLink('interfaces', objectInterface.id)
  139. objectImplements.push(objectInterface)
  140. }))
  141. }
  142. // an object's fields render in the `Fields` section
  143. if (def.fields.length) {
  144. await Promise.all(def.fields.map(async (field) => {
  145. if (!field.description) return
  146. const objectField = {}
  147. objectField.name = field.name.value
  148. objectField.description = await helpers.getDescription(field.description.value)
  149. objectField.type = helpers.getType(field)
  150. objectField.id = helpers.getId(objectField.type)
  151. objectField.kind = helpers.getTypeKind(objectField.type, schema)
  152. objectField.href = helpers.getFullLink(objectField.kind, objectField.id)
  153. objectField.arguments = await helpers.getArguments(field.arguments, schema)
  154. objectField.isDeprecated = helpers.getDeprecationStatus(field.directives)
  155. objectField.deprecationReason = await helpers.getDeprecationReason(field.directives, objectField)
  156. objectField.preview = await helpers.getPreview(field.directives, objectField, previewsPerVersion)
  157. objectFields.push(objectField)
  158. }))
  159. }
  160. if (objectImplements.length) object.implements = sortBy(objectImplements, 'name')
  161. if (objectFields.length) object.fields = sortBy(objectFields, 'name')
  162. data.objects.push(object)
  163. return
  164. }
  165. // INTERFACES
  166. if (def.kind === 'InterfaceTypeDefinition') {
  167. const graphqlInterface = {}
  168. const interfaceFields = []
  169. graphqlInterface.name = def.name.value
  170. graphqlInterface.kind = helpers.getKind(def.kind)
  171. graphqlInterface.id = helpers.getId(graphqlInterface.name)
  172. graphqlInterface.href = helpers.getFullLink('interfaces', graphqlInterface.id)
  173. graphqlInterface.description = await helpers.getDescription(def.description.value)
  174. graphqlInterface.isDeprecated = helpers.getDeprecationStatus(def.directives)
  175. graphqlInterface.deprecationReason = await helpers.getDeprecationReason(def.directives, graphqlInterface)
  176. graphqlInterface.preview = await helpers.getPreview(def.directives, graphqlInterface, previewsPerVersion)
  177. // an interface's fields render in the "Fields" section
  178. if (def.fields.length) {
  179. await Promise.all(def.fields.map(async (field) => {
  180. if (!field.description) return
  181. const interfaceField = {}
  182. interfaceField.name = field.name.value
  183. interfaceField.description = await helpers.getDescription(field.description.value)
  184. interfaceField.type = helpers.getType(field)
  185. interfaceField.id = helpers.getId(interfaceField.type)
  186. interfaceField.kind = helpers.getTypeKind(interfaceField.type, schema)
  187. interfaceField.href = helpers.getFullLink(interfaceField.kind, interfaceField.id)
  188. interfaceField.arguments = await helpers.getArguments(field.arguments, schema)
  189. interfaceField.isDeprecated = helpers.getDeprecationStatus(field.directives)
  190. interfaceField.deprecationReason = await helpers.getDeprecationReason(field.directives, interfaceField)
  191. interfaceField.preview = await helpers.getPreview(field.directives, interfaceField, previewsPerVersion)
  192. interfaceFields.push(interfaceField)
  193. }))
  194. }
  195. graphqlInterface.fields = sortBy(interfaceFields, 'name')
  196. data.interfaces.push(graphqlInterface)
  197. return
  198. }
  199. // ENUMS
  200. if (def.kind === 'EnumTypeDefinition') {
  201. const graphqlEnum = {}
  202. const enumValues = []
  203. graphqlEnum.name = def.name.value
  204. graphqlEnum.kind = helpers.getKind(def.kind)
  205. graphqlEnum.id = helpers.getId(graphqlEnum.name)
  206. graphqlEnum.href = helpers.getFullLink('enums', graphqlEnum.id)
  207. graphqlEnum.description = await helpers.getDescription(def.description.value)
  208. graphqlEnum.isDeprecated = helpers.getDeprecationStatus(def.directives)
  209. graphqlEnum.deprecationReason = await helpers.getDeprecationReason(def.directives, graphqlEnum)
  210. graphqlEnum.preview = await helpers.getPreview(def.directives, graphqlEnum, previewsPerVersion)
  211. await Promise.all(def.values.map(async (value) => {
  212. const enumValue = {}
  213. enumValue.name = value.name.value
  214. enumValue.description = await helpers.getDescription(value.description.value)
  215. enumValues.push(enumValue)
  216. }))
  217. graphqlEnum.values = sortBy(enumValues, 'name')
  218. data.enums.push(graphqlEnum)
  219. return
  220. }
  221. // UNIONS
  222. if (def.kind === 'UnionTypeDefinition') {
  223. const union = {}
  224. const possibleTypes = []
  225. union.name = def.name.value
  226. union.kind = helpers.getKind(def.kind)
  227. union.id = helpers.getId(union.name)
  228. union.href = helpers.getFullLink('unions', union.id)
  229. union.description = await helpers.getDescription(def.description.value)
  230. union.isDeprecated = helpers.getDeprecationStatus(def.directives)
  231. union.deprecationReason = await helpers.getDeprecationReason(def.directives, union)
  232. union.preview = await helpers.getPreview(def.directives, union, previewsPerVersion)
  233. // union types do not have directives so cannot be under preview/deprecated
  234. await Promise.all(def.types.map(async (type) => {
  235. const possibleType = {}
  236. possibleType.name = type.name.value
  237. possibleType.id = helpers.getId(possibleType.name)
  238. possibleType.href = helpers.getFullLink('objects', possibleType.id)
  239. possibleTypes.push(possibleType)
  240. }))
  241. union.possibleTypes = sortBy(possibleTypes, 'name')
  242. data.unions.push(union)
  243. return
  244. }
  245. // INPUT OBJECTS
  246. // NOTE: input objects ending with `Input` are NOT included in the v4 input objects sidebar
  247. // but they are still present in the docs (e.g., https://developer.github.com/v4/input_object/acceptenterpriseadministratorinvitationinput/)
  248. // so we will include them here
  249. if (def.kind === 'InputObjectTypeDefinition') {
  250. const inputObject = {}
  251. const inputFields = []
  252. inputObject.name = def.name.value
  253. inputObject.kind = helpers.getKind(def.kind)
  254. inputObject.id = helpers.getId(inputObject.name)
  255. inputObject.href = helpers.getFullLink('input-objects', inputObject.id)
  256. inputObject.description = await helpers.getDescription(def.description.value)
  257. inputObject.isDeprecated = helpers.getDeprecationStatus(def.directives)
  258. inputObject.deprecationReason = await helpers.getDeprecationReason(def.directives, inputObject)
  259. inputObject.preview = await helpers.getPreview(def.directives, inputObject, previewsPerVersion)
  260. if (def.fields.length) {
  261. await Promise.all(def.fields.map(async (field) => {
  262. const inputField = {}
  263. inputField.name = field.name.value
  264. inputField.description = await helpers.getDescription(field.description.value)
  265. inputField.type = helpers.getType(field)
  266. inputField.id = helpers.getId(inputField.type)
  267. inputField.kind = helpers.getTypeKind(inputField.type, schema)
  268. inputField.href = helpers.getFullLink(inputField.kind, inputField.id)
  269. inputField.isDeprecated = helpers.getDeprecationStatus(field.directives)
  270. inputField.deprecationReason = await helpers.getDeprecationReason(field.directives, inputField)
  271. inputField.preview = await helpers.getPreview(field.directives, inputField, previewsPerVersion)
  272. inputFields.push(inputField)
  273. }))
  274. }
  275. inputObject.inputFields = sortBy(inputFields, 'name')
  276. data.inputObjects.push(inputObject)
  277. return
  278. }
  279. // SCALARS
  280. if (def.kind === 'ScalarTypeDefinition') {
  281. const scalar = {}
  282. scalar.name = def.name.value
  283. scalar.kind = helpers.getKind(def.kind)
  284. scalar.id = helpers.getId(scalar.name)
  285. scalar.href = helpers.getFullLink('scalars', scalar.id)
  286. scalar.description = await helpers.getDescription(def.description.value)
  287. scalar.isDeprecated = helpers.getDeprecationStatus(def.directives)
  288. scalar.deprecationReason = await helpers.getDeprecationReason(def.directives, scalar)
  289. scalar.preview = await helpers.getPreview(def.directives, scalar, previewsPerVersion)
  290. data.scalars.push(scalar)
  291. }
  292. }))
  293. // add non-schema scalars and sort all scalars alphabetically
  294. data.scalars = sortBy(data.scalars.concat(externalScalars), 'name')
  295. // sort all the types alphabetically
  296. data.queries.connections = sortBy(data.queries.connections, 'name')
  297. data.queries.fields = sortBy(data.queries.fields, 'name')
  298. data.mutations = sortBy(data.mutations, 'name')
  299. data.objects = sortBy(data.objects, 'name')
  300. data.interfaces = sortBy(data.interfaces, 'name')
  301. data.enums = sortBy(data.enums, 'name')
  302. data.unions = sortBy(data.unions, 'name')
  303. data.inputObjects = sortBy(data.inputObjects, 'name')
  304. data.scalars = sortBy(data.scalars, 'name')
  305. return data
  306. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...