UNPKG

@graphql-eslint/eslint-plugin

Version:
40 lines (39 loc) 1.35 kB
import { GraphQLSchema } from 'graphql'; import debugFactory from 'debug'; import fg from 'fast-glob'; import chalk from 'chalk'; import { ModuleCache } from './cache.js'; const schemaCache = new ModuleCache(); const debug = debugFactory('graphql-eslint:schema'); export 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 GraphQLSchema); const schemaPaths = fg.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.red(`Error while loading schema: ${error.message}`); } schema = error; } return schema; }