UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

102 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); const core_1 = require("@graphql-inspector/core"); const core_2 = require("@oclif/core"); const base_command_1 = tslib_1.__importDefault(require("../../base-command")); const config_1 = require("../../helpers/config"); const operations_1 = require("../../helpers/operations"); class OperationsCheck extends base_command_1.default { async run() { var _a; try { const { flags, args } = await this.parse(OperationsCheck); await this.require(flags); 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 operations = await (0, operations_1.loadOperations)(file, { normalize: false, }); if (operations.length === 0) { this.info('No operations found'); this.exit(0); return; } const result = await this.registryApi(registry, token).fetchLatestVersion(); const sdl = (_a = result.latestVersion) === null || _a === void 0 ? void 0 : _a.sdl; if (!sdl) { this.error('No schema found'); } const schema = (0, graphql_1.buildSchema)(sdl, { assumeValidSDL: true, assumeValid: true, }); const invalidOperations = (0, core_1.validate)(schema, operations.map(s => new graphql_1.Source(s.content, s.location))); if (invalidOperations.length === 0) { this.success('All operations are valid'); this.exit(0); return; } this.fail('Some operations are invalid'); this.log(['', `Total: ${operations.length}`, `Invalid: ${invalidOperations.length}`, ''].join('\n')); this.printInvalidDocuments(invalidOperations, 'errors'); } catch (error) { if (error instanceof core_2.Errors.ExitError) { throw error; } else { this.fail('Failed to validate operations'); this.handleFetchError(error); } } } printInvalidDocuments(invalidDocuments, listKey) { invalidDocuments.forEach(doc => { if (doc.errors.length) { this.renderErrors(doc.source.name, doc[listKey]).forEach(line => { this.log(line); }); } }); } renderErrors(sourceName, errors) { const errorsAsString = errors.map(e => ` - ${this.bolderize(e.message)}`).join('\n'); return [`ERROR in ${sourceName}:\n`, errorsAsString, '\n\n']; } } OperationsCheck.description = 'checks operations against a published schema'; OperationsCheck.flags = { registry: core_2.Flags.string({ description: 'registry address', }), token: core_2.Flags.string({ description: 'api token', }), require: core_2.Flags.string({ description: 'Loads specific require.extensions before running the command', default: [], multiple: true, }), }; OperationsCheck.args = [ { name: 'file', required: true, description: 'Glob pattern to find the operations', hidden: false, }, ]; exports.default = OperationsCheck; //# sourceMappingURL=check.js.map