@sanity/codegen 3.88.1-typegen-experimental.0 → 3.88.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.mts +85 -282
- package/lib/index.d.ts +85 -282
- package/lib/index.js +172 -537
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +173 -538
- package/lib/index.mjs.map +1 -1
- package/package.json +17 -15
- package/src/_exports/index.ts +3 -14
- package/src/readConfig.ts +23 -71
- package/src/readSchema.ts +1 -0
- package/src/typescript/expressionResolvers.ts +3 -4
- package/src/typescript/findQueriesInPath.ts +2 -9
- package/src/typescript/findQueriesInSource.ts +6 -22
- package/src/typescript/typeGenerator.ts +306 -445
- package/src/typescript/asyncQueue.ts +0 -100
- package/src/typescript/schemaTypeGenerator.ts +0 -381
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Implements an asynchronous queue pattern. Allows enqueueing items,
|
|
3
|
-
* consuming them via an async iterator, and signaling completion or errors.
|
|
4
|
-
*/
|
|
5
|
-
export class AsyncQueue<T> {
|
|
6
|
-
// Internal state:
|
|
7
|
-
// `queue`: Holds items yet to be consumed.
|
|
8
|
-
// `resume`: A callback to signal the consumer when new items arrive or the queue finishes/errors.
|
|
9
|
-
// `finished`: Flag indicating if the producer is done adding items (via `finish` or `throwError`).
|
|
10
|
-
// `pendingError`: Stores an error passed to `throwError`, to be thrown by the consumer.
|
|
11
|
-
private queue: T[] = []
|
|
12
|
-
private resume: (() => void) | null = null
|
|
13
|
-
private finished = false
|
|
14
|
-
private pendingError: unknown = null
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Enqueues an item. Throws if `finish` or `throwError` has been called.
|
|
18
|
-
*/
|
|
19
|
-
public enqueue(item: T): void {
|
|
20
|
-
if (this.finished) {
|
|
21
|
-
throw new Error('Cannot enqueue items to a finished AsyncQueue.')
|
|
22
|
-
}
|
|
23
|
-
this.queue.push(item)
|
|
24
|
-
// If the iterator is waiting, resume it.
|
|
25
|
-
if (this.resume) {
|
|
26
|
-
this.resume()
|
|
27
|
-
this.resume = null
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Signals that no more items will be enqueued. Idempotent.
|
|
33
|
-
*/
|
|
34
|
-
public finish(): void {
|
|
35
|
-
if (!this.finished) {
|
|
36
|
-
this.finished = true
|
|
37
|
-
// If the iterator is waiting, resume it.
|
|
38
|
-
if (this.resume) {
|
|
39
|
-
this.resume()
|
|
40
|
-
this.resume = null
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Signals an error occurred during production. Finishes the queue. Idempotent.
|
|
47
|
-
*/
|
|
48
|
-
public throwError(error: unknown): void {
|
|
49
|
-
if (!this.finished) {
|
|
50
|
-
this.pendingError = error
|
|
51
|
-
this.finished = true
|
|
52
|
-
// If the iterator is waiting, resume it.
|
|
53
|
-
if (this.resume) {
|
|
54
|
-
this.resume()
|
|
55
|
-
this.resume = null
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Async iterator to consume enqueued items.
|
|
62
|
-
* Throws if `throwError` was called.
|
|
63
|
-
*/
|
|
64
|
-
public async *[Symbol.asyncIterator](): AsyncIterableIterator<T> {
|
|
65
|
-
while (true) {
|
|
66
|
-
// Throw immediately if an error occurred before or during waiting.
|
|
67
|
-
if (this.pendingError) {
|
|
68
|
-
throw this.pendingError
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Wait for new items if the queue is empty and not finished.
|
|
72
|
-
if (this.queue.length === 0 && !this.finished) {
|
|
73
|
-
await new Promise<void>((resolve) => {
|
|
74
|
-
this.resume = resolve
|
|
75
|
-
})
|
|
76
|
-
// Re-check for errors after resuming, as `throwError` could have been called
|
|
77
|
-
// while we were waiting.
|
|
78
|
-
if (this.pendingError) {
|
|
79
|
-
throw this.pendingError
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Yield available items.
|
|
84
|
-
while (this.queue.length > 0) {
|
|
85
|
-
// `!` is safe because `this.queue.length > 0`.
|
|
86
|
-
yield this.queue.shift()!
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// If finished and queue is empty, the loop is done.
|
|
90
|
-
if (this.finished && this.queue.length === 0) {
|
|
91
|
-
break
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
// Final check for errors, in case `throwError` was called after the last item
|
|
95
|
-
// was yielded but before the loop condition was checked again.
|
|
96
|
-
if (this.pendingError) {
|
|
97
|
-
throw this.pendingError
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
import * as t from '@babel/types'
|
|
2
|
-
import {
|
|
3
|
-
type ArrayTypeNode,
|
|
4
|
-
type DocumentSchemaType,
|
|
5
|
-
type InlineTypeNode,
|
|
6
|
-
type ObjectAttribute,
|
|
7
|
-
type ObjectTypeNode,
|
|
8
|
-
type SchemaType,
|
|
9
|
-
type TypeDeclarationSchemaType,
|
|
10
|
-
typeEvaluate,
|
|
11
|
-
type TypeNode,
|
|
12
|
-
type UnionTypeNode,
|
|
13
|
-
} from 'groq-js'
|
|
14
|
-
|
|
15
|
-
import {safeParseQuery} from '../safeParseQuery'
|
|
16
|
-
|
|
17
|
-
export interface TypeEvaluationStats {
|
|
18
|
-
allTypes: number
|
|
19
|
-
unknownTypes: number
|
|
20
|
-
emptyUnions: number
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface SchemaTypeGeneratorOptions {
|
|
24
|
-
schema: SchemaType
|
|
25
|
-
schemaId: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class SchemaTypeGenerator {
|
|
29
|
-
static ProjectionBase = t.identifier('ProjectionBase')
|
|
30
|
-
static InternalReferenceSymbol = Object.assign(t.identifier('internalGroqTypeReferenceTo'), {
|
|
31
|
-
typeAnnotation: t.tsTypeAnnotation(
|
|
32
|
-
Object.assign(t.tsTypeOperator(t.tsSymbolKeyword()), {operator: 'unique'}),
|
|
33
|
-
),
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
static sanitizeIdentifier(input: string): string {
|
|
37
|
-
return `${input.replace(/^\d/, '_').replace(/[^$\w]+(.)/g, (_, char) => char.toUpperCase())}`
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static normalizeIdentifier(input: string): string {
|
|
41
|
-
const sanitized = SchemaTypeGenerator.sanitizeIdentifier(input)
|
|
42
|
-
return `${sanitized.charAt(0).toUpperCase()}${sanitized.slice(1)}`
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public readonly schema: SchemaType
|
|
46
|
-
public readonly schemaId: string
|
|
47
|
-
|
|
48
|
-
private typeNames = new Set<string>()
|
|
49
|
-
private tsTypes = new Map<string, t.TSType>()
|
|
50
|
-
private identifiers = new Map<string, t.Identifier>()
|
|
51
|
-
|
|
52
|
-
constructor({schema, schemaId}: SchemaTypeGeneratorOptions) {
|
|
53
|
-
this.schemaId = schemaId
|
|
54
|
-
this.schema = schema
|
|
55
|
-
|
|
56
|
-
for (const type of schema) {
|
|
57
|
-
if (this.typeNames.has(type.name)) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`Duplicate type name "${type.name}" in schema "${schemaId}". Type names must be unique within the same schema.`,
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
this.typeNames.add(type.name)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const uniqueIdentifiers = new Set<string>()
|
|
66
|
-
for (const type of schema) {
|
|
67
|
-
const desiredName = SchemaTypeGenerator.normalizeIdentifier(type.name)
|
|
68
|
-
let resultingName = desiredName
|
|
69
|
-
|
|
70
|
-
let index = 2
|
|
71
|
-
while (uniqueIdentifiers.has(resultingName)) {
|
|
72
|
-
resultingName = `${desiredName}_${index}`
|
|
73
|
-
index++
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
uniqueIdentifiers.add(resultingName)
|
|
77
|
-
this.identifiers.set(type.name, t.identifier(resultingName))
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
for (const type of schema) {
|
|
81
|
-
this.tsTypes.set(type.name, this.generateTsType(type))
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private shouldOmitProjection({rest, attributes}: ObjectTypeNode) {
|
|
86
|
-
if (rest?.type === 'unknown') return true
|
|
87
|
-
if (rest?.type === 'object' && this.shouldOmitProjection(rest)) return true
|
|
88
|
-
return Object.values(attributes).every((attr) => attr.value.type === 'null')
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private generateTsType(
|
|
92
|
-
typeNode: TypeNode | TypeDeclarationSchemaType | DocumentSchemaType,
|
|
93
|
-
): t.TSType {
|
|
94
|
-
switch (typeNode.type) {
|
|
95
|
-
case 'string': {
|
|
96
|
-
if (typeNode.value !== undefined) {
|
|
97
|
-
return t.tsLiteralType(t.stringLiteral(typeNode.value))
|
|
98
|
-
}
|
|
99
|
-
return t.tsStringKeyword()
|
|
100
|
-
}
|
|
101
|
-
case 'number': {
|
|
102
|
-
if (typeNode.value !== undefined) {
|
|
103
|
-
return t.tsLiteralType(t.numericLiteral(typeNode.value))
|
|
104
|
-
}
|
|
105
|
-
return t.tsNumberKeyword()
|
|
106
|
-
}
|
|
107
|
-
case 'boolean': {
|
|
108
|
-
if (typeNode.value !== undefined) {
|
|
109
|
-
return t.tsLiteralType(t.booleanLiteral(typeNode.value))
|
|
110
|
-
}
|
|
111
|
-
return t.tsBooleanKeyword()
|
|
112
|
-
}
|
|
113
|
-
case 'unknown': {
|
|
114
|
-
return t.tsUnknownKeyword()
|
|
115
|
-
}
|
|
116
|
-
case 'document': {
|
|
117
|
-
return this.generateDocumentTsType(typeNode)
|
|
118
|
-
}
|
|
119
|
-
case 'type': {
|
|
120
|
-
return this.generateTsType(typeNode.value)
|
|
121
|
-
}
|
|
122
|
-
case 'array': {
|
|
123
|
-
return this.generateArrayTsType(typeNode)
|
|
124
|
-
}
|
|
125
|
-
case 'object': {
|
|
126
|
-
return this.generateObjectTsType(typeNode)
|
|
127
|
-
}
|
|
128
|
-
case 'union': {
|
|
129
|
-
return this.generateUnionTsType(typeNode)
|
|
130
|
-
}
|
|
131
|
-
case 'inline': {
|
|
132
|
-
return this.generateInlineTsType(typeNode)
|
|
133
|
-
}
|
|
134
|
-
case 'null': {
|
|
135
|
-
return t.tsNullKeyword()
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
default: {
|
|
139
|
-
throw new Error(
|
|
140
|
-
`Encountered unsupported node type "${
|
|
141
|
-
// @ts-expect-error This should never happen
|
|
142
|
-
typeNode.type
|
|
143
|
-
}" while generating schema types for schema "${this.schemaId}"`,
|
|
144
|
-
)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Helper function used to generate TS types for array type nodes.
|
|
150
|
-
private generateArrayTsType(typeNode: ArrayTypeNode): t.TSTypeReference {
|
|
151
|
-
const typeNodes = this.generateTsType(typeNode.of)
|
|
152
|
-
return t.tsTypeReference(t.identifier('Array'), t.tsTypeParameterInstantiation([typeNodes]))
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Helper function used to generate TS types for object properties.
|
|
156
|
-
private generateTsObjectProperty(key: string, attribute: ObjectAttribute): t.TSPropertySignature {
|
|
157
|
-
const type = this.generateTsType(attribute.value)
|
|
158
|
-
const propertySignature = t.tsPropertySignature(
|
|
159
|
-
t.identifier(SchemaTypeGenerator.sanitizeIdentifier(key)),
|
|
160
|
-
t.tsTypeAnnotation(type),
|
|
161
|
-
)
|
|
162
|
-
propertySignature.optional = attribute.optional
|
|
163
|
-
|
|
164
|
-
return propertySignature
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Helper function used to generate TS types for object type nodes.
|
|
168
|
-
private generateObjectTsType(typeNode: ObjectTypeNode): t.TSType {
|
|
169
|
-
const props: t.TSPropertySignature[] = []
|
|
170
|
-
Object.entries(typeNode.attributes).forEach(([key, attribute]) => {
|
|
171
|
-
props.push(this.generateTsObjectProperty(key, attribute))
|
|
172
|
-
})
|
|
173
|
-
const rest = typeNode.rest
|
|
174
|
-
|
|
175
|
-
if (rest) {
|
|
176
|
-
switch (rest.type) {
|
|
177
|
-
case 'unknown': {
|
|
178
|
-
return t.tsUnknownKeyword()
|
|
179
|
-
}
|
|
180
|
-
case 'object': {
|
|
181
|
-
Object.entries(rest.attributes).forEach(([key, attribute]) => {
|
|
182
|
-
props.push(this.generateTsObjectProperty(key, attribute))
|
|
183
|
-
})
|
|
184
|
-
break
|
|
185
|
-
}
|
|
186
|
-
case 'inline': {
|
|
187
|
-
const resolved = this.generateInlineTsType(rest)
|
|
188
|
-
// if object rest is unknown, we can't generate a type literal for it
|
|
189
|
-
if (t.isTSUnknownKeyword(resolved)) return resolved
|
|
190
|
-
return t.tsIntersectionType([t.tsTypeLiteral(props), resolved])
|
|
191
|
-
}
|
|
192
|
-
default: {
|
|
193
|
-
// @ts-expect-error This should never happen
|
|
194
|
-
throw new Error(`Type "${rest.type}" not found in schema`)
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (typeNode.dereferencesTo) {
|
|
200
|
-
const derefType = Object.assign(
|
|
201
|
-
t.tsPropertySignature(
|
|
202
|
-
SchemaTypeGenerator.InternalReferenceSymbol,
|
|
203
|
-
t.tsTypeAnnotation(t.tsLiteralType(t.stringLiteral(typeNode.dereferencesTo))),
|
|
204
|
-
),
|
|
205
|
-
{computed: true, optional: true},
|
|
206
|
-
)
|
|
207
|
-
props.push(derefType)
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return t.tsTypeLiteral(props)
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private generateInlineTsType(typeNode: InlineTypeNode): t.TSType {
|
|
214
|
-
const identifier = this.getIdentifier(typeNode.name)
|
|
215
|
-
if (!identifier) {
|
|
216
|
-
// Not found in schema, return unknown type
|
|
217
|
-
return t.addComment(
|
|
218
|
-
t.tsUnknownKeyword(),
|
|
219
|
-
'trailing',
|
|
220
|
-
` Unable to locate the referenced type "${typeNode.name}" in schema "${this.schemaId}"`,
|
|
221
|
-
true,
|
|
222
|
-
)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return t.tsTypeReference(identifier)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Helper function used to generate TS types for union type nodes.
|
|
229
|
-
private generateUnionTsType(typeNode: UnionTypeNode): t.TSType {
|
|
230
|
-
if (typeNode.of.length === 0) return t.tsNeverKeyword()
|
|
231
|
-
if (typeNode.of.length === 1) return this.generateTsType(typeNode.of[0])
|
|
232
|
-
return t.tsUnionType(typeNode.of.map((node) => this.generateTsType(node)))
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// Helper function used to generate TS types for document type nodes.
|
|
236
|
-
private generateDocumentTsType(document: DocumentSchemaType): t.TSType {
|
|
237
|
-
const props = Object.entries(document.attributes).map(([key, node]) =>
|
|
238
|
-
this.generateTsObjectProperty(key, node),
|
|
239
|
-
)
|
|
240
|
-
|
|
241
|
-
return t.tsTypeLiteral(props)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
getTypeNames(): string[] {
|
|
245
|
-
return Array.from(this.typeNames)
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
getTsType(typeName: string): t.TSType | undefined {
|
|
249
|
-
return this.tsTypes.get(typeName)
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
getIdentifier(typeName: string): t.Identifier | undefined {
|
|
253
|
-
return this.identifiers.get(typeName)
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
evaluateQuery(query: string): {tsType: t.TSType; stats: TypeEvaluationStats} {
|
|
257
|
-
const ast = safeParseQuery(query)
|
|
258
|
-
const typeNode = typeEvaluate(ast, this.schema)
|
|
259
|
-
const tsType = this.generateTsType(typeNode)
|
|
260
|
-
const stats = walkAndCountQueryTypeNodeStats(typeNode)
|
|
261
|
-
return {tsType, stats}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
evaluateProjection(projection: string): {tsType: t.TSType; stats: TypeEvaluationStats} | null {
|
|
265
|
-
const results: {tsType: t.TSType; stats: TypeEvaluationStats}[] = []
|
|
266
|
-
|
|
267
|
-
for (const schemaType of this.schema) {
|
|
268
|
-
// only document types are supported for projections
|
|
269
|
-
if (schemaType.type !== 'document') continue
|
|
270
|
-
|
|
271
|
-
const projectionAst = safeParseQuery(projection)
|
|
272
|
-
if (projectionAst.type !== 'Object') {
|
|
273
|
-
throw new Error(
|
|
274
|
-
`Invalid projection syntax: Projections must be enclosed in curly braces, (e.g., "{_id, title}"). Received: "${projection}"`,
|
|
275
|
-
)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const ast = safeParseQuery(`* [_type==${JSON.stringify(schemaType.name)}] ${projection} [0]`)
|
|
279
|
-
const result = typeEvaluate(ast, this.schema)
|
|
280
|
-
|
|
281
|
-
// Skip results that aren't a union of exactly 2 types (the actual type and null)
|
|
282
|
-
if (result.type !== 'union' || result.of.length !== 2) continue
|
|
283
|
-
|
|
284
|
-
// Find the non-null member of the union (the actual type)
|
|
285
|
-
const projectionResult = result.of.find((node) => node.type !== 'null')
|
|
286
|
-
// Skip members that aren't objects (projections should always be objects)
|
|
287
|
-
if (projectionResult?.type !== 'object') continue
|
|
288
|
-
if (this.shouldOmitProjection(projectionResult)) continue
|
|
289
|
-
|
|
290
|
-
const stats = walkAndCountQueryTypeNodeStats(projectionResult)
|
|
291
|
-
const tsType = t.tsTypeReference(
|
|
292
|
-
SchemaTypeGenerator.ProjectionBase,
|
|
293
|
-
t.tsTypeParameterInstantiation([
|
|
294
|
-
this.generateTsType(projectionResult),
|
|
295
|
-
t.tsLiteralType(t.stringLiteral(schemaType.name)),
|
|
296
|
-
]),
|
|
297
|
-
)
|
|
298
|
-
|
|
299
|
-
results.push({tsType, stats})
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Return `null` if no document types match the projection.
|
|
303
|
-
// Using `null` instead of `never` (empty union) explicitly signals that no
|
|
304
|
-
// types matched, which is expected behavior (e.g., in multi-schema setups).
|
|
305
|
-
// Stats are also omitted when returning `null`.
|
|
306
|
-
if (!results.length) return null
|
|
307
|
-
|
|
308
|
-
if (results.length === 1) return results[0]
|
|
309
|
-
|
|
310
|
-
const combinedTsType = t.tsUnionType(results.map((i) => i.tsType))
|
|
311
|
-
const accumulatedStats = results.reduce(
|
|
312
|
-
(acc, {stats}) => ({
|
|
313
|
-
allTypes: acc.allTypes + stats.allTypes,
|
|
314
|
-
emptyUnions: acc.emptyUnions + stats.emptyUnions,
|
|
315
|
-
unknownTypes: acc.unknownTypes + stats.unknownTypes,
|
|
316
|
-
}),
|
|
317
|
-
{allTypes: 0, unknownTypes: 0, emptyUnions: 0},
|
|
318
|
-
)
|
|
319
|
-
// Add 1 for the final union type since multiple results were combined
|
|
320
|
-
accumulatedStats.allTypes += 1
|
|
321
|
-
|
|
322
|
-
return {tsType: combinedTsType, stats: accumulatedStats}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
export function walkAndCountQueryTypeNodeStats(typeNode: TypeNode): TypeEvaluationStats {
|
|
327
|
-
switch (typeNode.type) {
|
|
328
|
-
case 'unknown': {
|
|
329
|
-
return {allTypes: 1, unknownTypes: 1, emptyUnions: 0}
|
|
330
|
-
}
|
|
331
|
-
case 'array': {
|
|
332
|
-
const acc = walkAndCountQueryTypeNodeStats(typeNode.of)
|
|
333
|
-
acc.allTypes += 1 // count the array type itself
|
|
334
|
-
return acc
|
|
335
|
-
}
|
|
336
|
-
case 'object': {
|
|
337
|
-
// if the rest is unknown, we count it as one unknown type
|
|
338
|
-
if (typeNode.rest && typeNode.rest.type === 'unknown') {
|
|
339
|
-
return {allTypes: 2, unknownTypes: 1, emptyUnions: 0} // count the object type itself as well
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
const restStats = typeNode.rest
|
|
343
|
-
? walkAndCountQueryTypeNodeStats(typeNode.rest)
|
|
344
|
-
: {allTypes: 0, unknownTypes: 0, emptyUnions: 0}
|
|
345
|
-
|
|
346
|
-
// count the object type itself
|
|
347
|
-
restStats.allTypes += 1
|
|
348
|
-
|
|
349
|
-
return Object.values(typeNode.attributes).reduce((acc, attribute) => {
|
|
350
|
-
const {allTypes, unknownTypes, emptyUnions} = walkAndCountQueryTypeNodeStats(
|
|
351
|
-
attribute.value,
|
|
352
|
-
)
|
|
353
|
-
return {
|
|
354
|
-
allTypes: acc.allTypes + allTypes,
|
|
355
|
-
unknownTypes: acc.unknownTypes + unknownTypes,
|
|
356
|
-
emptyUnions: acc.emptyUnions + emptyUnions,
|
|
357
|
-
}
|
|
358
|
-
}, restStats)
|
|
359
|
-
}
|
|
360
|
-
case 'union': {
|
|
361
|
-
if (typeNode.of.length === 0) {
|
|
362
|
-
return {allTypes: 1, unknownTypes: 0, emptyUnions: 1}
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return typeNode.of.reduce(
|
|
366
|
-
(acc, type) => {
|
|
367
|
-
const {allTypes, unknownTypes, emptyUnions} = walkAndCountQueryTypeNodeStats(type)
|
|
368
|
-
return {
|
|
369
|
-
allTypes: acc.allTypes + allTypes,
|
|
370
|
-
unknownTypes: acc.unknownTypes + unknownTypes,
|
|
371
|
-
emptyUnions: acc.emptyUnions + emptyUnions,
|
|
372
|
-
}
|
|
373
|
-
},
|
|
374
|
-
{allTypes: 1, unknownTypes: 0, emptyUnions: 0}, // count the union type itself
|
|
375
|
-
)
|
|
376
|
-
}
|
|
377
|
-
default: {
|
|
378
|
-
return {allTypes: 1, unknownTypes: 0, emptyUnions: 0}
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
}
|