@graphql-markdown/core
Version:
GraphQL-Markdown core package for generating Markdown documentation from a GraphQL schema.
152 lines (151 loc) • 4.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfiguration = exports.setLoaderOptions = exports.graphQLConfigExtension = void 0;
const logger_1 = require("@graphql-markdown/logger");
const validation_1 = require("./directives/validation");
/**
* The name of the GraphQL Markdown extension.
* Used to identify the extension in graphql-config.
*/
const EXTENSION_NAME = "graphql-markdown";
/**
* GraphQL extension declaration for graphql-config.
*
* @returns The extension configuration object with name property.
*
* @example
* ```typescript
* // In graphql-config setup
* const config = await loadConfig({
* extensions: [graphQLConfigExtension],
* });
* ```
*/
const graphQLConfigExtension = () => {
return { name: EXTENSION_NAME };
};
exports.graphQLConfigExtension = graphQLConfigExtension;
/**
* Default throw options for loading configuration.
* @internal
*/
const DEFAULT_THROW_OPTIONS = {
throwOnMissing: false,
throwOnEmpty: false,
};
/**
* Sets loader options for GraphQL Markdown loaders.
*
* This function takes a LoaderOption object and merges the provided options
* with any existing options for each loader.
*
* @param loaders - The loader configuration object.
* @param options - The package options to apply to loaders.
* @returns The updated loader configuration.
*
* @example
* ```typescript
* const loaders = {
* TypeScriptLoader: {
* module: "@graphql-markdown/typescript-loader",
* options: { baseDir: "./src" }
* }
* };
* const options = { outputDir: "./docs" };
* const updatedLoaders = setLoaderOptions(loaders, options);
* // Result: loaders with { baseDir: "./src", outputDir: "./docs" }
* ```
*/
// Exported so tests can jest.spyOn via namespace import; fallow can't trace
// dynamic spy interception and production: true excludes the test callers.
// fallow-ignore-next-line unused-export
const setLoaderOptions = (loaders, options) => {
for (const loader in loaders) {
if ((0, validation_1.isLoaderString)(loaders[loader])) {
loaders[loader] = {
module: loaders[loader],
options,
};
}
else {
loaders[loader].options = {
...options,
...loaders[loader].options,
};
}
}
return loaders;
};
exports.setLoaderOptions = setLoaderOptions;
/**
* Loads the GraphQL Markdown configuration from graphql-config.
*
* This function attempts to load the GraphQL config and extract the
* GraphQL Markdown extension configuration for the specified project ID.
* It also normalizes schema configurations.
*
* @param id - The project ID to load configuration for.
* @param options - Optional package options to apply.
* @param throwOptions - Options for controlling throw behavior.
* @returns The extension project configuration if found, otherwise `undefined`.
*
* @throws Will throw an error if throwOnMissing or throwOnEmpty is true and
* the corresponding condition is met.
*
* @example
* ```typescript
* // Basic usage
* const config = await loadConfiguration("my-project");
*
* // With options and throw behavior
* const config = await loadConfiguration(
* "my-project",
* { baseDir: "./src" },
* { throwOnMissing: true, throwOnEmpty: false }
* );
* ```
*/
const loadConfiguration = async (id, options, { throwOnMissing, throwOnEmpty } = DEFAULT_THROW_OPTIONS) => {
let graphQLConfig;
if (typeof id !== "string") {
return undefined;
}
try {
graphQLConfig = await import("graphql-config");
}
catch {
(0, logger_1.log)("Cannot find module 'graphql-config'!");
return undefined;
}
const config = await graphQLConfig.loadConfig({
...options,
extensions: [exports.graphQLConfigExtension],
throwOnMissing,
throwOnEmpty,
});
if (!config) {
return undefined;
}
try {
const projectConfig = config
.getProject(id)
.extension(EXTENSION_NAME);
if (Array.isArray(projectConfig.schema)) {
const schema = projectConfig.schema[0];
if ((0, validation_1.isSchemaString)(schema)) {
projectConfig.schema = schema;
}
else if ((0, validation_1.isSchemaObject)(schema)) {
projectConfig.schema = Object.keys(schema)[0];
if (projectConfig.loaders) {
projectConfig.loaders = (0, exports.setLoaderOptions)(projectConfig.loaders, Object.values(schema)[0]);
}
}
}
return projectConfig;
}
catch {
return undefined;
}
};
exports.loadConfiguration = loadConfiguration;