@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
83 lines • 3.18 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const graphql_1 = require("graphql");
const core_1 = require("@oclif/core");
const base_command_1 = tslib_1.__importDefault(require("../base-command"));
const errors_1 = require("../helpers/errors");
const schema_1 = require("../helpers/schema");
class Introspect extends base_command_1.default {
async run() {
var _a;
const { flags, args } = await this.parse(Introspect);
const headers = (_a = flags.header) === null || _a === void 0 ? void 0 : _a.reduce((acc, header) => {
const [key, ...values] = header.split(':');
return Object.assign(Object.assign({}, acc), { [key]: values.join(':') });
}, {});
const schema = await (0, schema_1.loadSchema)(args.location, {
headers,
method: 'POST',
}).catch(err => {
throw new errors_1.APIError(err);
});
if (!schema) {
throw new errors_1.UnexpectedError('Unable to load schema');
}
if (!flags.write) {
this.log(schema);
return;
}
if (flags.write) {
const filepath = (0, node_path_1.resolve)(process.cwd(), flags.write);
switch ((0, node_path_1.extname)(flags.write.toLowerCase())) {
case '.graphql':
case '.gql':
case '.gqls':
case '.graphqls':
(0, node_fs_1.writeFileSync)(filepath, schema, 'utf8');
break;
case '.json': {
const schemaObject = (0, graphql_1.buildSchema)(schema, {
assumeValidSDL: true,
assumeValid: true,
});
(0, node_fs_1.writeFileSync)(filepath, JSON.stringify((0, graphql_1.introspectionFromSchema)(schemaObject), null, 2), 'utf8');
break;
}
default:
throw new errors_1.UnsupportedFileExtensionError(flags.write, [
'.graphql',
'.gql',
'.gqls',
'.graphqls',
'.json',
]);
}
this.logSuccess(`Saved to ${filepath}`);
}
}
}
Introspect.description = 'introspects a GraphQL Schema';
Introspect.flags = {
write: core_1.Flags.string({
aliases: ['W'],
description: 'Write to a file (possible extensions: .graphql, .gql, .gqls, .graphqls, .json)',
}),
header: core_1.Flags.string({
aliases: ['H'],
description: 'HTTP header to add to the introspection request (in key:value format)',
multiple: true,
}),
};
Introspect.args = {
location: core_1.Args.string({
name: 'location',
required: true,
description: 'GraphQL Schema location (URL or file path/glob)',
hidden: false,
}),
};
exports.default = Introspect;
//# sourceMappingURL=introspect.js.map
;