@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
131 lines • 4.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const base_command_1 = tslib_1.__importDefault(require("../../base-command"));
const config_1 = require("../../helpers/config");
const git_1 = require("../../helpers/git");
const schema_1 = require("../../helpers/schema");
const validation_1 = require("../../helpers/validation");
class SchemaCheck extends base_command_1.default {
async run() {
try {
const { flags, args } = await this.parse(SchemaCheck);
await this.require(flags);
const service = this.maybe('service', flags);
const forceSafe = this.maybe('forceSafe', flags);
const usesGitHubApp = this.maybe('github', flags) === true;
const registry = this.ensure({
key: 'registry',
args: flags,
defaultValue: config_1.graphqlEndpoint,
env: 'HIVE_REGISTRY',
});
const file = args.file;
const token = this.ensure({
key: 'token',
args: flags,
env: 'HIVE_TOKEN',
});
const sdl = await (0, schema_1.loadSchema)(file);
const git = await (0, git_1.gitInfo)(() => {
// noop
});
const commit = git.commit;
(0, validation_1.invariant)(typeof sdl === 'string' && sdl.length > 0, 'Schema seems empty');
if (usesGitHubApp) {
(0, validation_1.invariant)(typeof commit === 'string', `Couldn't resolve commit sha required for GitHub Application`);
}
const result = await this.registryApi(registry, token).schemaCheck({
input: {
service,
sdl: (0, schema_1.minifySchema)(sdl),
github: usesGitHubApp
? {
commit: commit,
}
: null,
},
usesGitHubApp,
});
if (result.schemaCheck.__typename === 'SchemaCheckSuccess') {
const changes = result.schemaCheck.changes;
if (result.schemaCheck.initial) {
this.success('Schema registry is empty, nothing to compare your schema with.');
}
else if (!(changes === null || changes === void 0 ? void 0 : changes.total)) {
this.success('No changes');
}
else {
schema_1.renderChanges.call(this, changes);
this.log('');
}
}
else if (result.schemaCheck.__typename === 'SchemaCheckError') {
const changes = result.schemaCheck.changes;
const errors = result.schemaCheck.errors;
schema_1.renderErrors.call(this, errors);
if (changes && changes.total) {
this.log('');
schema_1.renderChanges.call(this, changes);
}
this.log('');
if (forceSafe) {
this.success('Breaking changes were expected (forced)');
}
else {
this.exit(1);
}
}
else if (result.schemaCheck.__typename === 'GitHubSchemaCheckSuccess') {
this.success(result.schemaCheck.message);
}
else {
this.error(result.schemaCheck.message);
}
}
catch (error) {
if (error instanceof core_1.Errors.ExitError) {
throw error;
}
else {
this.fail('Failed to check schema');
this.handleFetchError(error);
}
}
}
}
SchemaCheck.description = 'checks schema';
SchemaCheck.flags = {
service: core_1.Flags.string({
description: 'service name (only for distributed schemas)',
}),
registry: core_1.Flags.string({
description: 'registry address',
}),
token: core_1.Flags.string({
description: 'api token',
}),
forceSafe: core_1.Flags.boolean({
description: 'mark the check as safe, breaking changes are expected',
}),
github: core_1.Flags.boolean({
description: 'Connect with GitHub Application',
default: false,
}),
require: core_1.Flags.string({
description: 'Loads specific require.extensions before running the codegen and reading the configuration',
default: [],
multiple: true,
}),
};
SchemaCheck.args = [
{
name: 'file',
required: true,
description: 'Path to the schema file(s)',
hidden: false,
},
];
exports.default = SchemaCheck;
//# sourceMappingURL=check.js.map