1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.GraphQLDefinitionsFactory = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
|
6 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
7 | const apollo_server_core_1 = require("apollo-server-core");
|
8 | const chokidar = require("chokidar");
|
9 | const graphql_1 = require("graphql");
|
10 | const schema_1 = require("@graphql-tools/schema");
|
11 | const graphql_ast_explorer_1 = require("./graphql-ast.explorer");
|
12 | const graphql_types_loader_1 = require("./graphql-types.loader");
|
13 | const utils_1 = require("./utils");
|
14 | class GraphQLDefinitionsFactory {
|
15 | constructor() {
|
16 | this.gqlAstExplorer = new graphql_ast_explorer_1.GraphQLAstExplorer();
|
17 | this.gqlTypesLoader = new graphql_types_loader_1.GraphQLTypesLoader();
|
18 | }
|
19 | generate(options) {
|
20 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
21 | const isDebugEnabled = !(options && options.debug === false);
|
22 | const typePathsExists = options.typePaths && !shared_utils_1.isEmpty(options.typePaths);
|
23 | if (!typePathsExists) {
|
24 | throw new Error(`"typePaths" property cannot be empty.`);
|
25 | }
|
26 | const isFederation = options && options.federation;
|
27 | const definitionsGeneratorOptions = {
|
28 | emitTypenameField: options.emitTypenameField,
|
29 | skipResolverArgs: options.skipResolverArgs,
|
30 | defaultScalarType: options.defaultScalarType,
|
31 | customScalarTypeMapping: options.customScalarTypeMapping,
|
32 | additionalHeader: options.additionalHeader,
|
33 | };
|
34 | if (options.watch) {
|
35 | this.printMessage('GraphQL factory is watching your files...', isDebugEnabled);
|
36 | const watcher = chokidar.watch(options.typePaths);
|
37 | watcher.on('change', (file) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
38 | this.printMessage(`[${new Date().toLocaleTimeString()}] "${file}" has been changed.`, isDebugEnabled);
|
39 | yield this.exploreAndEmit(options.typePaths, options.path, options.outputAs, isFederation, isDebugEnabled, definitionsGeneratorOptions);
|
40 | }));
|
41 | }
|
42 | yield this.exploreAndEmit(options.typePaths, options.path, options.outputAs, isFederation, isDebugEnabled, definitionsGeneratorOptions);
|
43 | });
|
44 | }
|
45 | exploreAndEmit(typePaths, path, outputAs, isFederation, isDebugEnabled, definitionsGeneratorOptions = {}) {
|
46 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
47 | if (isFederation) {
|
48 | return this.exploreAndEmitFederation(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions);
|
49 | }
|
50 | return this.exploreAndEmitRegular(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions);
|
51 | });
|
52 | }
|
53 | exploreAndEmitFederation(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions) {
|
54 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
55 | const typeDefs = yield this.gqlTypesLoader.mergeTypesByPaths(typePaths);
|
56 | const { buildFederatedSchema, printSchema, } = load_package_util_1.loadPackage('@apollo/federation', 'ApolloFederation', () => require('@apollo/federation'));
|
57 | const schema = buildFederatedSchema([
|
58 | {
|
59 | typeDefs: apollo_server_core_1.gql `
|
60 | ${typeDefs}
|
61 | `,
|
62 | resolvers: {},
|
63 | },
|
64 | ]);
|
65 | const tsFile = yield this.gqlAstExplorer.explore(apollo_server_core_1.gql `
|
66 | ${printSchema(schema)}
|
67 | `, path, outputAs, definitionsGeneratorOptions);
|
68 | yield tsFile.save();
|
69 | this.printMessage(`[${new Date().toLocaleTimeString()}] The definitions have been updated.`, isDebugEnabled);
|
70 | });
|
71 | }
|
72 | exploreAndEmitRegular(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions) {
|
73 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
74 | const typeDefs = yield this.gqlTypesLoader.mergeTypesByPaths(typePaths || []);
|
75 | if (!typeDefs) {
|
76 | throw new Error(`"typeDefs" property cannot be null.`);
|
77 | }
|
78 | let schema = schema_1.makeExecutableSchema({
|
79 | typeDefs,
|
80 | resolverValidationOptions: { allowResolversNotInSchema: true },
|
81 | });
|
82 | schema = utils_1.removeTempField(schema);
|
83 | const tsFile = yield this.gqlAstExplorer.explore(apollo_server_core_1.gql `
|
84 | ${graphql_1.printSchema(schema)}
|
85 | `, path, outputAs, definitionsGeneratorOptions);
|
86 | yield tsFile.save();
|
87 | this.printMessage(`[${new Date().toLocaleTimeString()}] The definitions have been updated.`, isDebugEnabled);
|
88 | });
|
89 | }
|
90 | printMessage(text, isEnabled) {
|
91 | isEnabled && console.log(text);
|
92 | }
|
93 | }
|
94 | exports.GraphQLDefinitionsFactory = GraphQLDefinitionsFactory;
|