@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
56 lines (55 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadGraphQLConfig = exports.loadOnDiskGraphQLConfig = void 0;
const tslib_1 = require("tslib");
const path_1 = require("path");
const debug_1 = tslib_1.__importDefault(require("debug"));
const graphql_config_1 = require("graphql-config");
const code_file_loader_1 = require("@graphql-tools/code-file-loader");
const debug = (0, debug_1.default)('graphql-eslint:graphql-config');
let graphQLConfig;
function loadOnDiskGraphQLConfig(filePath) {
return (0, graphql_config_1.loadConfigSync)({
// load config relative to the file being linted
rootDir: (0, path_1.dirname)(filePath),
throwOnEmpty: false,
throwOnMissing: false,
extensions: [codeFileLoaderExtension],
});
}
exports.loadOnDiskGraphQLConfig = loadOnDiskGraphQLConfig;
function loadGraphQLConfig(options) {
// We don't want cache config on test environment
// Otherwise schema and documents will be same for all tests
if (process.env.NODE_ENV !== 'test' && graphQLConfig) {
return graphQLConfig;
}
const onDiskConfig = !options.skipGraphQLConfig && loadOnDiskGraphQLConfig(options.filePath);
debug('options.skipGraphQLConfig: %o', options.skipGraphQLConfig);
if (onDiskConfig) {
debug('Graphql-config path %o', onDiskConfig.filepath);
}
const configOptions = options.projects
? { projects: options.projects }
: {
schema: (options.schema || ''),
documents: options.documents || options.operations,
extensions: options.extensions,
include: options.include,
exclude: options.exclude,
};
graphQLConfig =
onDiskConfig ||
new graphql_config_1.GraphQLConfig({
config: configOptions,
filepath: 'virtual-config',
}, [codeFileLoaderExtension]);
return graphQLConfig;
}
exports.loadGraphQLConfig = loadGraphQLConfig;
const codeFileLoaderExtension = api => {
const { schema, documents } = api.loaders;
schema.register(new code_file_loader_1.CodeFileLoader());
documents.register(new code_file_loader_1.CodeFileLoader());
return { name: 'graphql-eslint-loaders' };
};