UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

160 lines • 6.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const zod_1 = require("zod"); const core_1 = require("@oclif/core"); const base_command_1 = tslib_1.__importDefault(require("../../base-command")); const gql_1 = require("../../gql"); const graphql_1 = require("../../gql/graphql"); const config_1 = require("../../helpers/config"); class AppCreate extends base_command_1.default { async run() { const { flags, args } = await this.parse(AppCreate); const endpoint = this.ensure({ key: 'registry.endpoint', args: flags, defaultValue: config_1.graphqlEndpoint, env: 'HIVE_REGISTRY', }); const accessToken = this.ensure({ key: 'registry.accessToken', args: flags, env: 'HIVE_TOKEN', }); const file = args.file; const fs = await Promise.resolve().then(() => tslib_1.__importStar(require('fs/promises'))); const contents = await fs.readFile(file, 'utf-8'); const operations = JSON.parse(contents); const validationResult = ManifestModel.safeParse(operations); if (validationResult.success === false) { // TODO: better error message :) throw new Error('Invalid manifest'); } const result = await this.registryApi(endpoint, accessToken).request({ operation: CreateAppDeploymentMutation, variables: { input: { appName: flags['name'], appVersion: flags['version'], }, }, }); if (result.createAppDeployment.error) { // TODO: better error message formatting :) throw new Error(result.createAppDeployment.error.message); } if (!result.createAppDeployment.ok) { throw new Error('Unknown error'); } if (result.createAppDeployment.ok.createdAppDeployment.status !== graphql_1.AppDeploymentStatus.Pending) { this.log(`App deployment "${flags['name']}@${flags['version']}" is "${result.createAppDeployment.ok.createdAppDeployment.status}". Skip uploading documents...`); return; } let buffer = []; const flush = async (force = false) => { if (buffer.length >= 100 || force) { const result = await this.registryApi(endpoint, accessToken).request({ operation: AddDocumentsToAppDeploymentMutation, variables: { input: { appName: flags['name'], appVersion: flags['version'], documents: buffer, }, }, }); if (result.addDocumentsToAppDeployment.error) { if (result.addDocumentsToAppDeployment.error.details) { const affectedOperation = buffer.at(result.addDocumentsToAppDeployment.error.details.index); const maxCharacters = 40; if (affectedOperation) { const truncatedBody = (affectedOperation.body.length > maxCharacters - 3 ? affectedOperation.body.substring(0, maxCharacters) + '...' : affectedOperation.body).replace(/\n/g, '\\n'); this.infoWarning(`Failed uploading document: ${result.addDocumentsToAppDeployment.error.details.message}` + `\nOperation hash: ${affectedOperation === null || affectedOperation === void 0 ? void 0 : affectedOperation.hash}` + `\nOperation body: ${truncatedBody}`); } } this.error(result.addDocumentsToAppDeployment.error.message); } buffer = []; } }; let counter = 0; for (const [hash, body] of Object.entries(validationResult.data)) { buffer.push({ hash, body }); await flush(); counter++; } await flush(true); this.log(`\nApp deployment "${flags['name']}@${flags['version']}" (${counter} operations) created.\nActive it with the "hive app:publish" command.`); } } AppCreate.description = 'create an app deployment'; AppCreate.flags = { 'registry.endpoint': core_1.Flags.string({ description: 'registry endpoint', }), 'registry.accessToken': core_1.Flags.string({ description: 'registry access token', }), name: core_1.Flags.string({ description: 'app name', required: true, }), version: core_1.Flags.string({ description: 'app version', required: true, }), }; AppCreate.args = { file: core_1.Args.string({ name: 'file', required: true, description: 'Path to the persisted operations mapping.', hidden: false, }), }; exports.default = AppCreate; const ManifestModel = zod_1.z.record(zod_1.z.string()); const CreateAppDeploymentMutation = (0, gql_1.graphql)(/* GraphQL */ ` mutation CreateAppDeployment($input: CreateAppDeploymentInput!) { createAppDeployment(input: $input) { ok { createdAppDeployment { id name version status } } error { message } } } `); const AddDocumentsToAppDeploymentMutation = (0, gql_1.graphql)(/* GraphQL */ ` mutation AddDocumentsToAppDeployment($input: AddDocumentsToAppDeploymentInput!) { addDocumentsToAppDeployment(input: $input) { ok { appDeployment { id name version status } } error { message details { index message __typename } } } } `); //# sourceMappingURL=create.js.map