@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
50 lines (49 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDocuments = void 0;
const tslib_1 = require("tslib");
const path_1 = require("path");
const debug_1 = tslib_1.__importDefault(require("debug"));
const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
const cache_js_1 = require("./cache.js");
const debug = (0, debug_1.default)('graphql-eslint:operations');
const operationsCache = new cache_js_1.ModuleCache();
const handleVirtualPath = (documents) => {
const filepathMap = Object.create(null);
return documents.map(source => {
var _a;
const location = source.location;
if (['.gql', '.graphql'].some(extension => location.endsWith(extension))) {
return source;
}
(_a = filepathMap[location]) !== null && _a !== void 0 ? _a : (filepathMap[location] = -1);
const index = (filepathMap[location] += 1);
return {
...source,
location: (0, path_1.resolve)(location, `${index}_document.graphql`),
};
});
};
const getDocuments = (project) => {
const documentsKey = project.documents;
if (!documentsKey) {
return [];
}
let siblings = operationsCache.get(documentsKey);
if (!siblings) {
debug('Loading operations from %o', project.documents);
const documents = project.loadDocumentsSync(project.documents, {
skipGraphQLImport: true,
pluckConfig: project.extensions.pluckConfig,
});
if (debug.enabled) {
debug('Loaded %d operations', documents.length);
const operationsPaths = fast_glob_1.default.sync(project.documents, { absolute: true });
debug('Operations pointers %O', operationsPaths);
}
siblings = handleVirtualPath(documents);
operationsCache.set(documentsKey, siblings);
}
return siblings;
};
exports.getDocuments = getDocuments;