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