UNPKG

@onn-software/ddl-to-gql

Version:

Convert a SQL DDL to a GraphQL implementation with all relations.

94 lines (93 loc) 4.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaGenerator = void 0; const globals_1 = require("../globals"); const schema_generator_templates_1 = require("./schema-generator.templates"); class SchemaGenerator { execute(tableDefs, gqlNoRoot = false, gqlNoMutations = false) { const gqlSchema = tableDefs.map((table) => this.generateGqlSchema(table, gqlNoMutations)); const gqlQueries = this.generateGqlSchemaQueries(tableDefs, gqlNoRoot); const gqlMutations = gqlNoMutations ? [] : this.generateGqlSchemaMutations(tableDefs, gqlNoRoot); return schema_generator_templates_1.baseGql + gqlSchema.join('\n') + gqlQueries.join('\n') + gqlMutations.join('\n'); } generateGqlSchema(tableDef, gqlNoMutations) { const interfaceName = globals_1.Globals.getGqlName(tableDef.tableName); const queryTypes = this.generateQueryTypes(interfaceName, tableDef); const upsertTypes = gqlNoMutations ? [] : this.generateUpsertTypes(interfaceName, tableDef); return [...queryTypes, ...upsertTypes].join('\n'); } generateUpsertTypes(interfaceName, tableDef) { const schema = [`input ${interfaceName}_upsert {`]; tableDef.columns.forEach((c) => { schema.push(` ${c.key}: ${schema_generator_templates_1.GqlTypeMap[c.sqlType.split('(')[0].toUpperCase()] ?? 'String'}`); }); schema.push('}'); schema.push(''); return schema; } generateQueryTypes(interfaceName, tableDef) { const schema = [`type ${interfaceName} {`]; tableDef.columns.forEach((c) => { schema.push(` ${c.key}: ${schema_generator_templates_1.GqlTypeMap[c.sqlType.split('(')[0].toUpperCase()] ?? 'String'}${c.nullable ? '' : '!'}`); }); if (tableDef.relations.length > 0) { schema.push(''); schema.push(' # Relations'); tableDef.relations.forEach((r) => { const type = globals_1.Globals.getGqlName(r.to.table); if (r.many) { const key = globals_1.Globals.composeToRelationKey(r); schema.push(` ${key}(paginate: Paginate, orderBy: OrderBy, where: [WhereClause!], distinct: [String!]): Paginated${type}!`); } else { const key = globals_1.Globals.composeToRelationKey(r); schema.push(` ${key}: ${type}`); } }); } schema.push('}'); schema.push(''); schema.push(`type Paginated${interfaceName} {`); schema.push(' pageIndex: Int!'); schema.push(' pageSize: Int!'); schema.push(' totalEntries: Int!'); schema.push(` data: [${interfaceName}!]!`); schema.push('}'); schema.push(''); return schema; } generateGqlSchemaQueries(tableDefs, gqlNoRoot) { const queries = ['']; if (gqlNoRoot) { queries.push(`type ${globals_1.Globals.GQL_PREFIX}Query {`); } else { queries.push('extend type Query {'); } tableDefs.forEach(table => { queries.push(` ${table.tableName}(paginate: Paginate, orderBy: OrderBy, where: [WhereClause!], distinct: [String!]): Paginated${globals_1.Globals.getGqlName(table.tableName)}!`); }); queries.push('}'); queries.push(''); return queries; } generateGqlSchemaMutations(tableDefs, gqlNoRoot) { const mutations = ['']; if (gqlNoRoot) { mutations.push(`type ${globals_1.Globals.GQL_PREFIX}Mutation {`); } else { mutations.push('extend type Mutation {'); } tableDefs.forEach(table => { mutations.push(` action_${table.tableName}(action: MutationAction!, where: [WhereClause!]!, value: ${globals_1.Globals.getGqlName(table.tableName)}_upsert!): MutationResult!`); mutations.push(` insert_${table.tableName}(value: ${globals_1.Globals.getGqlName(table.tableName)}_upsert!): MutationResult!`); mutations.push(` update_${table.tableName}(where: [WhereClause!]!, value: ${globals_1.Globals.getGqlName(table.tableName)}_upsert!): MutationResult!`); mutations.push(` delete_${table.tableName}(where: [WhereClause!]!): MutationResult!`); }); mutations.push('}'); mutations.push(''); return mutations; } } exports.SchemaGenerator = SchemaGenerator;