UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

153 lines • 5.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); 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 schema_1 = require("../../helpers/schema"); const TargetInput = tslib_1.__importStar(require("../../helpers/target-input")); const validation_1 = require("../../helpers/validation"); const schemaPushMutation = (0, gql_1.graphql)(/* GraphQL */ ` mutation CLI_SchemaPushMutation($input: SchemaPushInput!) { schemaPush(input: $input) { ok { schemaRevision { service revision digest } } error { message } } } `); class SchemaPush extends base_command_1.default { async run() { try { const { flags, args } = await this.parse(SchemaPush); await this.require(flags); let endpoint; let accessToken; try { endpoint = this.ensure({ key: 'registry.endpoint', args: flags, legacyFlagName: 'registry', defaultValue: config_1.graphqlEndpoint, env: 'HIVE_REGISTRY', description: SchemaPush.flags['registry.endpoint'].description, }); } catch (error) { this.logDebug(error); throw new errors_1.MissingEndpointError(); } try { accessToken = this.ensure({ key: 'registry.accessToken', args: flags, legacyFlagName: 'token', env: 'HIVE_TOKEN', description: SchemaPush.flags['registry.accessToken'].description, }); } catch (error) { this.logDebug(error); throw new errors_1.MissingRegistryTokenError(); } const target = TargetInput.parse(flags.target); if (target.type === 'error') { throw new errors_1.InvalidTargetError(); } let sdl; try { const rawSdl = await (0, schema_1.loadSchema)('first-federation-then-graphql-introspection', args.file, { logger: this.logger, }); (0, validation_1.invariant)(typeof rawSdl === 'string' && rawSdl.length > 0, 'Schema seems empty'); sdl = (0, schema_1.minifySchema)(rawSdl); } catch (error) { if (error instanceof graphql_1.GraphQLError) { throw new errors_1.InvalidSDLError(error); } throw error; } const result = await this.registryApi(endpoint, accessToken).request({ operation: schemaPushMutation, variables: { input: { target: target.data, service: flags.service, revision: flags.revision, sdl, }, }, timeout: 55000, }); if (result.schemaPush.error) { throw new errors_1.APIError(result.schemaPush.error.message); } if (!result.schemaPush.ok) { throw new errors_1.APIError('Schema push returned no result.'); } const revision = result.schemaPush.ok.schemaRevision; this.logSuccess('Schema revision pushed.'); if (revision.service) { this.logInfo(`Service: ${revision.service}`); } this.logInfo(`Revision: ${revision.revision}`); this.logInfo(`Digest: ${revision.digest}`); } catch (error) { if (error instanceof core_1.Errors.CLIError) { throw error; } this.logFailure('Failed to push schema revision'); throw new errors_1.UnexpectedError(error instanceof Error ? error.message : JSON.stringify(error)); } } } SchemaPush.description = 'pushes a schema revision for later publication'; SchemaPush.flags = { target: core_1.Flags.string({ required: true, description: 'The target to push against as "$organizationSlug/$projectSlug/$targetSlug" or a target UUID.', }), service: core_1.Flags.string({ description: 'service name (required for distributed schemas)', }), revision: core_1.Flags.string({ required: true, description: 'immutable schema revision, such as a commit SHA', }), 'registry.endpoint': core_1.Flags.string({ description: 'registry endpoint' }), 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' }), token: core_1.Flags.string({ description: 'api token', deprecated: { message: 'use --registry.accessToken instead', version: '0.21.0' }, }), require: core_1.Flags.string({ description: 'Loads specific require.extensions before running the codegen and reading the configuration', default: [], multiple: true, }), }; SchemaPush.args = { file: core_1.Args.string({ name: 'file', required: true, description: 'Path to the schema file(s)', }), }; exports.default = SchemaPush; //# sourceMappingURL=push.js.map