UNPKG

3.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.GraphQLDefinitionsFactory = void 0;
4const schema_1 = require("@graphql-tools/schema");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const chokidar = require("chokidar");
7const graphql_1 = require("graphql");
8const graphql_tag_1 = require("graphql-tag");
9const graphql_ast_explorer_1 = require("./graphql-ast.explorer");
10const graphql_types_loader_1 = require("./graphql-types.loader");
11const utils_1 = require("./utils");
12class GraphQLDefinitionsFactory {
13 constructor() {
14 this.gqlAstExplorer = new graphql_ast_explorer_1.GraphQLAstExplorer();
15 this.gqlTypesLoader = new graphql_types_loader_1.GraphQLTypesLoader();
16 }
17 async generate(options) {
18 const isDebugEnabled = !(options && options.debug === false);
19 const typePathsExists = options.typePaths && !(0, shared_utils_1.isEmpty)(options.typePaths);
20 if (!typePathsExists) {
21 throw new Error(`"typePaths" property cannot be empty.`);
22 }
23 const definitionsGeneratorOptions = {
24 emitTypenameField: options.emitTypenameField,
25 skipResolverArgs: options.skipResolverArgs,
26 defaultScalarType: options.defaultScalarType,
27 customScalarTypeMapping: options.customScalarTypeMapping,
28 additionalHeader: options.additionalHeader,
29 defaultTypeMapping: options.defaultTypeMapping,
30 enumsAsTypes: options.enumsAsTypes,
31 };
32 if (options.watch) {
33 this.printMessage('GraphQL factory is watching your files...', isDebugEnabled);
34 const watcher = chokidar.watch(options.typePaths);
35 watcher.on('change', async (file) => {
36 this.printMessage(`[${new Date().toLocaleTimeString()}] "${file}" has been changed.`, isDebugEnabled);
37 await this.exploreAndEmit(options.typePaths, options.path, options.outputAs, isDebugEnabled, definitionsGeneratorOptions, options.typeDefs);
38 });
39 }
40 await this.exploreAndEmit(options.typePaths, options.path, options.outputAs, isDebugEnabled, definitionsGeneratorOptions, options.typeDefs);
41 }
42 async exploreAndEmit(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions, typeDefs) {
43 const typePathDefs = await this.gqlTypesLoader.mergeTypesByPaths(typePaths || []);
44 const mergedTypeDefs = (0, utils_1.extend)(typePathDefs, typeDefs);
45 if (!mergedTypeDefs) {
46 throw new Error(`"typeDefs" property cannot be null.`);
47 }
48 let schema = (0, schema_1.makeExecutableSchema)({
49 typeDefs: mergedTypeDefs,
50 resolverValidationOptions: { requireResolversToMatchSchema: 'ignore' },
51 });
52 schema = (0, utils_1.removeTempField)(schema);
53 const tsFile = await this.gqlAstExplorer.explore((0, graphql_tag_1.gql) `
54 ${(0, graphql_1.printSchema)(schema)}
55 `, path, outputAs, definitionsGeneratorOptions);
56 await tsFile.save();
57 this.printMessage(`[${new Date().toLocaleTimeString()}] The definitions have been updated.`, isDebugEnabled);
58 }
59 printMessage(text, isEnabled) {
60 isEnabled && console.log(text);
61 }
62}
63exports.GraphQLDefinitionsFactory = GraphQLDefinitionsFactory;