@graphql-inspector/action
Version:
GraphQL Inspector functionality for GitHub Actions
67 lines (66 loc) • 2.48 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const http_1 = require("http");
const graphql_yoga_1 = require("graphql-yoga");
const open_1 = tslib_1.__importDefault(require("open"));
const commands_1 = require("@graphql-inspector/commands");
const logger_1 = require("@graphql-inspector/logger");
const fake_js_1 = require("./fake.js");
exports.default = (0, commands_1.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,
},
});
},
async handler(args) {
const { headers, token } = (0, commands_1.parseGlobalArgs)(args);
const apolloFederation = args.federation || false;
const aws = args.aws || false;
const method = args.method?.toUpperCase() || 'POST';
const schema = await loaders.loadSchema(args.schema, {
headers,
token,
method,
}, apolloFederation, aws);
const port = args.port;
try {
(0, fake_js_1.fake)(schema);
const yoga = (0, graphql_yoga_1.createYoga)({
schema,
logging: false,
});
const server = (0, http_1.createServer)(yoga);
await new Promise(resolve => server.listen(port, () => resolve()));
const url = `http://localhost:${port}/graphql`;
logger_1.Logger.success(`GraphQL API: ${url}`);
await (0, open_1.default)(url);
const shutdown = () => {
server.close(() => {
process.exit(0);
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
}
catch (e) {
logger_1.Logger.error(e.message || e);
}
},
};
});
;