UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

227 lines • 8.65 kB
"use strict"; 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 gql_1 = require("../../gql"); const config_1 = require("../../helpers/config"); const errors_1 = require("../../helpers/errors"); const git_1 = require("../../helpers/git"); const schema_1 = require("../../helpers/schema"); const TargetInput = tslib_1.__importStar(require("../../helpers/target-input")); const proposeSchemaMutation = (0, gql_1.graphql)(/* GraphQL */ ` mutation proposeSchema($input: CreateSchemaProposalInput!) { createSchemaProposal(input: $input) { __typename ok { schemaProposal { id } } error { message ... on CreateSchemaProposalError { details { description title } } } } } `); class ProposalCreate extends base_command_1.default { async run() { var _a; try { const { flags, args } = await this.parse(ProposalCreate); let target = null; { const result = TargetInput.parse(flags.target); if (result.type === 'error') { throw new errors_1.InvalidTargetError(); } target = result.data; } const service = flags.service; const usesGitHubApp = flags.github === true; let endpoint, accessToken; try { endpoint = this.ensure({ key: 'registry.endpoint', args: flags, legacyFlagName: 'registry', defaultValue: config_1.graphqlEndpoint, env: 'HIVE_REGISTRY', description: ProposalCreate.flags['registry.endpoint'].description, }); } catch (e) { throw new errors_1.MissingEndpointError(); } const file = args.file; try { accessToken = this.ensure({ key: 'registry.accessToken', args: flags, legacyFlagName: 'token', env: 'HIVE_TOKEN', description: ProposalCreate.flags['registry.accessToken'].description, }); } catch (e) { throw new errors_1.MissingRegistryTokenError(); } const sdl = await (0, schema_1.loadSchema)(file).catch(e => { throw new errors_1.SchemaFileNotFoundError(file, e); }); const git = await (0, git_1.gitInfo)(() => { // noop }); const commit = flags.commit || (git === null || git === void 0 ? void 0 : git.commit); const author = flags.author || (git === null || git === void 0 ? void 0 : git.author); if (!author) { throw new errors_1.AuthorRequiredError(); } if (typeof sdl !== 'string' || sdl.length === 0) { throw new errors_1.SchemaFileEmptyError(file); } let github = null; if (usesGitHubApp) { if (!commit) { throw new errors_1.CommitRequiredError(); } if (!git.repository) { throw new errors_1.GithubRepositoryRequiredError(); } if (!git.pullRequestNumber) { this.warn("Could not resolve pull request number. Are you running this command on a 'pull_request' event?\n" + 'See https://the-guild.dev/graphql/hive/docs/other-integrations/ci-cd#github-workflow-for-ci'); } github = { commit: commit, repository: git.repository, pullRequestNumber: git.pullRequestNumber, }; } const result = await this.registryApi(endpoint, accessToken).request({ operation: proposeSchemaMutation, variables: { input: { target, title: flags.title, description: flags.description, isDraft: flags.draft, author, initialChecks: [ { service, sdl: (0, schema_1.minifySchema)(sdl), github, meta: !!commit && !!author ? { commit, author, } : null, contextId: (_a = flags.contextId) !== null && _a !== void 0 ? _a : undefined, url: flags.url, }, ], }, }, }); if (result.createSchemaProposal.ok) { const id = result.createSchemaProposal.ok.schemaProposal.id; if (id) { this.logSuccess(`Created proposal ${id}.`); } if (result.createSchemaProposal.error) { throw new errors_1.APIError(result.createSchemaProposal.error.message); } } } catch (error) { if (error instanceof core_1.Errors.CLIError) { throw error; } else { this.logFailure('Failed to create schema proposal'); throw new errors_1.UnexpectedError(error); } } } } ProposalCreate.description = 'Proposes a schema change'; ProposalCreate.flags = { 'registry.endpoint': core_1.Flags.string({ description: 'registry endpoint', }), /** @deprecated */ registry: core_1.Flags.string({ description: 'registry address', deprecated: { message: 'use --registry.endpoint 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', }, }), target: core_1.Flags.string({ required: true, description: 'The target against which to propose the schema (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").', }), title: core_1.Flags.string({ required: true, description: 'Title of the proposal. This should be a short description of the change.', }), description: core_1.Flags.string({ required: false, description: 'Description of the proposal. This should be a more detailed explanation of the change.', }), draft: core_1.Flags.boolean({ default: false, description: 'Set to true to open the proposal as a Draft. This indicates the proposal is still in progress.', }), /** CLI Only supports service at a time right now. */ service: core_1.Flags.string({ description: 'service name (only for distributed schemas)', }), github: core_1.Flags.boolean({ description: 'Connect with GitHub Application', default: false, }), author: core_1.Flags.string({ description: 'Author of the change', }), commit: core_1.Flags.string({ description: 'Associated commit sha', }), contextId: core_1.Flags.string({ description: 'Context ID for grouping the schema check.', }), url: core_1.Flags.string({ description: 'If checking a service, then you can optionally provide the service URL to see the difference in the supergraph during the check.', }), }; ProposalCreate.args = { file: core_1.Args.string({ name: 'file', required: true, description: 'Path to the schema file(s)', hidden: false, }), }; exports.default = ProposalCreate; //# sourceMappingURL=create.js.map