@graphql-markdown/core
Version:
GraphQL-Markdown core package for generating Markdown documentation from a GraphQL schema.
103 lines (102 loc) • 3.8 kB
JavaScript
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.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 Promise.resolve(`${diffModule}`).then(s => __importStar(require(s)));
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;
;