UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

68 lines (67 loc) 2.54 kB
import { __awaiter } from "tslib"; import { createServer } from 'http'; import { createYoga } from 'graphql-yoga'; import open from 'open'; import { createCommand, parseGlobalArgs, } from '@graphql-inspector/commands'; import { Logger } from '@graphql-inspector/logger'; import { fake } from './fake.js'; export default createCommand(api => { const { loaders } = api; return { command: 'serve <schema>', describe: 'Runs server with fake data based on schema', builder(yargs) { return yargs .positional('schema', { describe: 'Point to a schema', type: 'string', demandOption: true, }) .options({ port: { alias: 'p', describe: 'Port', type: 'number', default: 4000, }, }); }, handler(args) { var _a; return __awaiter(this, void 0, void 0, function* () { const { headers, token } = parseGlobalArgs(args); const apolloFederation = args.federation || false; const aws = args.aws || false; const method = ((_a = args.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'POST'; const schema = yield loaders.loadSchema(args.schema, { headers, token, method, }, apolloFederation, aws); const port = args.port; try { fake(schema); const yoga = createYoga({ schema, logging: false, }); const server = createServer(yoga); yield new Promise(resolve => server.listen(port, () => resolve())); const url = `http://localhost:${port}/graphql`; Logger.success(`GraphQL API: ${url}`); yield open(url); const shutdown = () => { server.close(() => { process.exit(0); }); }; process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); } catch (e) { Logger.error(e.message || e); } }); }, }; });