UNPKG

pg-proto-parser

Version:
73 lines (72 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RuntimeSchemaGenerator = void 0; const protobufjs_1 = require("@launchql/protobufjs"); class RuntimeSchemaGenerator { root; nodeTypes = new Set(); constructor(root) { this.root = root; this.extractNodeTypes(); } extractNodeTypes() { const nodeType = this.root.lookupType('Node'); if (nodeType && nodeType.oneofs && nodeType.oneofs.node) { const oneof = nodeType.oneofs.node; for (const fieldName of oneof.fieldsArray.map(f => f.name)) { const field = nodeType.fields[fieldName]; if (field && field.type) { this.nodeTypes.add(field.type); } } } } generateNodeSpecs() { const nodeSpecs = []; const pgQueryNamespace = this.root.nested?.pg_query; if (pgQueryNamespace && pgQueryNamespace instanceof protobufjs_1.Namespace && pgQueryNamespace.nestedArray) { pgQueryNamespace.nestedArray.forEach((nested) => { if (nested instanceof protobufjs_1.Type && nested.name !== 'Node') { const nodeSpec = this.createNodeSpec(nested); if (nodeSpec) { nodeSpecs.push(nodeSpec); } } }); } return nodeSpecs.sort((a, b) => a.name.localeCompare(b.name)); } createNodeSpec(type) { const fields = []; Object.values(type.fields).forEach((field) => { const fieldSpec = this.createFieldSpec(field); if (fieldSpec) { fields.push(fieldSpec); } }); return { name: type.name, isNode: this.nodeTypes.has(type.name), fields: fields.sort((a, b) => a.name.localeCompare(b.name)) }; } createFieldSpec(field) { const fieldName = field.name; const isArray = field.repeated || false; const optional = !field.required; const fieldType = field.type; return { name: fieldName, type: fieldType, isArray, optional }; } getNodeTypes() { return Array.from(this.nodeTypes).sort(); } getNodeTypesCount() { return this.nodeTypes.size; } } exports.RuntimeSchemaGenerator = RuntimeSchemaGenerator;