( types: ReadonlyMap<string, IntrospectionType>, )
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | const buildDefinitions = ( |
| 37 | types: ReadonlyMap<string, IntrospectionType>, |
| 38 | ): Record<string, unknown> => { |
| 39 | const defs: Record<string, unknown> = {}; |
| 40 | |
| 41 | for (const [name, type] of types) { |
| 42 | // Skip internal types |
| 43 | if (name.startsWith("__")) continue; |
| 44 | |
| 45 | if (type.kind === "INPUT_OBJECT" && type.inputFields) { |
| 46 | const properties: Record<string, unknown> = {}; |
| 47 | const required: string[] = []; |
| 48 | |
| 49 | for (const field of type.inputFields) { |
| 50 | const schema = typeRefToJsonSchema(field.type, types); |
| 51 | if (field.description) { |
| 52 | (schema as Record<string, unknown>).description = field.description; |
| 53 | } |
| 54 | properties[field.name] = schema; |
| 55 | if (isNonNull(field.type)) { |
| 56 | required.push(field.name); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | const def: Record<string, unknown> = { type: "object", properties }; |
| 61 | if (required.length > 0) def.required = required; |
| 62 | if (type.description) def.description = type.description; |
| 63 | defs[name] = def; |
| 64 | } |
| 65 | |
| 66 | if (type.kind === "ENUM" && type.enumValues) { |
| 67 | defs[name] = { |
| 68 | type: "string", |
| 69 | enum: type.enumValues.map((v) => v.name), |
| 70 | ...(type.description ? { description: type.description } : {}), |
| 71 | }; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return defs; |
| 76 | }; |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | // Convert a type ref to JSON Schema using $ref for complex types |
no test coverage detected