UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

173 lines 6.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const zod_1 = tslib_1.__importDefault(require("zod")); const core_1 = require("@oclif/core"); const base_command_1 = tslib_1.__importDefault(require("../../base-command")); const gql_1 = require("../../gql"); const config_1 = require("../../helpers/config"); const errors_1 = require("../../helpers/errors"); const TargetInput = tslib_1.__importStar(require("../../helpers/target-input")); const CLI_SchemaVersionPromoteMutation = (0, gql_1.graphql)(/* GraphQL */ ` mutation CLI_SchemaVersionPromoteMutation($input: SchemaVersionPromoteInput!) { schemaVersionPromote(input: $input) { ok { newSchemaVersion { id } linkToWebsite } error { message } } } `); class SchemaPromote extends base_command_1.default { async run() { var _a, _b; try { const { flags } = await this.parse(SchemaPromote); let accessToken, endpoint; try { endpoint = this.ensure({ key: 'registry.endpoint', args: flags, legacyFlagName: 'registry', defaultValue: config_1.graphqlEndpoint, env: 'HIVE_REGISTRY', description: SchemaPromote.flags['registry.endpoint'].description, }); } catch (e) { this.logDebug(e); throw new errors_1.MissingEndpointError(); } try { accessToken = this.ensure({ key: 'registry.accessToken', args: flags, legacyFlagName: 'token', env: 'HIVE_TOKEN', description: SchemaPromote.flags['registry.accessToken'].description, }); } catch (e) { this.logDebug(e); throw new errors_1.MissingRegistryTokenError(); } let toTarget = null; if (flags.to) { const result = TargetInput.parse(flags.to); if (result.type === 'error') { throw new errors_1.InvalidTargetError('--to'); } toTarget = result.data; } if (!toTarget) { throw new errors_1.InvalidTargetError('--to'); } let fromTarget = null; if (flags.from) { const result = TargetInput.parse(flags.from); if (result.type === 'error') { throw new errors_1.InvalidTargetError('--from'); } fromTarget = result.data; } let fromSchemaVersionById = null; if (flags.version) { const result = zod_1.default.string().uuid().safeParse(flags.version); if (result.error) { throw new errors_1.InvalidVersionIdError(); } fromSchemaVersionById = result.data; } if (fromTarget && fromSchemaVersionById) { throw new errors_1.ConflictingOptionsError(['--from', '--version']); } let source; if (fromTarget) { source = { fromTarget }; } else if (fromSchemaVersionById) { source = { fromSchemaVersionById }; } else { throw new errors_1.MissingArgumentsError(['--from', (_a = SchemaPromote.flags.from.description) !== null && _a !== void 0 ? _a : ''], ['--version', (_b = SchemaPromote.flags.version.description) !== null && _b !== void 0 ? _b : '']); } const result = await this.registryApi(endpoint, accessToken).request({ operation: CLI_SchemaVersionPromoteMutation, variables: { input: { source, target: { toTarget, }, }, }, }); if (result.schemaVersionPromote.error) { this.logFailure(result.schemaVersionPromote.error.message); this.exit(1); } if (result.schemaVersionPromote.ok) { this.logSuccess(`Version ${result.schemaVersionPromote.ok.newSchemaVersion.id.substring(0, 8)} was created.`); this.logSuccess('Schema Version successfully promoted.'); if (result.schemaVersionPromote.ok.linkToWebsite) { this.logInfo(`Available at ${result.schemaVersionPromote.ok.linkToWebsite}`); } } } catch (error) { if (error instanceof core_1.Errors.CLIError) { throw error; } else { this.logFailure(`Failed to complete`); throw new errors_1.UnexpectedError(error); } } } } SchemaPromote.description = 'promote a schema version'; SchemaPromote.flags = { 'registry.endpoint': core_1.Flags.string({ description: 'registry endpoint', }), /** @deprecated */ registry: core_1.Flags.string({ description: 'registry address', deprecated: { message: 'use --registry.accessToken instead', version: '0.21.0', }, }), 'registry.accessToken': core_1.Flags.string({ description: 'registry access token', }), /** @deprecated */ token: core_1.Flags.string({ description: 'api token', deprecated: { message: 'use --registry.accessToken instead', version: '0.21.0', }, }), from: core_1.Flags.string({ description: 'The target to which the schema version should be promoted from (slug or ID).' + ' This can either be a slug following the format "$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging")' + ' or an UUID (e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80").', }), version: core_1.Flags.string({ description: 'The specific schema version ID to promote. It must be within the same project as the target the version should be promoted to.', }), to: core_1.Flags.string({ description: 'The target to which the schema version should be promoted to (slug or ID).' + ' This can either be a slug following the format "$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging")' + ' or an UUID (e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80").', required: true, }), }; exports.default = SchemaPromote; //# sourceMappingURL=promote.js.map