UNPKG

@graphql-inspector/introspect-command

Version:
73 lines (72 loc) 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = handler; const fs_1 = require("fs"); const path_1 = require("path"); const graphql_1 = require("graphql"); const commands_1 = require("@graphql-inspector/commands"); const logger_1 = require("@graphql-inspector/logger"); function handler({ schema: unsortedSchema, output, comments, }) { const schema = (0, graphql_1.lexicographicSortSchema)(unsortedSchema); const introspection = (0, graphql_1.introspectionFromSchema)(schema); const filepath = (0, path_1.resolve)(process.cwd(), output); let content; switch ((0, path_1.extname)(output.toLowerCase())) { case '.graphql': case '.gql': case '.gqls': case '.graphqls': content = graphql_1.printSchema(schema, { commentDescriptions: comments, }); break; case '.json': content = JSON.stringify(introspection, null, 2); break; default: throw new Error('Only .graphql, .gql and .json files are supported'); } (0, fs_1.writeFileSync)(output, content, 'utf8'); logger_1.Logger.success(`Saved to ${filepath}`); } exports.default = (0, commands_1.createCommand)(api => { const { loaders } = api; return { command: 'introspect <schema>', describe: 'Introspect a schema', builder(yargs) { return yargs .positional('schema', { describe: 'Point to a schema', type: 'string', demandOption: true, }) .options({ w: { alias: 'write', describe: 'Write to a file', type: 'string', }, comments: { describe: 'Use preceding comments as the description', type: 'boolean', }, }) .default('w', 'graphql.schema.json'); }, async handler(args) { const { headers, token } = (0, commands_1.parseGlobalArgs)(args); const output = args.write; const comments = args.comments || false; const apolloFederation = args.federation || false; const aws = args.aws || false; const method = args.method?.toUpperCase() || 'POST'; const schema = await loaders.loadSchema(args.schema, { token, headers, method, }, apolloFederation, aws); return handler({ schema, output, comments }); }, }; });