UNPKG

@graphql-markdown/core

Version:

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

70 lines (69 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasChanges = void 0; const logger_1 = require("@graphql-markdown/logger"); /** * Module for detecting changes in GraphQL schemas. * This module provides functionality to compare GraphQL schemas and detect changes. * * @module diff * @category Core * @since 1.0.0 */ /** * Determines if there are changes in the GraphQL schema by using a specified diff method and module. * * @param schema - The GraphQL schema to check for changes. * @param tmpDir - The temporary directory to store intermediate files during the diff process. * @param diffMethod - The name of the diff method to use. Must be a string or `null`. * @param diffModule - The module to import for performing the diff. Defaults to `@graphql-markdown/diff`. * @returns A promise that resolves to `true` if changes are detected or if the diff method/module is invalid, otherwise `false`. * * @example * ```typescript * import { hasChanges } from "./diff"; * import { buildSchema } from "graphql"; * * const schema = buildSchema(` * type Query { * hello: String * } * `); * * const changesDetected = await hasChanges(schema, "/tmp", "methodName"); * console.log(changesDetected); // true or false * ``` * * @example Using with a custom diff module * ```typescript * import { hasChanges } from "./diff"; * * const schema = getMySchema(); * const result = await hasChanges( * schema, * "/tmp/schema-diff", * "breaking", * "./my-custom-diff-module" * ); * ``` * * @throws Will log a warning if the specified diff module cannot be found. * @see {@link DiffMethodName} for available diff methods * @see {@link FunctionCheckSchemaChanges} for the signature of the function imported from the diff module * @category Schema * @since 1.0.0 */ const hasChanges = async (schema, tmpDir, diffMethod, diffModule = "@graphql-markdown/diff") => { if (typeof diffMethod !== "string" || typeof diffModule !== "string") { return true; } try { const { checkSchemaChanges, } = await import(diffModule); return await checkSchemaChanges(schema, tmpDir, diffMethod); } catch { (0, logger_1.log)(`Cannot find module '${diffModule}' from @graphql-markdown/core!`, logger_1.LogLevel.warn); return true; } }; exports.hasChanges = hasChanges;