UNPKG

@graphql-eslint/eslint-plugin

Version:
45 lines (44 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSchema = void 0; const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); const debug_1 = tslib_1.__importDefault(require("debug")); const fast_glob_1 = tslib_1.__importDefault(require("fast-glob")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const cache_js_1 = require("./cache.js"); const schemaCache = new cache_js_1.ModuleCache(); const debug = (0, debug_1.default)('graphql-eslint:schema'); function getSchema(project, schemaOptions) { const schemaKey = project.schema; if (!schemaKey) { return null; } const cache = schemaCache.get(schemaKey); if (cache) { return cache; } let schema; try { debug('Loading schema from %o', project.schema); schema = project.loadSchemaSync(project.schema, 'GraphQLSchema', { ...schemaOptions, pluckConfig: project.extensions.pluckConfig, }); if (debug.enabled) { debug('Schema loaded: %o', schema instanceof graphql_1.GraphQLSchema); const schemaPaths = fast_glob_1.default.sync(project.schema, { absolute: true }); debug('Schema pointers %O', schemaPaths); } // Do not set error to cache, since cache reload will be done after some `lifetime` seconds schemaCache.set(schemaKey, schema); } catch (error) { if (error instanceof Error) { error.message = chalk_1.default.red(`Error while loading schema: ${error.message}`); } schema = error; } return schema; } exports.getSchema = getSchema;