UNPKG

2.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.GraphQLTypesLoader = void 0;
4const tslib_1 = require("tslib");
5const merge_1 = require("@graphql-tools/merge");
6const common_1 = require("@nestjs/common");
7const glob = require("fast-glob");
8const fs = require("fs");
9const lodash_1 = require("lodash");
10const util = require("util");
11const normalize = require('normalize-path');
12const readFile = util.promisify(fs.readFile);
13let GraphQLTypesLoader = class GraphQLTypesLoader {
14 mergeTypesByPaths(paths) {
15 return tslib_1.__awaiter(this, void 0, void 0, function* () {
16 if (!paths || paths.length === 0) {
17 return null;
18 }
19 const types = yield this.getTypesFromPaths(paths);
20 const flatTypes = lodash_1.flatten(types);
21 return merge_1.mergeTypeDefs(flatTypes, {
22 throwOnConflict: true,
23 commentDescriptions: true,
24 reverseDirectives: true,
25 });
26 });
27 }
28 getTypesFromPaths(paths) {
29 return tslib_1.__awaiter(this, void 0, void 0, function* () {
30 const includeNodeModules = this.includeNodeModules(paths);
31 paths = Array.isArray(paths)
32 ? paths.map((path) => normalize(path))
33 : normalize(paths);
34 const filePaths = yield glob(paths, {
35 ignore: includeNodeModules ? [] : ['node_modules'],
36 });
37 if (filePaths.length === 0) {
38 throw new Error(`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.`);
39 }
40 const fileContentsPromises = filePaths.sort().map((filePath) => {
41 return readFile(filePath.toString(), 'utf8');
42 });
43 return Promise.all(fileContentsPromises);
44 });
45 }
46 includeNodeModules(pathOrPaths) {
47 if (Array.isArray(pathOrPaths)) {
48 return pathOrPaths.some((path) => path.includes('node_modules'));
49 }
50 return pathOrPaths.includes('node_modules');
51 }
52};
53GraphQLTypesLoader = tslib_1.__decorate([
54 common_1.Injectable()
55], GraphQLTypesLoader);
56exports.GraphQLTypesLoader = GraphQLTypesLoader;