UNPKG

@graphql-markdown/core

Version:

GraphQL-Markdown core package for generating Markdown documentation from a GraphQL schema.

99 lines (98 loc) 3.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getPrinter = void 0; /** * Loads and initializes a printer module for GraphQL schema documentation. * * This function dynamically imports the specified printer module and initializes it * with the provided configuration and options. The printer is responsible for rendering * GraphQL schema documentation in the desired format. * * @param printerModule - The name/path of the printer module to load * @param config - Configuration for the printer including schema, baseURL, and linkRoot * @param options - Additional options for customizing the printer's behavior * @param mdxModule - Optional MDX module for MDX-specific functionality * * @returns A promise that resolves to the initialized Printer instance * * @throws Will throw an error if printerModule is not a string * @throws Will throw an error if config is not provided * @throws Will throw an error if the module specified by printerModule cannot be found * * @example * ```typescript * import { getPrinter } from '@graphql-markdown/core'; * import { buildSchema } from 'graphql'; * * const schema = buildSchema(` * type Query { * hello: String * } * `); * * const printer = await getPrinter( * '@graphql-markdown/printer-legacy', * { * schema, * baseURL: '/docs', * linkRoot: 'graphql' * }, * { * printTypeOptions: { includeDeprecationReasons: true } * } * ); * * const output = printer.printSchema(); * ``` */ const getPrinter = async (printerModule, config, options, mdxModule) => { if (typeof printerModule !== "string") { throw new Error("Invalid printer module name."); } if (!config) { throw new Error("Invalid printer config."); } try { const { Printer } = await Promise.resolve(`${printerModule}`).then(s => __importStar(require(s))); const { schema, baseURL, linkRoot } = config; await Printer.init(schema, baseURL, linkRoot, { ...options }, mdxModule); return Printer; } catch { throw new Error(`Cannot find module '${printerModule}'.`); } }; exports.getPrinter = getPrinter;