UNPKG

@launchql/cli

Version:
74 lines (71 loc) 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const server_1 = require("@launchql/server"); const usage = ` LaunchQL Get GraphQL Schema: lql get-graphql-schema [OPTIONS] Options: --help, -h Show this help message --database <name> Database name (default: launchql) --schemas <list> Comma-separated schemas to include --endpoint <url> GraphQL endpoint to fetch schema via introspection --headerHost <host> Optional Host header to send with endpoint requests --auth <token> Optional Authorization header value (e.g., "Bearer 123") --header "Name: Value" Optional HTTP header; repeat to add multiple headers --out <path> Output file path (default: print to stdout) `; const defaultSchemas = [ 'collections_public', 'dashboard_public' ]; exports.default = async (argv, prompter, _options) => { if (argv.help || argv.h) { console.log(usage); process.exit(0); } const endpoint = argv.endpoint ?? ''; const headerHost = argv.headerHost ?? ''; const auth = argv.auth ?? ''; const database = argv.database ?? 'launchql'; const schemasArg = argv.schemas ?? defaultSchemas.join(','); const out = argv.out ?? ''; const headerArg = argv.header; const headerList = Array.isArray(headerArg) ? headerArg : headerArg ? [headerArg] : []; const headers = {}; for (const h of headerList) { const idx = typeof h === 'string' ? h.indexOf(':') : -1; if (idx <= 0) continue; const name = h.slice(0, idx).trim(); const value = h.slice(idx + 1).trim(); if (!name) continue; headers[name] = value; } const schemas = schemasArg.split(',').map(s => s.trim()).filter(Boolean); let sdl; if (endpoint) { const opts = {}; if (headerHost) opts.headerHost = headerHost; if (auth) opts.auth = auth; if (Object.keys(headers).length) opts.headers = headers; sdl = await server_1.fetchEndpointSchemaSDL(endpoint, opts); } else { // The server package already depends on postgraphile and graphql, // and exporting a reusable programmatic builder from there // avoids adding new dependencies to cli and prevents duplication. sdl = await (0, server_1.buildSchemaSDL)({ database, schemas }); } if (out) { await fs_1.promises.writeFile(out, sdl, 'utf8'); console.log(`Wrote schema SDL to ${out}`); } else { process.stdout.write(sdl + '\n'); } };