UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

177 lines • 6.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); 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 print_table_1 = require("../../helpers/print-table"); const SchemaVersionForActionIdQuery = (0, gql_1.graphql)(/* GraphQL */ ` query SchemaVersionForActionId( $actionId: ID! $includeSDL: Boolean! $includeSupergraph: Boolean! $includeSubgraphs: Boolean! ) { schemaVersionForActionId(actionId: $actionId) { id valid sdl @include(if: $includeSDL) supergraph @include(if: $includeSupergraph) schemas @include(if: $includeSubgraphs) { nodes { __typename ... on SingleSchema { id date } ... on CompositeSchema { id date url service } } total } } } `); class SchemaFetch extends base_command_1.default { async run() { var _a, _b; const { flags, args } = await this.parse(SchemaFetch); const endpoint = this.ensure({ key: 'registry.endpoint', args: flags, env: 'HIVE_REGISTRY', legacyFlagName: 'registry', defaultValue: config_1.graphqlEndpoint, }); const accessToken = this.ensure({ key: 'registry.accessToken', args: flags, legacyFlagName: 'token', env: 'HIVE_TOKEN', message: errors_1.ACCESS_TOKEN_MISSING, }); const actionId = args.actionId; const sdlType = this.ensure({ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore key: 'type', // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore args: flags, defaultValue: 'sdl', }); const result = await this.registryApi(endpoint, accessToken).request({ operation: SchemaVersionForActionIdQuery, variables: { actionId, includeSDL: sdlType === 'sdl', includeSupergraph: sdlType === 'supergraph', includeSubgraphs: sdlType === 'subgraphs', }, }); if (result.schemaVersionForActionId == null) { return this.error(`No schema found for action id ${actionId}`); } if (result.schemaVersionForActionId.valid === false) { return this.error(`Schema is invalid for action id ${actionId}`); } if ((_a = result.schemaVersionForActionId) === null || _a === void 0 ? void 0 : _a.schemas) { const { total, nodes } = result.schemaVersionForActionId.schemas; const table = [ ['service', 'url', 'date'], ...nodes.map(node => { var _a; return [ /** @ts-expect-error: If service is undefined then use id. */ (_a = node.service) !== null && _a !== void 0 ? _a : node.id, node.__typename === 'CompositeSchema' ? node.url : 'n/a', node.date, ]; }), ]; const stats = `subgraphs length: ${total}`; const printed = `${(0, print_table_1.printTable)(table)}\n\r${stats}`; if (flags.write) { const filepath = (0, node_path_1.resolve)(process.cwd(), flags.write); await (0, promises_1.writeFile)(filepath, printed, 'utf8'); } this.log(printed); } else { const schema = (_b = result.schemaVersionForActionId.sdl) !== null && _b !== void 0 ? _b : result.schemaVersionForActionId.supergraph; if (schema == null) { return this.error(`No ${sdlType} found for action id ${actionId}`); } if (flags.write) { const filepath = (0, node_path_1.resolve)(process.cwd(), flags.write); switch ((0, node_path_1.extname)(flags.write.toLowerCase())) { case '.graphql': case '.gql': case '.gqls': case '.graphqls': await (0, promises_1.writeFile)(filepath, schema, 'utf8'); break; default: this.fail(`Unsupported file extension ${(0, node_path_1.extname)(flags.write)}`); this.exit(1); } return; } this.log(schema); } } } SchemaFetch.description = 'fetch a schema, supergraph, or list of subgraphs from the Hive API'; SchemaFetch.flags = { /** @deprecated */ registry: core_1.Flags.string({ description: 'registry address', deprecated: { message: 'use --registry.endpoint instead', version: '0.21.0', }, }), /** @deprecated */ token: core_1.Flags.string({ description: 'api token', deprecated: { message: 'use --registry.accessToken instead', version: '0.21.0', }, }), 'registry.endpoint': core_1.Flags.string({ description: 'registry endpoint', }), 'registry.accessToken': core_1.Flags.string({ description: 'registry access token', }), type: core_1.Flags.string({ aliases: ['T'], description: 'Type to fetch (possible types: sdl, supergraph, subgraphs)', }), write: core_1.Flags.string({ aliases: ['W'], description: 'Write to a file (possible extensions: .graphql, .gql, .gqls, .graphqls)', }), outputFile: core_1.Flags.string({ description: 'whether to write to a file instead of stdout', }), }; SchemaFetch.args = { actionId: core_1.Args.string({ name: 'actionId', required: true, description: 'action id (e.g. commit sha)', hidden: false, }), }; exports.default = SchemaFetch; //# sourceMappingURL=fetch.js.map