@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
52 lines (51 loc) • 2.03 kB
JavaScript
import { __decorate } from "tslib";
import { mergeTypeDefs } from '@graphql-tools/merge';
import { Injectable } from '@nestjs/common';
import { flatten } from 'es-toolkit';
import * as fs from 'fs';
import normalize from 'normalize-path';
import * as util from 'util';
import { globPaths } from './utils/glob.util.js';
const readFile = util.promisify(fs.readFile);
let GraphQLTypesLoader = class GraphQLTypesLoader {
async mergeTypesByPaths(paths) {
if (!paths || paths.length === 0) {
return null;
}
const types = await this.getTypesFromPaths(paths);
const flatTypes = flatten(types);
return mergeTypeDefs(flatTypes, {
throwOnConflict: true,
commentDescriptions: true,
reverseDirectives: true,
});
}
async getTypesFromPaths(paths) {
const includeNodeModules = this.includeNodeModules(paths);
paths = Array.isArray(paths)
? paths.map((path) => normalize(path))
: normalize(paths);
const filePaths = await globPaths(paths, {
ignore: includeNodeModules ? [] : ['node_modules'],
});
if (filePaths.length === 0) {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`No type definitions were found with the specified file name patterns: "${paths}". Please make sure there is at least one file that matches the given patterns.`);
}
const fileContentsPromises = filePaths.sort().map((filePath) => {
return readFile(filePath.toString(), 'utf8');
});
return Promise.all(fileContentsPromises);
}
includeNodeModules(pathOrPaths) {
if (Array.isArray(pathOrPaths)) {
return pathOrPaths.some((path) => path.includes('node_modules'));
}
return pathOrPaths.includes('node_modules');
}
};
GraphQLTypesLoader = __decorate([
Injectable()
], GraphQLTypesLoader);
export { GraphQLTypesLoader };